What is the correct way to configure Swagger2 with Spring MVC, not spring boot.
Currently I have following components added
Maven Dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!--Dependency for swagger ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
In dispatcher servlet context
<bean id="swagger2Config" class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration"/>
<mvc:resources location="/resources/" mapping="/resources/**"
order="1" />
<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html" />
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**" />
<mvc:default-servlet-handler />
and added a configuration
@EnableSwagger2
public class SwaggerConfiguration extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
My application context is say sample, when I tried to launch the application
http://localhost:8080/sample/swagger-ui.html,
I'm getting error
Unable to infer base url. This is common when using dynamic servlet
registration or when the API is behind an API Gateway. The base url is the
root of where all the swagger resources are served. For e.g. if the api is
available at http://example.org/api/v2/api-docs then the base url is
http://example.org/api/. Please enter the location manually:
If I tried http://localhost:8080/sample/v2/api-docs/
I'm getting exception
Handler processing failed; nested exception is java.lang.NoSuchMethodError
on org.springframework.web.util.UriComponentsBuilder.fromHttpRequest
Am I missing anything else here to make it work.
By the way, I'm using Spring 3.2.9 and Servlet API 2.5
Thank you all