swagger用於定義API文檔。
好處: 前後端分離開發 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