第四章 springboot + swagger,springbootswagger

來源:互聯網
上載者:User

第四章 springboot + swagger,springbootswagger

註:本文參考自

http://www.jianshu.com/p/0465a2b837d2

 

swagger用於定義API文檔。

好處:

  • 前後端分離開發
  • API文檔非常明確
  • 測試的時候不需要再使用URL輸入瀏覽器的方式來訪問Controller
  • 傳統的輸入URL的測試方式對於post請求的傳參比較麻煩(當然,可以使用postman這樣的瀏覽器外掛程式)
  • spring-boot與swagger的整合簡單的一逼

1、項目結構

和上一節一樣,沒有改變。

2、pom.xml

引入了兩個jar。

1 <dependency> 2 <groupId>io.springfox</groupId> 3 <artifactId>springfox-swagger2</artifactId> 4 <version>2.2.2</version> 5 </dependency> 6 <dependency> 7 <groupId>io.springfox</groupId> 8 <artifactId>springfox-swagger-ui</artifactId> 9 <version>2.2.2</version>10 </dependency>View Code

3、Application.java

1 package com.xxx.firstboot; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 import springfox.documentation.swagger2.annotations.EnableSwagger2; 7 8 @SpringBootApplication //same as @Configuration+@EnableAutoConfiguration+@ComponentScan 9 @EnableSwagger2 //啟動swagger註解10 public class Application {11 12 public static void main(String[] args) {13 SpringApplication.run(Application.class, args);14 }15 16 }View Code

說明:

  • 引入了一個註解@EnableSwagger2來啟動swagger註解。(啟動該註解使得用在controller中的swagger註解生效,覆蓋的範圍由@ComponentScan的配置來指定,這裡預設指定為根路徑"com.xxx.firstboot"下的所有controller)

4、UserController.java

1 package com.xxx.firstboot.web; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.RequestHeader; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 import org.springframework.web.bind.annotation.RequestParam; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import com.xxx.firstboot.domain.User;11 import com.xxx.firstboot.service.UserService;12 13 import io.swagger.annotations.Api;14 import io.swagger.annotations.ApiImplicitParam;15 import io.swagger.annotations.ApiImplicitParams;16 import io.swagger.annotations.ApiOperation;17 import io.swagger.annotations.ApiResponse;18 import io.swagger.annotations.ApiResponses;19 20 @RestController21 @RequestMapping("/user")22 @Api("userController相關api")23 public class UserController {24 25 @Autowired26 private UserService userService;27 28 // @Autowired29 // private MyRedisTemplate myRedisTemplate;30 31 @ApiOperation("擷取使用者資訊")32 @ApiImplicitParams({33 @ApiImplicitParam(paramType="header",name="username",dataType="String",required=true,value="使用者的姓名",defaultValue="zhaojigang"),34 @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="使用者的密碼",defaultValue="wangna")35 })36 @ApiResponses({37 @ApiResponse(code=400,message="請求參數沒填好"),38 @ApiResponse(code=404,message="請求路徑沒有或頁面跳轉路徑不對")39 })40 @RequestMapping(value="/getUser",method=RequestMethod.GET)41 public User getUser(@RequestHeader("username") String username, @RequestParam("password") String password) {42 return userService.getUser(username,password);43 }44 45 // @RequestMapping("/testJedisCluster")46 // public User testJedisCluster(@RequestParam("username") String username){47 // String value = myRedisTemplate.get(MyConstants.USER_FORWARD_CACHE_PREFIX, username);48 // if(StringUtils.isBlank(value)){49 // myRedisTemplate.set(MyConstants.USER_FORWARD_CACHE_PREFIX, username, JSON.toJSONString(getUser()));50 // return null;51 // }52 // return JSON.parseObject(value, User.class);53 // }54 55 }View Code

說明:

  • @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:拋出異常的類

以上這些就是最常用的幾個註解了。

具體其他的註解,查看:

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!",查看響應內容:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.