zoukankan      html  css  js  c++  java
  • SpringBoot集成Swagger2,3分钟轻松入手!

    一、引入maven

            <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.9.2版本,该版本有个小坑后面会提到

    二、创建一个Swagger配置类

    @Configuration
    @EnableSwagger2//开启Swagger2的自动配置
    @Profile({"dev", "pred"})
    public class SwaggerConfig {
    @Bean
    public Docket peopleDeptApi() {
    	return new Docket(DocumentationType.SWAGGER_2)
    			.apiInfo(apiInfo("人才库", "人才库-人员,部门接口文档", "1.0"))
    			.select()
    			.apis(RequestHandlerSelectors.basePackage("com.allqj.platform_base_organization.base.controller"))
    			.paths(PathSelectors.any())
    			.build()
    			.groupName("人员,部门API");
    }
    
    	@Bean
    	public Docket brokerApi() {
    		return new Docket(DocumentationType.SWAGGER_2)
    				.apiInfo(apiInfo("人才库", "人才库-经纪人接口文档", "1.0"))
    				.select()
    				.apis(RequestHandlerSelectors.basePackage("com.allqj.platform_base_organization.broker.controller"))
    				.paths(PathSelectors.any())
    				.build()
    				.groupName("经纪人API");
    	}
    	@Bean
    	public Docket dictionaryApi() {
    		return new Docket(DocumentationType.SWAGGER_2)
    				.apiInfo(apiInfo("人才库", "人才库-字典接口文档", "1.0"))
    				.select()
    				.apis(RequestHandlerSelectors.basePackage("com.allqj.platform_base_organization.dictionary.controller"))
    				.paths(PathSelectors.any())
    				.build()
    				.groupName("字典API");
    	}
    	private ApiInfo apiInfo(String title, String description, String version) {
    		return new ApiInfoBuilder().title(title).description(description).version(version).build();
    	}
    }
    

    三、防止中文分组乱码

    在application.yml文件里配置

    spring:
      http:
        encoding:
          charset: UTF-8
          force: true
          enabled: true
    server:
      tomcat:
        uri-encoding: UTF-8
    

    如果还是读取不到,清除浏览器缓存

    四、分组配置就好了,访问项目路径/swagger-ui.html就ok了

    五、修复 swaggerfox 升级 2.9.2版本问题

    开始的时候我有提到:整合Swagger新版本中,有一个小坑。这里具体说明下,当我们使用版本2.9.2时候,如果项目实体中有Integer类型的属性,当我们打开Api文档的时候会出现一个警告信息:

    java.lang.NumberFormatException: For input string: ""
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_221]
    	at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_221]
    	at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_221]
    	at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
    	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
    	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:687) [jackson-databind-2.9.7.jar:2.9.7]
    	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) [jackson-databind-2.9.7.jar:2.9.7]
    
    解决方案

    排除springfox-swagger2 引入的 swagger-models 1.5.20版本,手动引入1.5.21版本的jar。具体原因可以查看如下链接,这篇文章说的很详细!

    https://blog.csdn.net/qq122516902/article/details/89673363

    pom.xml配置如下

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
                <exclusions>
                    <exclusion>
                        <artifactId>swagger-models</artifactId>
                        <groupId>io.swagger</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--手动引入 swagger-models 的 1.5.21 版本解决java.lang.NumberFormatException: For input string: "" 错误-->
            <dependency>
                <artifactId>swagger-models</artifactId>
                <groupId>io.swagger</groupId>
                <version>1.5.21</version>
            </dependency>
    
  • 相关阅读:
    typescript-定义静态类型和对象类型
    typescript-全局安装
    Cube-ui的使用
    mobx-react
    Calatan Number
    快速排序
    Boyer-Moore
    优先级队列
    PointNet环境搭建
    h5py库
  • 原文地址:https://www.cnblogs.com/xzy-/p/13549221.html
Copyright © 2011-2022 走看看