zilcuanu
zilcuanu

Reputation: 3715

swagger2 not working with Spring REST

I have a spring application whose web.xml looks like below:

<servlet>
  <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/app-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>

Inside my app-servlet.xml I have the below config entry:

<context:component-scan base-package="com.rest.apidoc" />

I have my SwaggerConfig inside the com.rest.apidoc directory

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

Now when I try to run the app http://domain:port/app/service/api-docs, I get the error on my console No mapping found for HTTP request with URI [/app/service/api-docs] in DispatcherServlet with name 'app'

Where am I going wrong?

Upvotes: 0

Views: 458

Answers (1)

Ilya Zinkovich
Ilya Zinkovich

Reputation: 4430

There is few things that may go wrong:

  1. you don't register appropriate resource handlers. example
  2. you may hit the wrong url. "app" is only a servlet name, doesn't need to appear in url. servlet-mapping
  3. if you've registered resource handlers correctly and included springfox-swagger-ui dependency in your project, you need to request "http://domain:port/service/swagger-ui.html" to see API documented with swagger.

Refer to the following project to check whether you've configured your application correctly: Spring Security for REST Example Project

Upvotes: 1

Related Questions