首页 > 其他分享 >swagger2 踩坑记录

swagger2 踩坑记录

时间:2022-11-11 11:01:19浏览次数:62  
标签:swagger 记录 swagger2 ui registry io springfox


1. swagger2 是用​​Springfox Swagger2​​导入的

swagger2 最新版本只到  2.9.2,后面2.10 以上都是swagger3了 

如果要使用 swagger3  ,可以不用springfox 了,用什么,你再找一下

 

2. swagger2 添加完注解就完事了?

不是,默认不能直接访问 ***/swagger-ui.html 页面依旧是空白

增加个配置,映射静态文件,并且注意Interceptor 需要过滤一下url

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {

/**
* 访问静态资源
* */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* SpringBoot自动配置本身并不会把/swagger-ui.html
* 这个路径映射到对应的目录META-INF/resources/下面
* 采用WebMvcConfigurerAdapter将swagger的静态文件进行发布;
*/
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
//将所有/static/** 访问都映射到classpath:/static/ 目录下
registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");

super.addResourceHandlers(registry);
}

/**
* @desc com.mt.spring.user.web.i18n.config。I18nConfig.java 冲突迁移到此
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MessageResourceInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/swagger-resources/**",
"/webjars/**","/v2/**","/swagger-ui.html/**"
,"/api-docs.html","/favicon.ico");
}

}

spring-boot 版本比较高需要用 

public class Swagger2Config implements WebMvcConfigurer {

3. swagger2 可以访问了?还是有些报错

Illegal DefaultValue null for parameter type integer`和`NumberFormatException: For input string: ""

从上面这句可以看出,有个默认值是空字符串的变量转换成Integer类型时异常。

at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]

根据上面这句报错信息,点进去AbstractSerializableParameter.java:412可以看到

if(BaseIntegerProperty.TYPE.equals(type)){

    return Long.valueOf(example);

}

就是说如果实体属性类型是Integer,就把example转为Long类型,而example默认为"",导致转换错误

我是用的是更改依赖

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger_v}</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger_v}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.22</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>

也可以参考其他的:

​​javascript:void(0)​​

 

还会有其他问题:

持续出现重复报错:

No mapping found for HTTP request with URI [/null/swagger-resources/configur

很多

很多

关掉之前访问 ***/swagger-ui.html 的页面,等app启动完再开启

 

 

springfox 相关文档

​http://springfox.github.io/springfox/docs/current/​

​http://springfox.github.io/springfox/docs/snapshot/​

 

建议还是用 swagger3 吧~~~

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:swagger,记录,swagger2,ui,registry,io,springfox
From: https://blog.51cto.com/u_2776699/5843269

相关文章