SpringFox Swagger2注解基本用法
一切参数说明,参考官方API文档:http://docs.swagger.io/swagger-core/current/apidocs/index.html?io/swagger/annotations
在实体和API注释这块,有些注释不一定在页面上表现,但是接口返回的数据上全部都有返回。
Swagger2出自SpringFOX, 官网:https://github.com/springfox/springfox,而与https://swagger.io/不同,所以在https://swagger.io/上是找不到Swagger2的开发文档。
在Swagger2的GitHUb页面上也没提供相关的详细文档,不过可以参考示例代码库https://github.com/springfox/springfox-demos进行参考。
不过下面这些网址还能找到关于Swagger的先关API文档,并且兼容Swagger2的:
http://docs.swagger.io/swagger-core/current/apidocs/index.html
https://github.com/swagger-api/swagger-core(旧版示例)
https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X#quick-annotation-overview
https://github.com/swagger-api/swagger-samples(新版示例)
下面是收集的一些常用注解,如果要更多的用法,可以参照上面官网提供的文档:
名称 | 描述 |
---|---|
@Api | 将类标记为Swagger资源。 |
@ApiImplicitParam | 表示API操作中的单个参数。 |
@ApiImplicitParams | 允许多个ApiImplicitParam对象列表的包装器。 |
@ApiModel | 提供有关Swagger型号的其他信息。 |
@ApiModelProperty | 添加和操作模型属性的数据。 |
@ApiOperation | 描述针对特定路径的操作或通常的HTTP方法。 |
@ApiParam | 为操作参数添加额外的元数据。 |
@ApiResponse | 描述操作的可能响应。 |
@ApiResponses | 允许多个ApiResponse对象列表的包装器。 |
@Authorization | 声明在资源或操作上使用授权方案。 |
@AuthorizationScope | 描述OAuth2授权范围。 |
@ResponseHeader | 表示可以作为响应的一部分提供的头。 |
示例代码:
SwaggerConfig
配置
- import com.google.common.base.Predicate;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.ResponseEntity;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
- import static com.google.common.base.Predicates.or;
- import static springfox.documentation.builders.PathSelectors.regex;
- /**
- * SwaggerConfig
- */
- @Configuration
- @EnableSwagger2
- public class SwaggerConfig {
- @Value("${server.servlet-path}")
- private String pathMapping;
- private ApiInfo initApiInfo() {
- ApiInfo apiInfo = new ApiInfo("XXX项目 Platform API",//大标题
- initContextInfo(),//简单的描述
- "1.0.0",//版本
- "服务条款",
- "后台开发团队",//作者
- "The Apache License, Version 2.0",//链接显示文字
- "http://www.baidu.com"//网站链接
- );
- return apiInfo;
- }
- private String initContextInfo() {
- StringBuffer sb = new StringBuffer();
- sb.append("REST API 设计在细节上有很多自己独特的需要注意的技巧,并且对开发人员在构架设计能力上比传统 API 有着更高的要求。")
- .append("<br/>")
- .append("本文通过翔实的叙述和一系列的范例,从整体结构,到局部细节,分析和解读了为了提高易用性和高效性,REST API 设计应该注意哪些问题以及如何解决这些问题。");
- return sb.toString();
- }
- @Bean
- public Docket restfulApi() {
- System.out.println("http://localhost:8080" + pathMapping + "/swagger-ui.html");
- return new Docket(DocumentationType.SWAGGER_2)
- .groupName("RestfulApi")
- // .genericModelSubstitutes(DeferredResult.class)
- .genericModelSubstitutes(ResponseEntity.class)
- .useDefaultResponseMessages(true)
- .forCodeGeneration(false)
- .pathMapping(pathMapping) // base,最终调用接口后会和paths拼接在一起
- .select()
- .paths(doFilteringRules())
- .build()
- .apiInfo(initApiInfo());
- }
- /**
- * 设置过滤规则
- * 这里的过滤规则支持正则匹配
- * @return
- */
- private Predicate<String> doFilteringRules() {
- return or(
- regex("/hello.*"),
- regex("/vehicles.*")
- );
- }
- }
Controller
的配置
- import com.reachauto.hkr.cxn.swagger.bean.ChangeRentalShopParameter;
- import io.swagger.annotations.*;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.*;
- import java.util.Date;
- @Api(value = "API - VehiclesController", description = "车辆模块接口详情")
- @RestController
- @RequestMapping("/vehicles")
- public class VehiclesController {
- private static Logger logger = LoggerFactory.getLogger(VehiclesController.class);
- @ApiOperation(value = "查询车辆接口", notes = "此接口描述xxxxxxxxxxxxx<br/>xxxxxxx<br>值得庆幸的是这儿支持html标签<hr/>", response = String.class)
- @ApiImplicitParams({
- @ApiImplicitParam(name = "vno", value = "车牌", required = false,
- dataType = "string", paramType = "query", defaultValue = "辽A12345"),
- @ApiImplicitParam(name = "page", value = "page", required = false,
- dataType = "Integer", paramType = "query",defaultValue = "1"),
- @ApiImplicitParam(name = "count", value = "count", required = false,
- dataType = "Integer", paramType = "query",defaultValue = "10")
- })
- @ApiResponses(value = {
- @ApiResponse(code = 200, message = "Successful — 请求已完成"),
- @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"),
- @ApiResponse(code = 401, message = "未授权客户机访问数据"),
- @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"),
- @ApiResponse(code = 500, message = "服务器不能完成请求")}
- )
- @ResponseBody
- @RequestMapping(value = "", method = RequestMethod.GET)
- public ModelMap findVehicles(@RequestParam(value = "vno", required = false) String vno,
- @RequestParam(value = "page", required = false) Integer page,
- @RequestParam(value = "count", required = false) Integer count)
- throws Exception {
- logger.info("http://localhost:8501/api/v1/vehicles");
- logger.info("## {},{},{}", vno, page, count);
- logger.info("## 请求时间:{}", new Date());
- ModelMap map = new ModelMap();
- map.addAttribute("vno", vno);
- map.addAttribute("page", page);
- return map;
- }
- @ApiOperation(value = "根据车牌查询车辆", notes = "这种类型的查询是精确查询,其结果只有一条数据", response = String.class)
- @ApiImplicitParams({
- @ApiImplicitParam(name = "vno", value = "车牌", required = false,
- dataType = "string", paramType = "path", defaultValue = "辽A12345")
- })
- @ApiResponses(value = {
- @ApiResponse(code = 200, message = "Successful — 请求已完成"),
- @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"),
- @ApiResponse(code = 401, message = "未授权客户机访问数据"),
- @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"),
- @ApiResponse(code = 500, message = "服务器不能完成请求")}
- )
- @ResponseBody
- @RequestMapping(value = "vno={vno}", method = RequestMethod.GET)
- public ModelMap getVno(@PathVariable(value = "vno") String vno)
- throws Exception {
- logger.info("http://localhost:8501/api/v1/vehicles/vno={}", vno);
- logger.info("## 请求时间:{}", new Date());
- ModelMap map = new ModelMap();
- map.addAttribute("vno", vno);
- return map;
- }
- @ApiOperation(value = "车辆位置查询接口", notes = "根据车牌查询车辆位置信息", response = String.class)
- @ApiImplicitParams({
- @ApiImplicitParam(name = "vno", value = "车牌", required = false,
- dataType = "string", paramType = "path", defaultValue = "辽A12345")
- })
- @ApiResponses(value = {
- @ApiResponse(code = 200, message = "Successful — 请求已完成"),
- @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"),
- @ApiResponse(code = 401, message = "未授权客户机访问数据"),
- @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"),
- @ApiResponse(code = 500, message = "服务器不能完成请求")}
- )
- @ResponseBody
- @RequestMapping(value = "vno={vno}/location", method = RequestMethod.GET)
- public ModelMap getLocation(@PathVariable(value = "vno") String vno)
- throws Exception {
- logger.info("getLocation");
- logger.info("## 请求时间:{}", new Date());
- ModelMap map = new ModelMap();
- map.addAttribute("vno", vno);
- return map;
- }
- @ApiOperation(value = "根据车辆id查询", notes = "精确查询,最常规的方式,支持POST和GET方式", response = String.class)
- @ApiImplicitParams({
- @ApiImplicitParam(name = "id", value = "id", required = false,
- dataType = "string", paramType = "path", defaultValue = "12344444")
- })
- @ApiResponses(value = {
- @ApiResponse(code = 200, message = "Successful — 请求已完成"),
- @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"),
- @ApiResponse(code = 401, message = "未授权客户机访问数据"),
- @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"),
- @ApiResponse(code = 500, message = "服务器不能完成请求")}
- )
- @ResponseBody
- @RequestMapping(value = "{id}", method = {RequestMethod.GET,RequestMethod.POST})
- public ModelMap getById(@PathVariable(value = "id") String id)
- throws Exception {
- logger.info("http://localhost:8501/api/v1/vehicles/{}", id);
- logger.info("## 请求时间:{}", new Date());
- ModelMap map = new ModelMap();
- map.addAttribute("{RequestMethod.GET,RequestMethod.POST}", id);
- return map;
- }
- @ApiOperation(value = "根据车辆id查询", notes = "精确查询,最常规的方式,支持POST和GET方式", response = String.class)
- @ApiImplicitParams({
- @ApiImplicitParam(name = "id", value = "id", required = false,
- dataType = "string", paramType = "path", defaultValue = "12344444")
- })
- @ApiResponses(value = {
- @ApiResponse(code = 200, message = "Successful — 请求已完成"),
- @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"),
- @ApiResponse(code = 403, message = "服务器拒绝请求"),
- @ApiResponse(code = 401, message = "未授权客户机访问数据"),
- @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"),
- @ApiResponse(code = 500, message = "服务器不能完成请求")}
- )
- @ResponseBody
- @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
- public ModelMap delById(@PathVariable(value = "id") String id)
- throws Exception {
- logger.info("http://localhost:8501/api/v1/vehicles/{}", id);
- logger.info("## 请求时间:{}", new Date());
- ModelMap map = new ModelMap();
- map.addAttribute("RequestMethod.DELETE", id);
- return map;
- }
- @ApiOperation(value = "网点挂靠", notes = "嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻", response = String.class)
- @ApiResponses(value = {
- @ApiResponse(code = 200, message = "Successful — 请求已完成"),
- @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"),
- @ApiResponse(code = 401, message = "未授权客户机访问数据"),
- @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"),
- @ApiResponse(code = 500, message = "服务器不能完成请求")}
- )
- @ResponseBody
- @RequestMapping(value = "change_rentalshop", method = {RequestMethod.PUT,RequestMethod.PATCH})
- public ModelMap changeRentalShop(@RequestBody ChangeRentalShopParameter parameter)
- throws Exception {
- logger.info("http://localhost:8501/api/v1/vehicles/change_rentalshop | {}", parameter);
- logger.info("## 请求时间:{}", new Date());
- ModelMap map = new ModelMap();
- map.addAttribute("网点挂靠", new Date());
- return map;
- }
- }
Application
启动模块
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
示例代码输出的效果:
测试工程:https://github.com/easonjim/5_java_example/tree/master/swagger2test/test1
参考:
https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/
https://gumutianqi1.gitbooks.io/specification-doc/content/tools-doc/spring-boot-swagger2-guide.html(以上内容部分转自此篇文章)
http://blog.csdn.net/xupeng874395012/article/details/68946676
http://blog.csdn.net/z28126308/article/details/71126677
http://blog.csdn.net/u014231523/article/details/76522486
http://blog.csdn.net/fanpeng1100/article/details/54016292
SpringFox Swagger2注解基本用法的更多相关文章
- SpringBoot中使用springfox+swagger2书写API文档
随着前后端的分离,借口文档变的尤其重要,springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfox生成的API文 ...
- 使用springfox+swagger2书写API文档(十八)
使用springfox+swagger2书写API文档 springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfo ...
- 浅谈@RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别
浅谈@RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别 Spring 2.5 版本新增了注解功能, 通过注解,代码编写简化了很多:但熟悉注解的使 ...
- MP实战系列(十)之SpringMVC集成SpringFox+Swagger2
该示例基于之前的实战系列,如果公司框架是使用JDK7以上及其Spring+MyBatis+SpringMVC/Spring+MyBatis Plus+SpringMVC可直接参考该实例. 不过建议最好 ...
- springboot swagger2注解使用
swagger2 注解整体说明 @Api:用在请求的类上,表示对类的说明 tags="说明该类的作用,可以在UI界面上看到的注解" value="该参数没什么意义,在UI ...
- Spring的注解@Qualifier用法
Spring的注解@Qualifier用法在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢?根据注入资源的注解不同实现的方式有一点小小的区别 ...
- 全面解析Spring中@ModelAttribute注解的用法
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...
- Hibernate 注解的用法以及说明(二)
注解映射必须满足两大条件:Hibernate3.2以上版本和JSEE 5. @Entity 类注释,所有要持久化的类都要有@Entity public class Org implements ...
- spring的@Transactional注解详细用法
概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下: 为不同的事务API提供一致的编程模型, ...
随机推荐
- cocos creator 场景如何透明,多个canvas层级显示
转载地址:https://forum.cocos.com/t/creator-canvas/55373/14 Creator 版本:1.7 目标平台:WEB MOBILE 项目需要,页面做了多个Can ...
- js易混API汇总
一:slice()方法 ————————————http://www.w3school.com.cn/jsref/jsref_slice_string.asp ———————————————————— ...
- Java 8 (8) 默认方法
传统上,Java程序的接口是将相关方法按照预定组合到一起的方式.实现接口的类必须为接口中定义的方法提供一个实现,或者从父类中集成它的实现.但是,一旦类库的设计者需要更新接口,向接口中加入新的方法时候, ...
- LN : leetcode 406 Queue Reconstruction by Height
lc 406 Queue Reconstruction by Height 406 Queue Reconstruction by Height Suppose you have a random l ...
- es6数值扩展
1. 二进制和八进制表示法 从 ES5 开始,在严格模式之中,八进制就不再允许使用前缀0表示,ES6 进一步明确,要使用前缀0o表示. ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0 ...
- android调用webservice接口获取信息
我的有一篇博客上讲了如何基于CXF搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数. 1.在lib中放入ksoap2的jar包 ...
- px-em-rem单位转换
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- mysql 存储引擎学习
现在我们常用的MySQL存储引擎主要是两种:InnoDB and MyISAM. 1.MyISAM 执行效率高 不支持事务 不支持外键 每个MyISAM在磁盘上存储成3个文件,其中文件名和表名都相同, ...
- JAVA环境变量配置后未变动配置失效处理
环境: Windows 7 x64 配置方案来源于教程: http://www.mamicode.com/info-detail-563355.html 配置方案出现的问题: 正确配置JAVA环境变量 ...
- Java学习2_一些基础2_字符串_16.5.5
接上一次的博客. 不可变字符串: Java中String类没有提供用于修改字符串的方法.如果想将greeting中的“Hello”改为“Help!”需要先提取所需要的的字符,然后再拼接.即 greet ...