1. 程式人生 > >Spring Cloud 整合 Swagger2

Spring Cloud 整合 Swagger2

span import author div https hand ice opera sele

詳細用法: https://www.cnblogs.com/softidea/p/6251249.html

原文鏈接(註意文末):https://blog.csdn.net/ityqing/article/details/81217383

一、引入依賴:

技術分享圖片
<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.5.0</version>
        </dependency>
View Code

註意了註意了知識點!

版本一定要2.5.0以及以上版本! 網上博文大多數引的2.2.2版本的, 這個版本在demo中沒有問題, 但是開發中你肯定會引別的插件.

2.2.2版本的與feign有沖突! 會報bean創建加載異常! 這是個坑不想網友們再遇到同樣的錯誤.

二、Swagger2配置文件類:

技術分享圖片
package com.leyou;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import
springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @ClassName: swagger2配置 * @Description: TODO * @author yeric * @date 2019.01.28 */ @Configuration @EnableSwagger2 public class LyItemSwagger { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.leyou.item.web")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Ly Item Service") .description("--------------------------------") .termsOfServiceUrl("https://blog.csdn.net/ityqing") .contact("yeric") .version("1.0") .build(); } }
View Code

這個類要和啟動類放在同一個目錄下,

技術分享圖片

三、controller代碼:

技術分享圖片
package com.leyou.item.web;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("category")
@Api
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("list")
    @ApiOperation(value = "根據分類id獲得分類列表", notes = "樹列表")
    @ApiImplicitParam(name = "pid", value = "分類id", paramType = "query", required = true, dataType = "Integen")
    public ResponseEntity<List<Category>> queryCategoryListByPid (@RequestParam("pid") Long pid) {
        return ResponseEntity.ok(categoryService.queryCategoryListByPid(pid));
    }
}
View Code

訪問:http://localhost:8080/swagger-ui.html#/:

技術分享圖片

Spring Cloud 整合 Swagger2