- 1、引入依赖
- 2、启动类注入@EnableOpenApi
- 3、写测试类
- 4、查看swagger-ui文档
- 5、添加API注释
- 6、设置配置信息及分组
2、启动类注入@EnableOpenApiio.springfox springfox-boot-starter3.0.0
@EnableOpenApi @SpringBootApplication public class SwaggerDemoApplication { public static void main(String[] args) { SpringApplication.run(SwaggerDemoApplication.class, args); } }3、写测试类
http://127.0.0.1:8090/test1
@RestController public class SwaggerApiController { @RequestMapping("/test1") public String index(){ return "Hello Word"; } }4、查看swagger-ui文档
http://127.0.0.1:8090/swagger-ui/
5、添加API注释开发组dev001
@Api(tags = "SwaggerAPIController测试类") @RestController public class SwaggerApiController { @ApiOperation("测试方法") @RequestMapping("/test1") public String index(){ return "Hello Word"; } @ApiOperation("测试查询") @ApiImplicitParams({ @ApiImplicitParam(name = "name",value = "姓名",required = true,paramType = "query"), @ApiImplicitParam(name = "age",value = "年龄",required = true,paramType = "query",dataType = "Integer"), }) @PostMapping("/search") public String search(String name,Integer age){ return name + ":" + age; } @ApiOperation("测试添加") @PostMapping("/add") public String add(User user){ return user.getName() + ":" + user.getAge(); } @ApiOperation("根据Id获取用户信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path") }) @ApiResponses({ @ApiResponse(code = 500,message = "指定业务的报错信息,返回客户端"), @ApiResponse(code = 400,message = "请求参数没填好"), @ApiResponse(code = 404,message = "请求路径没有或者页面路径不对") }) @GetMapping("/user/{id}") public User load(@PathVariable("id") Integer id){ return new User(id,"jack",22); } }
开发组dev002
@Api(tags = "SwaggerApiDev002Controller测试类-dev002") @RestController public class SwaggerApiDev002Controller { @ApiOperation("测试方法-dev002") @GetMapping("/test2") public String index(){ return "Hello Word Dev002"; } }
user实体
@ApiModel("用户信息实体") public class User { @ApiModelProperty("编号") private Integer id; @ApiModelProperty("姓名") private String name; @ApiModelProperty("年龄") private Integer age; public User(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }6、设置配置信息及分组
@Configuration public class Swagger3Config { @Bean public Docket createRestApi() { Docket docket = new Docket(documentationType.OAS_30); //创建api信息 docket.apiInfo(createApiInfo()); //开关 docket.enable(true); //指定包扫描 docket.select().apis(RequestHandlerSelectors.basePackage("com.qrkj.swaggerdemo.controller.dev001")).build(); //路径扫描 //docket.select().paths(PathSelectors.ant("/**")); docket.groupName("开发组001"); return docket; } @Bean public ApiInfo createApiInfo() { return new ApiInfo("Swagger Demo", "Api documentation", "1.0", "http://127.0.0.1:8089/swagger-ui/", new Contact("liuyc","http://127.0.0.1:8089/swagger-ui/","2524738590@qq.com"), "Apache 2.0", "http://127.0.0.1:8089/swagger-ui/", new ArrayList()); } @Bean public Docket createRestApi2() { Docket docket = new Docket(documentationType.OAS_30); //创建api信息 docket.apiInfo(createApiInfo2()); //开关 docket.enable(true); //指定包扫描 docket.select().apis(RequestHandlerSelectors.basePackage("com.qrkj.swaggerdemo.controller.dev002")).build(); //路径扫描 //docket.select().paths(PathSelectors.ant("/**")); docket.groupName("开发组002"); return docket; } @Bean public ApiInfo createApiInfo2() { return new ApiInfo("Swagger Demo Dev002", "Api documentation", "1.0", "http://127.0.0.1:8089/swagger-ui/", new Contact("liuyc","http://127.0.0.1:8089/swagger-ui/","2524738590@qq.com"), "Apache 2.0", "http://127.0.0.1:8089/swagger-ui/", new ArrayList()); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)