此篇博客转发自:http://www.cnblogs.com/java-zhao/p/5348113.html

swagger用于定义API文档。

好处:

  • 前后端分离开发
  • API文档非常明确
  • 测试的时候不需要再使用URL输入浏览器的方式来访问Controller
  • 传统的输入URL的测试方式对于post请求的传参比较麻烦(当然,可以使用postman这样的浏览器插件)
  • spring-boot与swagger的集成简单的一逼

1、项目结构

和上一节一样,没有改变。

2、pom.xml

引入了两个jar。

        <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>

3、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)

4、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);
// } }

说明:

  • @Api:用在类上,说明该类的作用
  • @ApiOperation:用在方法上,说明方法的作用
  • @ApiImplicitParams:用在方法上包含一组参数说明
  • @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
    • paramType:参数放在哪个地方

      • header-->请求参数的获取:@RequestHeader
      • query-->请求参数的获取:@RequestParam
      • path(用于restful接口)-->请求参数的获取:@PathVariable
      • body(不常用)
      • form(不常用)
    • name:参数名
    • dataType:参数类型
    • required:参数是否必须传
    • value:参数的意思
    • defaultValue:参数的默认值
  • @ApiResponses:用于表示一组响应
  • @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
    • code:数字,例如400
    • message:信息,例如"请求参数没填好"
    • response:抛出异常的类
  • @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
    • @ApiModelProperty:描述一个model的属性

以上这些就是最常用的几个注解了。

需要注意的是:

  • ApiImplicitParam这个注解不只是注解,还会影响运行期的程序,例子如下:

  

如果ApiImplicitParam中的phone的paramType是query的话,是无法注入到rest路径中的,而且如果是path的话,是不需要配置ApiImplicitParam的,即使配置了,其中的value="手机号"也不会在swagger-ui展示出来。

具体其他的注解,查看:

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!",查看响应内容:

第四章 springboot + swagger(转载)的更多相关文章

  1. 第四章 springboot + swagger

    http://www.cnblogs.com/java-zhao/p/5348113.html

  2. 第五章 springboot + mybatis(转载)

    本编博客转发自:http://www.cnblogs.com/java-zhao/p/5350021.html springboot集成了springJDBC与JPA,但是没有集成mybatis,所以 ...

  3. 第七章 springboot + retrofit(转载)

    本篇博客转发自:http://www.cnblogs.com/java-zhao/p/5350861.html retrofit:一套RESTful架构的Android(Java)客户端实现. 好处: ...

  4. 第六章 springboot + 事务(转载)

    本篇博客转发自:http://www.cnblogs.com/java-zhao/p/5350106.html 在实际开发中,其实很少会用到事务,一般情况下事务用的比较多的是在金钱计算方面. myba ...

  5. 第三章 springboot + jedisCluster(转载)

    本编博客转发自:http://www.cnblogs.com/java-zhao/p/5347703.html 如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制. 如果使用的是r ...

  6. 第二十四章 springboot注入servlet

    问:有了springMVC,为什么还要用servlet?有了servlet3的注解,为什么还要使用ServletRegistrationBean注入的方式? 使用场景:在有些场景下,比如我们要使用hy ...

  7. 第十四章 springboot + profile(不同环境读取不同配置)

    具体做法: 不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中:prod环境下的配置配置在application-prod.prope ...

  8. 第二十五章 springboot + hystrixdashboard

    注意: hystrix基本使用:第十九章 springboot + hystrix(1) hystrix计数原理:附6 hystrix metrics and monitor 一.hystrixdas ...

  9. [Effective Java]第四章 类和接口

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

随机推荐

  1. 【ORM】--FluentNHibernate之AutoMapping详解

           上篇文章详细讨论了FluentNHibernate的基本映射的使用方法,它的映射基本用法是跟NHibernate完全一样的,首先要创建数据库链接配置文件,然后编写Table的Mappin ...

  2. jQuery学习笔记(在js中增加、删除及定位控件的操作)

    代码内容很多都是从amazeui直接copy过来的,先声明,请不要说在下抄袭- - <!-------------------- HTML代码 ----------------------> ...

  3. python 功能代码安全高效写法

    一. with 链接地址:https://www.ibm.com/developerworks/cn/opensource/os-cn-pythonwith/

  4. 面试iOS遇到这种笔试《操作评估》

    一.开发团队需求: 开发一款软件需要前端后台和推广的人.1,首先要明确设计这个APP的理念2,合理的列出APP的需求3,找到后台人员让他们搭好后台数据4,前端的人负责展示到界面上5,推广人员负责让更多 ...

  5. java网络流传输,中文乱码问题。

    最近需要从某个网页上抓取数据.一波三折. 1. 先要找到网站页面调用后台数据服务的url地址,但是本人对js不了解,花了不少时间在分析其网页源代码的js部分,试图寻找出调用数据的链接. 后来得知浏览器 ...

  6. ubuntu客户端使用RDP协议连接windows服务器

    如果服务器是linux或unix,可以使用ssh远程连接服务器.也有服务器是windows的,通常使用RDP协议进行连接. 1  环境 客户端:ubuntu14.04 LST 服务器:windows ...

  7. modelsim(3) - summary ,issue,tips

    1) the OEM of modelsim is 10 times slower than offical questa 2)how to the file full path in the mod ...

  8. SCN

    SCN 一.SCN的引入 众所周知,当oracle实例崩溃时,oracle通过检查点队列使用CKPT进程,周期性的将LRBA记入控制文件,以记录读取REDO LOG的范围.确定范围之后,oracle首 ...

  9. bash coding to changeNames

    ____通配符和正则表达式 此处的定义只针对linux 中的shell语言,对其它语言不适用 _正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配.grep.awk.sed等命令可以支持正则表 ...

  10. WEB测试方法及注意地方

    1页面部分(1) 页面清单是否完整(是否已经将所需要的页面全部都列出来了)(2) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示)(3) 页面在窗口中的显示是否正确.美观( ...