springfox: springfox-swagger-ui cannot find swagger-resources when run outside of spring boot
Hi,
I’m running in springfox v2.2.2 inside JBoss. My dispatch serverlet looks like this:
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/restservices/*</url-pattern>
</servlet-mapping>
And it works fine, and i can get to the swagger-resources from here: http://localhost:8080/rootContext/restservices/swagger-resources
And i can view the api from here: http://localhost:8080/rootContext/restservices/v2/api-docs
And Swagger-UI is accessable from here: http://localhost:8080/rootContext/swagger-ui.html
The problem is when i load the http://localhost:8080/rootContext/swagger-ui.html page it is trying to access the swagger-resources from here:
http://localhost:8080/rootContext/swagger-resources
And they are not available. How can i fix this so that the swagger-ui.html either:
- looks in the right location (http://localhost:8080/rootContext/restservices/swagger-resources) or
- Swagger-ui.html is available at: http://localhost:8080/rootContext/restservices/swagger-ui.html
Thanks.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 34 (11 by maintainers)
Dear @shollander your solution is not working, here is the correct one:
registry.addResourceHandler("**/**").addResourceLocations("classpath:/META-INF/resources/");For those who use XML based configuration 😃
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/> <mvc:resources mapping="/swagger-ui.html" location="classpath:/META-INF/resources/swagger-ui.html"/>@davidluckystar I had the same issue and I was able to get it working by mapping swagger-ui.html to the servlet path like this:
Then you should be able to access swagger at http://localhost:8080/services/swagger-ui.html
Will gladly inform you of the findings once I get it work, have spend countless hours, and will start to look into your code…
I am having a variation of this issue using swagger 2.7.0.
Update, after a bunch of debugging I realized that having my SwaggerConfig in its own Spring profile causes this problem. I had this at the top of my SwaggerConfig class:
@Profile(“swagger”)
The reason for this is that the presence of swagger classes causes some of our unit tests to fail, so I wanted to put the swagger configuration in its own profile and exclude that from the unit tests.
However, this causes the problem described at the top of this ticket where swagger-ui.html works but /swagger-resources does not, but only when deployed in Tomcat (it works when run via bootRun). I suspect that this is either a sequencing problem in Spring initialization or some incompatibility between Swagger and Spring.
I’d like to leave this comment here in case anyone else runs into this problem if that’s ok.
I figured out that adding
<mvc:default-servlet-handler/>then i don’t have to add the:swagger启动后,老是报错下面的url无法打开swagger-ui.html, 提示内部url无法打开swagger-resources/configuration/ui
原因是: 启用了组件扫描导致:@ComponentScan(basePackages = {“com.haizhi.dao”,“com.haizhi.controll”,“com.haizhi.service”}) 这个问题真是费解。
解决办法:注释掉扫描注解即可: //@componentscan(basePackages = {“com.haizhi.dao”,“com.haizhi.controll”,“com.haizhi.service”})
Got it working for old servlets with the appropriate annotations