• 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#/:

  • 相关阅读:
    easyui 如何为datagrid添加自定义列属性(如:width,align,editor)
    Oracle中如何修改已存在数据的列名的数据类型
    web 表单方式上传文件方法(不用flash插件)
    easyui datagrid怎么动态获取表头的列名及显示名称
    如何解决“HttpException (0x80004005): 超过了最大请求长度”问题
    oracle中“ORA-00060: 等待资源时检测到死锁” 或存储过程编译卡死 解决方法
    Chosen v1.8.7 动态添加下拉选项
    【转发】vue v-for循环的用法(索引,键值)
    Vue Select默认选择项设置方法
    Vue内部使用setInterval轮询数据,对象数据重新赋值后再次渲染数据
  • 原文地址:https://www.cnblogs.com/yerikm/p/10331988.html
Copyright © 2020-2023  润新知