0

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

DBreaker
  • 319
  • 1
  • 4
  • 24

2 Answers2

0

It seems your @EnableSwagger2 annotation is not getting scanned during startup. Try and use the component scanning in your dispatcher servlet xml file. Something like below,

<context:component-scan base-package="your.package.name" />

Pass the package where the SwaggerConfiguration class is present. That should solve the issue.

WebNoob
  • 237
  • 6
  • 16
  • Actually based on https://stackoverflow.com/questions/33518672/swagger-version-for-spring-3-and-spring-4 and https://github.com/springfox/springfox/blob/v1.0.2/README.md, Spring 3.x is not compatible with Swagger 2. So I ended up using Swagger 1. I'm following the example given at https://github.com/martypitt/swagger-springmvc-example. I will update , once I successfully integrate Swagger 1 – DBreaker Oct 30 '18 at 02:42
  • Yeah, my example worked because I was using Spring version 4.3.5 which is compatible with Swagger 2. – WebNoob Oct 30 '18 at 07:20
0

Actually based on stackoverflow.com/questions/33518672/… and github.com/springfox/springfox/blob/v1.0.2/README.md, Spring 3.x is not compatible with Swagger 2. So I ended up using Swagger 1. I'm following the example given at github.com/martypitt/swagger-springmvc-example. I will update , once I successfully integrate Swagger 1

DBreaker
  • 319
  • 1
  • 4
  • 24