Spring Boot API 自动生成Swagger2使用
1.添加依赖
<!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
2.创建配置类
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.kxg.swagger.controller")) .build().apiInfo(new ApiInfoBuilder() .description("Swagger 文档") .contact(new Contact("塞北一抹云","","") ).version("v1.0.0") .title("API测试文档") .license("Apache2.0") .licenseUrl("http://www.apache.org/licenses/LICEESE-2.0") .build() ); } }
3.开发接口
@RestController @Api(tags="用户数据接口") public class UserController { @ApiOperation(value = "查询用户",notes = "根据ID查询用户") @ApiImplicitParam(paramType = "path",name = "id",value="用户id",required = true) @GetMapping("/user/{id}") public String getUserById(@PathVariable Integer id) { return "/user/"+id; } @ApiResponses({ @ApiResponse(code = 200,message = "删除成功"), @ApiResponse(code = 500,message = "删除失败")}) @ApiOperation(value="删除用户",notes = "通过id删除用户") @DeleteMapping("/user/{id}") public Integer deleteUserByid(@PathVariable Integer id) { return id; } @ApiOperation(value = "添加用户",notes = "添加一个用户,传入用户名和地址") @ApiImplicitParams({ @ApiImplicitParam(paramType = "query",name = "username",value="用户名", required = true,defaultValue = "kxg"), @ApiImplicitParam(paramType = "query",name = "address",value="用户地址", required = true,defaultValue = "bj")}) @ApiResponses({ @ApiResponse(code = 200,message = "添加成功"), @ApiResponse(code = 500,message = "添加失败")}) @PostMapping("/user") public String addUser(@RequestParam String username, @RequestParam String address) { return username +":" +address; } @ApiOperation(value = "修改用户",notes = "修改用户,传入用户信息") @PutMapping("/user") public String UpdateUser(@RequestBody User user) { return user.toString(); } @GetMapping("/ignore") @ApiIgnore public void ignoreMethod() { } @RequestMapping("/") @ApiIgnore public String test() { return "test"; } }
@ApiModel(value = "用户实体类",description = "用户信息描述类") @Component public class User { private Integer id; private String name; private String address; public String getName() { return name; } public Integer getId() { return id; } public String getAddress() { return address; } public void setName(String name) { this.name = name; } public void setId(Integer id) { this.id = id; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "{" + "id=" + id + ", name='" + name + '\'' + ", address='" + address + '\'' + '}'; } }
4.构建项目测试
是与
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency>
产生了冲突,暂时未能解决(先去除)
5.浏览器查看http://localhost:9090/swagger-ui.html
- required:表示字段是否必填
- value:参数的描述信息
- @ApiOperation(value:对方法的作用的一个描述,notes :备注方法的详细作用)
- @Api(tags="用户数据接口") 用在类上,用来描述整个类的 信息
- paramType 是指方法参数 的类型:path(@PathVariable)、query(@RequestParam)、header(@RequestHeader)、body(@RequestBody)、form
- name表述参数名称
- @ApiResponse 对响应结果的描述
- @RequestBody注解接受数据
- @ApiModel 和
- @ApiModelProperty(value="用户名") 注解配置对象的描述信息
- @ApiIgnore 表示不对某个接口生成 文档