类似postman接口 在线测试API接口
哈哈我也不太懂接口 还没前后端整合过呢
也就是注释你的各种接口吧 可有可无

先说依赖问题
这个是swagger2的依赖
当然出现问题了 已经更新到了swagger3了
参考:https://cloud.tencent.com/developer/article/1936862

         https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.10.5</version>
        </dependency>

swagger3 整合了两个依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

image
一般项目中两个配置文件 一个是生产开发 一个则为运行部署的

主要写的config

@Configuration
@EnableOpenApi
public class Myconfig {

    @Bean
    public Docket docket1( ) {
return new Docket(DocumentationType.SWAGGER_2)
        .groupName("高远")
        ;
    }

    @Bean
    public Docket docket2( ) {
        return new Docket(DocumentationType.SWAGGER_2).groupName("哈哈哈");
    }

    //swagger 在生产中使用 发布不使用
    //1.判断是不是生产环境
    //2.是否enable启动
    @Bean
    public Docket docket(Environment environment) {
        Profiles profiles = Profiles.of("dev");
        //获取项目环境
        boolean b = environment.acceptsProfiles(profiles);


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(b)
                .groupName("芳芳")
                .select()
                //requestHandlerSelector  配置要扫描接口的方式
                //basePackage指定要扫描的包
                .apis(RequestHandlerSelectors.basePackage("swagger.controller"))
                //过滤什么路径
//                .paths(PathSelectors.ant(""))
                .build();
    }

    //默认进入swagger.html中显示的东西 可更改
    public ApiInfo apiInfo() {
        return new ApiInfo(
                "芳芳",
                "一个描述",
                "1.0",
                "",  //Terms of service
                //作者信息
                new Contact(
                        "芳芳",
                        "https://www.baidu.com/",
                        "2047629848@qq.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }

展示几个实体类参数:

@Data
@ApiModel("这是一个注释")
public class User {
    @ApiModelProperty("用户名")
    private String name;
    @ApiModelProperty("密码")
    private String pwd;
}
controller:
@Controller
@ResponseBody
public class hello {
    @GetMapping(value = "/hello")
    public String hello(){
        return "高低远近";
    }
    @PostMapping(value = "/user")
    public User user(){
        return new User();
    }
    @ApiOperation("高低远近")
    @GetMapping("/hello2")
    public String hello2(@ApiParam("啊啊啊") String name){
        return "hello"+name;
    }

访问的是这个默认:
image
效果如图:
image
如果你传的实体类参数没过来是getter和setter方法没写
参考:https://www.jb51.net/article/234181.htm

posted on 2024-04-16 20:49  蒸饺  阅读(17)  评论(0)    收藏  举报