Search code examples
spring-mvcswaggerswagger-uiswagger-2.0

/swagger-resources/configuration/ui 404 error


I'm running in springfox v2.7.0 in my java spring based application. We have 3 dispatch serverlets configured in web.xml:

1)common springmvc servlet [Active]

<servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/config/common/springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>`

2 ) springmvc-v1 servlet [inactive]: which is disabled by commenting out it in web.xml and not in use.

<servlet>
    <servlet-name>springmvc-v1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.test.mywebservices.v1.config.WebConfig
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

3)springmvc-v2 servlet [Active]: for this I am implimenting swagger.

<servlet>
    <servlet-name>springmvc-v2</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.test.mywebservices.v2.config.WebConfig
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc-v2</servlet-name>
    <url-pattern>/v2/*</url-pattern>
</servlet-mapping>

And it works fine, and i can get to the swagger-resources from here: https://localhost:9002/mywebservices/v2/swagger-resources

And i can view the api from here: https://localhost:9002/mywebservices/v2/v2/api-docs

And Swagger-UI is accessible from here: https://localhost:9002/mywebservices/swagger-ui.html

The problem is when i load the https://localhost:9002/mywebservices/swagger-ui.html page it is trying to access the swagger-resources from here: https://localhost:9002/mywebservices/swagger-resources

And they are not available. But it is available at https://localhost:9002/mywebservices/v2/swagger-resources

How can i fix this so that the swagger-ui.html : I think Swagger-ui.html should be available at: https://localhost:9002/mywebservices/v2/swagger-ui.html so that springfox.js will get the right baseUrl.

https://localhost:9002/mywebservices/swagger-ui.html >>> pages loads but gives popup to enter the Base URL

https://localhost:9002/mywebservices/swagger-resources/configuration/ui >>> gives 404 hence popup appears

https://localhost:9002/mywebservices/webjars/springfox-swagger-ui/springfox.js >>> Able to access.This is working fine.

Please suggest the solution. Thanks in advance.


Solution

  • I was facing the same problem.

    The solution was to map on the MVC config of your app, manually the Controller managing the mapping of swagger-resources that exists on swagger-common.jar. The package value is "springfox.documentation.swagger.web". The configuration, depending on your app, should look similar that the one shown below:

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = { ApplicationPackages.REST_CONTROLLER_BASE_PACKAGE,
            "springfox.documentation.swagger.web" })
    public class MvcConfig extends WebMvcConfigurerAdapter {
    
    }