Spring Cloud 整合 Swagger2

Posted 沈叶唐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud 整合 Swagger2相关的知识,希望对你有一定的参考价值。

 

详细用法:  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的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot整合Swagger2

Spring Boot之Swagger2集成

SpringBoot之整合Swagger2

Spring Boot整合Swagger2构建RESTful API

SpringBoot2 整合 Swagger2

Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2