1. 程式人生 > >SpringBoot整合Swagger自動生成API文件

SpringBoot整合Swagger自動生成API文件

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 @[email protected][email protected] @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);
//    }

}

說明:

image

具體其他的註解,檢視:

image

最上邊一個紅框:@Api

GET紅框:method=RequestMethod.GET

右邊紅框:@ApiOperation

parameter紅框:@ApiImplicitParams系列註解

response messages紅框:@ApiResponses系列註解

輸入引數後,點選”try it out!”,檢視響應內容:

image

其他參考: