SpringBoot整合Swagger自动生成API文档
2017-10-11 15:55
681 查看
swagger用于定义API文档。
好处:
前后端分离开发
API文档非常明确
测试的时候不需要再使用URL输入浏览器的方式来访问Controller
传统的输入URL的测试方式对于post请求的传参比较麻烦(当然,可以使用postman这样的浏览器插件)
spring-boot与swagger的集成简单的一逼
首先,在项目pom中引入依赖,如下,
接着,在SpringBoot中创建Application.java,如下,
说明:
引入了一个注解@EnableSwagger2来启动swagger注解。(启动该注解使得用在controller中的swagger注解生效,覆盖的范围由@ComponentScan的配置来指定,这里默认指定为根路径”com.xxx.firstboot”下的所有controller)
接着,创建UserController.java,如下,
说明:
具体其他的注解,查看:
https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
接着,在浏览器输入”http://localhost:8080/swagger-ui.html“,可以看到如下界面,
最上边一个红框:@Api
GET红框:method=RequestMethod.GET
右边红框:@ApiOperation
parameter红框:@ApiImplicitParams系列注解
response messages红框:@ApiResponses系列注解
输入参数后,点击”try it out!”,查看响应内容:
其他参考:
==http://www.cnblogs.com/java-zhao/p/5348113.html==
https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
http://www.jianshu.com/p/8033ef83a8ed
http://www.cnblogs.com/softidea/p/6251249.html
好处:
前后端分离开发
API文档非常明确
测试的时候不需要再使用URL输入浏览器的方式来访问Controller
传统的输入URL的测试方式对于post请求的传参比较麻烦(当然,可以使用postman这样的浏览器插件)
spring-boot与swagger的集成简单的一逼
首先,在项目pom中引入依赖,如下,
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
接着,在SpringBoot中创建Application.java,如下,
package com.xxx.firstboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication //same as @Configuration+@EnableAutoConfiguration+@ComponentScan @EnableSwagger2 //启动swagger注解 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
说明:
引入了一个注解@EnableSwagger2来启动swagger注解。(启动该注解使得用在controller中的swagger注解生效,覆盖的范围由@ComponentScan的配置来指定,这里默认指定为根路径”com.xxx.firstboot”下的所有controller)
接着,创建UserController.java,如下,
package com.xxx.firstboot.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.xxx.firstboot.domain.User; import com.xxx.firstboot.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; @RestController @RequestMapping("/user") @Api("userController相关api") public class UserController { @Autowired private UserService userService; // @Autowired // private MyRedisTemplate myRedisTemplate; @ApiOperation("获取用户信息") @ApiImplicitParams({ @ApiImplicitParam(paramType="header",name="username",dataType="String",required=true,value="用户的姓名",defaultValue="zhaojigang"), @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="用户的密码",defaultValue="wangna") }) @ApiResponses({ @ApiResponse(code=400,message="请求参数没填好"), @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对") }) @RequestMapping(value="/getUser",method=RequestMethod.GET) public User getUser(@RequestHeader("username") String username, @RequestParam("password") String password) { return userService.getUser(username,password); } // @RequestMapping("/testJedisCluster") // public User testJedisCluster(@RequestParam("username") String username){ // String value = myRedisTemplate.get(MyConstants.USER_FORWARD_CACHE_PREFIX, username); // if(StringUtils.isBlank(value)){ // myRedisTemplate.set(MyConstants.USER_FORWARD_CACHE_PREFIX, username, JSON.toJSONString(getUser())); // return null; // } // return JSON.parseObject(value, User.class); // } }
说明:
具体其他的注解,查看:
https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
接着,在浏览器输入”http://localhost:8080/swagger-ui.html“,可以看到如下界面,
最上边一个红框:@Api
GET红框:method=RequestMethod.GET
右边红框:@ApiOperation
parameter红框:@ApiImplicitParams系列注解
response messages红框:@ApiResponses系列注解
输入参数后,点击”try it out!”,查看响应内容:
其他参考:
==http://www.cnblogs.com/java-zhao/p/5348113.html==
https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
http://www.jianshu.com/p/8033ef83a8ed
http://www.cnblogs.com/softidea/p/6251249.html
相关文章推荐
- Swagger与SpringMVC整合自动生成api(超详细)
- 使用Swagger整合SpringMVC自动生成Restful接口文档
- 用Swashbuckle给ASP.NET Core的项目自动生成Swagger的API帮助文档
- 用Swashbuckle给ASP.NET Core的项目自动生成Swagger的API帮助文档
- SwaggerUI自动生成API文档(SwaggerUI+SpringBoot)
- swagger:API在线文档自动生成框架
- Springboot | SpringBoot 微服务整合Swagger生成API文档
- spring-boot整合swagger生成在线api文档
- Spring Boot如何让Web API自动生成文档,并解决swagger-annotations的API注解description属性废弃的问题
- Swagger自动接口文档生成框架————springboot整合swagger总结
- springmvc集成Swagger自动生成api文档
- Spring MVC 集成 Swagger,API文档自动生成~
- Spring boot结合swagger自动生成api文档
- Spring MVC学习总结(9)——Spring MVC整合swagger自动生成api接口文档
- api文档自动生成 -swagger ui
- 用Swashbuckle给ASP.NET Core的项目自动生成Swagger的API帮助文档
- Spring MVC学习总结(9)——Spring MVC整合swagger自动生成api接口文档
- 使用swagger实现在线api文档自动生成 在线测试api接口
- SpringBoot中使用Swagger生成RESTful规范API文档
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)