我正在开发一个应该启用 swagger-ui 的 Spring Boot 应用程序。
访问时http://localhost:8080/swagger-ui.html http://localhost:8080/swagger-ui.html有一个错误弹出:
“无法推断基本网址...”
此外,http://localhost:8080/v2/api-docs http://localhost:8080/v2/api-docs显示:
第 1 行第 1 列错误:文档为空
该页面的源代码是 json,但请求内容类型为 application/xhtml+xml;charset=UTF-8
造成这种情况的原因似乎是我的自定义 Jackson 配置:
@Configuration
public class JacksonConfig {
@Bean
public MappingJackson2XmlHttpMessageConverter mappingJackson2XmlHttpMessageConverter() {
return new MappingJackson2XmlHttpMessageConverter(objectMapper());
}
@Bean
public ObjectMapper objectMapper() {
JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
XmlMapper objectMapper = new XmlMapper(xmlModule);
objectMapper
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
objectMapper
.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false)
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return objectMapper;
}
}
具有以下依赖关系:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
该问题也在这里描述:https://github.com/springfox/springfox/issues/1835 https://github.com/springfox/springfox/issues/1835
所以我的问题是:如何指定 jackson 消息转换器的优先级以使 swagger-ui 工作?
我只是在重新阅读自己的问题时偶然发现了解决方案。
只需将其添加到上面的 JacksonConfig 类中即可(不知道排序是否重要,但它有效)。
@Bean
public MappingJackson2HttpMessageConverter jsonConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)