1

I followed the instructions on the following page to add Swagger to my Spring MVC application http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

But I am not able to get Swagger to work. Whenever I hit the following URL: http://localhost:8080/demoApp/swagger-ui.html

I get the following error:

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.springframework.web.util.UriComponentsBuilder.fromHttpRequest(Lorg/springframework/http/HttpRequest;)Lorg/springframework/web/util/UriComponentsBuilder;
    org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1287)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

I tried looking up many articles and also tried the xml configuration instead of the annotation one but no use. There is a similar question on SO that says there might be an issue with Spring version and upgrading to Spring 4 and above solves the problem but I am already using spring version 4.1.4

Here is my pom.xml

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

Here is Configuration:

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

This is how I have registered the swagger resources with the application

<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
Nick Div
  • 5,338
  • 12
  • 65
  • 127
  • can you please post your pom file? – hovanessyan Feb 16 '18 at 08:04
  • Share your controller class and swagger-config class – Yogi Feb 16 '18 at 08:56
  • @hovanessyan Hello, I have updated the question with pom.xml – Nick Div Feb 16 '18 at 09:13
  • @Yogi Hello, I have added the config class to the question but not sure which controller are you asking me to post. – Nick Div Feb 16 '18 at 09:14
  • @NickDiv As your application is not springBoot application, have you registered Swagger's resources? – hovanessyan Feb 16 '18 at 09:18
  • Yes I did that. Let me add that piece of config as well. – Nick Div Feb 16 '18 at 09:20
  • ok. When you start your application, do you see in the output messages from Spring mvc EndpointHandlerMapping related to swagger? (e.g. /swagger-resources/ , /swagger-resources/configuration/ui) ? What is your application root? Do you have any default api prefixes configured by default? – hovanessyan Feb 16 '18 at 09:28
  • No, I do not see any Swagger end-points in the logs. But I am able to reach the Swagger UI. Just dont see the API calls listed. The page opens with stacktrace in the body. the root of the app is demoApp – Nick Div Feb 16 '18 at 09:32
  • I see - have you tried to clean and rebuild your project? (also it it's not a problem you can delete your local .m2 and build from the command line) – hovanessyan Feb 16 '18 at 09:35
  • Yup. I tried that as well :( – Nick Div Feb 16 '18 at 09:44
  • If you are using Spring Security, use the security configuration mentioned on **[Cannot open Swagger UI in tis Version 3 in my Spring Boot Example](https://stackoverflow.com/questions/73073519/cannot-open-swagger-ui-in-tis-version-3-in-my-spring-boot-example)** page. – Murat Yıldız Jan 31 '23 at 10:18

2 Answers2

3

Your spring-web dependency is version 4.1.4, where UriComponentsBuilder does not have the method swagger-ui is trying to invoke:

org.springframework.web.util.UriComponentsBuilder.fromHttpRequest(Lorg/springframework/http/HttpRequest;)

You need to bump your Spring version to higher one (e.g. 4.3.5+). where this method is availble.

hovanessyan
  • 30,580
  • 6
  • 55
  • 83
0

It is not necessary to use the annotations @EnableSwagger2

you have an easier way to add swagger.

you can find the solution by check my answer in the below link.

https://stackoverflow.com/a/48206971/4374472

Here you will have to add some dependency in pom.xml and a maven plugin , which will do the job.

Sandeep Kamath
  • 670
  • 9
  • 15