springboot結合swagger自動生成接口文檔


  前后台分離的開發漸漸已成趨勢。那么前后端的溝通就成了問題,包括移動端,web端。如果有一個東西在我們寫完代碼的時候,自動將接口的所有注釋,調用文檔提供出來,是不是一件很美好的事情。那就是使用swagger.

  1.使用swagger,首先在pom中引入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>

2.Application.java中引入@EnableSwagger2來啟動swagger注解

@Configuration
@SpringBootApplication // 組件掃描
@EnableScheduling
@EnableAsync
@EnableSwagger2
public class Application {

3.使用接口注解

@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的屬性

4.啟動服務,瀏覽器輸入"http://localhost:8080/swagger-ui.html"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM