启动类已配置config文件和@EnableSwagger2
config也已经配置
但是打开是
打开http://localhost:8089/v2/api-docs
接口返回正常
如果去掉
@ComponentScan({"com.example"})
能打开页面但是看不到接口
springboot整合swagger建议参考:
你真的了解 Swagger 文档吗?
https://www.52interview.com/book/25/261
前端请求/swagger-resources/configuration/ui返回406错误,也就是消息头无法接收(Not acceptable),结合后台Could not find acceptable representation的提示,定位到请求返回的格式错误
原因:启动类中配置MVC内容的处理策略,当请求没有后缀时默认是html格式,而/swagger-resources/configuration/ui实际返回的是json,导致解析错误
@Bean
public WebMvcConfigurer webConfg() {
return new WebMvcConfigurer() {
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).favorParameter(false).ignoreAcceptHeader(true).useRegisteredExtensionsOnly(false)
.defaultContentType(MediaType.TEXT_HTML).mediaType("html", MediaType.TEXT_HTML).mediaType("json", MediaType.APPLICATION_JSON);
}
};
}
解决方案:将默认格式改为json即可,defaultContentType(MediaType.APPLICATION_JSON);
你的com.example.controller包下有定义类吗