后台寫接口,由於要提供接口文檔給前台使用,所有研究了一下swagger,看到網上有篇文章寫得不錯,就直接拿過來了。
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:參數的默認值
- paramType:參數放在哪個地方
- @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!",查看響應內容:
本文轉載自:http://www.cnblogs.com/java-zhao/p/5348113.html
