Spring Boot API 自動生成Swagger2使用

1.添加依賴

<!-- swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2.創建配置類

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.kxg.swagger.controller"))
                .build().apiInfo(new ApiInfoBuilder()
                        .description("Swagger 文檔")
                        .contact(new Contact("塞北一抹雲","","")
                        ).version("v1.0.0")
                        .title("API測試文檔")
                        .license("Apache2.0")
                        .licenseUrl("http://www.apache.org/licenses/LICEESE-2.0")
                        .build()
                );
    }
}

 

3.開發接口

@RestController
@Api(tags="用戶數據接口")
public class UserController {


    @ApiOperation(value = "查詢用戶",notes = "根據ID查詢用戶")
    @ApiImplicitParam(paramType = "path",name = "id",value="用戶id",required = true)
    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable Integer id)
    {
        return "/user/"+id;
    }

    @ApiResponses({
            @ApiResponse(code = 200,message = "刪除成功"),
            @ApiResponse(code = 500,message = "刪除失敗")})
    @ApiOperation(value="刪除用戶",notes = "通過id刪除用戶")
    @DeleteMapping("/user/{id}")
    public Integer deleteUserByid(@PathVariable Integer id)
    {
        return id;
    }

    @ApiOperation(value = "添加用戶",notes = "添加一個用戶,傳入用戶名和地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "username",value="用戶名",
                              required = true,defaultValue = "kxg"),
            @ApiImplicitParam(paramType = "query",name = "address",value="用戶地址",
                              required = true,defaultValue = "bj")})
    @ApiResponses({
            @ApiResponse(code = 200,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失敗")})
    @PostMapping("/user")
    public String addUser(@RequestParam  String  username, @RequestParam String address)
    {
        return username +":" +address;
    }

    @ApiOperation(value = "修改用戶",notes = "修改用戶,傳入用戶信息")
    @PutMapping("/user")
    public String UpdateUser(@RequestBody User user)
    {
        return user.toString();
    }

    @GetMapping("/ignore")
    @ApiIgnore
    public void ignoreMethod()
    {

    }

    @RequestMapping("/")
    @ApiIgnore
    public String test()
    {
        return "test";
    }
}
@ApiModel(value = "用戶實體類",description = "用戶信息描述類")
@Component
public class User {
    private Integer id;
    private String  name;
    private String  address;

    public String getName() {
        return name;
    }

    public Integer getId() {
        return id;
    }

    public String getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

4.構建項目測試

是與

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

產生了衝突,暫時未能解決(先去除)

5.瀏覽器查看http://localhost:9090/swagger-ui.html

  • required:表示字段是否必填
  • value:參數的描述信息
  • @ApiOperation(value:對方法的作用的一個描述,notes :備註方法的詳細作用)
  • @Api(tags="用戶數據接口") 用在類上,用來描述整個類的 信息 
  • paramType 是指方法參數 的類型:path(@PathVariable)、query(@RequestParam)、header(@RequestHeader)、body(@RequestBody)、form
  • name表述參數名稱
  • @ApiResponse 對響應結果的描述
  • @RequestBody註解接受數據
  • @ApiModel 和
  • @ApiModelProperty(value="用戶名") 註解配置對象的描述信息
  • @ApiIgnore 表示不對某個接口生成 文檔

 

發佈了269 篇原創文章 · 獲贊 39 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章