springboot集成swagger2
swagger2有哪些注解
作用范围 | API | 使用位置 |
---|---|---|
对象属性 | @ApiModelProperty | 用在出入参数对象的字段上 |
协议集描述 | @Api | 用于controller类上 |
协议描述 | @ApiOperation | 用在controller的方法上 |
Response集 | @ApiResponses | 用在controller的方法上 |
Response | @ApiResponse | 用在 @ApiResponses里边 |
非对象参数集 | @ApiImplicitParams | 用在controller的方法上 |
非对象参数描述 | @ApiImplicitParam | 用在@ApiImplicitParams的方法里边 |
描述返回对象的意义 | @ApiModel | 用在返回对象类上 |
注解的使用
@ApiModelProperty-----------------------------name:属性名 value:属性描述
1 @ApiModelProperty(name = "userNo",value = "用户编号") 2 private int userNo; //用户编号
@ApiModel---------------------------------------description :model对象描述
1 @ApiModel(description = "用户信息") 2 public class User {}
@ApiOperation
1 @RequestMapping(value = "addUserForPost",method = RequestMethod.POST,headers = "Content-Type=application/x-www-form-urlencoded") 2 @ApiOperation(value = "表单形式添加用户") 3 public Map addUser(HttpServletRequest request,String userName, Integer userAge){ 4 5 Enumeration<String> names = request.getHeaderNames(); 6 while (names.hasMoreElements()){ 7 String name = names.nextElement(); 8 9 System.out.println(name+"==="+request.getHeader(name)); 10 } 11 System.out.println("userName="+userName+",userAge="+userAge); 12 RESPONSE.put("code",200); 13 RESPONSE.put("msg","添加成功"); 14 15 return RESPONSE; 16 }
@Api
1 @RestController 2 @Api(tags = "UserController | 测试swagger注解的控制器") 3 @RequestMapping(value = "/user") 4 public class UserController {}
@ApiImplicitParams 和@ApiImplicitParam组合使用
1 @RequestMapping(value = "/getUser",method = RequestMethod.GET) 2 @ApiOperation(value = "获取用户信息") 3 @ApiImplicitParams({ 4 @ApiImplicitParam(name = "id",value = "用户编号",paramType = "query"), 5 @ApiImplicitParam(name = "name",value = "用户名称",paramType = "query") 6 }) 7 public User getUser(@RequestParam(required = false) int id,@RequestParam(required = false) String name){ 8 User user = new User(); 9 user.setUserNo(id); 10 user.setAge(12); 11 user.setUserName("lisi"); 12 return user; 13 }
@ApiImplicitParam属性值说明
name:属性值
value:属性描述
paramtype:参数类型
paramtype:
a、path---------请求参数放在请求路径上 /a/{id},get请求
1 @RequestMapping(value = "/modifyUser/{userNo}",method = RequestMethod.GET) 2 @ApiOperation("修改用户信息") 3 @ApiImplicitParams({ 4 @ApiImplicitParam(name = "userNo",value = "用户编号",paramType = "path") 5 }) 6 public Map updUser(@PathVariable(required = true) int userNo){ 7 Map map = new HashMap(); 8 map.put("code",200); 9 map.put("msg","修改成功"); 10 return map; 11 }
b、query------请求参数拼接在请求url上 url?a=1&b=2,get请求
1 @RequestMapping(value = "/getUser",method = RequestMethod.GET) 2 @ApiOperation(value = "获取用户信息") 3 @ApiImplicitParams({ 4 @ApiImplicitParam(name = "id",value = "用户编号",paramType = "query"), 5 @ApiImplicitParam(name = "name",value = "用户名称",paramType = "query") 6 }) 7 public User getUser(@RequestParam(required = false) int id,@RequestParam(required = false) String name){ 8 User user = new User(); 9 user.setUserNo(id); 10 user.setAge(12); 11 user.setUserName("lisi"); 12 return user; 13 }
c、form -----请求参数以表单形式提交,必须是post请求,请求头必须指定headers="Content-Type=application/x-www-form-urlencoded"
1 @RequestMapping(value = "addUserForPost",method = RequestMethod.POST,headers = "Content-Type=application/x-www-form-urlencoded") 2 @ApiOperation(value = "表单形式添加用户") 3 @ApiImplicitParams({ 4 @ApiImplicitParam(name = "userName",value = "用户名称",paramType = "form",required = true), 5 @ApiImplicitParam(name = "userAge",value = "用户年龄",paramType = "form",required = true) 6 }) 7 public Map addUser(HttpServletRequest request,String userName, Integer userAge){ 8 9 Enumeration<String> names = request.getHeaderNames(); 10 while (names.hasMoreElements()){ 11 String name = names.nextElement(); 12 13 System.out.println(name+"==="+request.getHeader(name)); 14 } 15 System.out.println("userName="+userName+",userAge="+userAge); 16 RESPONSE.put("code",200); 17 RESPONSE.put("msg","添加成功"); 18 19 return RESPONSE; 20 }
d、body -----------请求参数在流里,即请求参数以json格式传递,post请求
1 @RequestMapping(value = "addUser",method = RequestMethod.POST) 2 @ApiOperation(value = "添加用户信息") 3 @ApiImplicitParams({ 4 @ApiImplicitParam(name = "user",value = "用户信息",dataType = "User",paramType = "body"), 5 }) 6 public Map addUser(@RequestBody User user){ 7 System.out.println(user.toString()); 8 Map map = new HashMap(); 9 map.put("code",200); 10 map.put("msg","添加成功"); 11 return map; 12 }
文件上传----------请求头必须指定header="Content-Type=multipart/form-data",post请求
1 @RequestMapping(value = "/upload",headers = "Content-type=multipart/form-data",method = RequestMethod.POST) 2 @ApiOperation(value = "文件上传") 3 @ApiImplicitParams({ 4 @ApiImplicitParam(name="userName",value = "用户名称",paramType = "query"), 5 @ApiImplicitParam(name = "age",value = "用户年龄",paramType = "query") 6 }) 7 public void upload(@ApiParam(name = "file",value = "上传文件") MultipartFile file,String userName,Integer age){ 8 9 System.out.println(file.getOriginalFilename()); 10 System.out.println(userName); 11 System.out.println(age); 12 }
springboot如何与swagger2集成
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
1、在启动类上加上@EnableSwagger2注解
2、自定义一个swagger2配置类
1 @Configuration 2 public class Sawgger2 { 3 4 @Bean 5 public Docket createRestApi(){ 6 7 return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()). 8 select().apis(RequestHandlerSelectors.basePackage("com.swagger.springbootswagger.controller")) 9 .paths(PathSelectors.any()).build(); 10 } 11 12 @Bean 13 public ApiInfo apiInfo(){ 14 return new ApiInfoBuilder().title("Spring boot中使用Swagger2构建RESTful APIs").version("1.0").build(); 15 } 16 }
3、配置是否开启swagger
配置文件自定义一个属性
spring.swagger2.enabled=true
在swagger配置类上加上 @ConditionalOnProperty(name = "spring.swagger2.enabled",havingValue = "true")注解
4、对swagger页面进行增强
1)maven引入如下jar包
<!-- swagger2 增强UI ,拥有好看的界面, 和接口分组,排序等功能,如不引用可自行删除--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.8.7</version> </dependency>
2)swagger配置类上加上 @EnableSwaggerBootstrapUI 注解
3)访问页面由swagger-ui.html变成doc.html