0

I am working on a simple HelloWorld Spring Boot application and I am trying to configure Swagger to automatically generate my REST service documentation.

I am following this tutorial: http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

But I am facing some difficulties.

I've added the following Swagger dependencies to the maven pom.xml file:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

and also the Java configuration class SwaggerConfig as follows:

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

According to the guide, this should be sufficient to view the generated documentation on http://localhost:8080/v2/api-docs However, I am getting "Whitespace Error Page: This application has no explicit mapping for /error, so you are seeing this as a fallback."

I am getting the same error for the Swagger UI page at http://localhost:8080/greeting/api/swagger-ui.html too.

For extra information, my HelloWorld code is as follows:

package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

and the greeting controller is

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

Everything is under main/java

I would really appreciate it if anyone has any ideas/suggestions on how to fix this! Thank you in advance :)

sharxx
  • 1
  • 1
  • Compare your project with one from baeldung https://github.com/eugenp/tutorials/tree/master/spring-security-rest – uli Jul 13 '19 at 21:33
  • 2
    correct URL is http://localhost:8080/swagger-ui.html if you have not changed the context path – Romil Patel Jul 14 '19 at 06:04
  • http://localhost:8080/swagger-ui.html returns "Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway." – sharxx Jul 14 '19 at 18:42
  • Does this answer your question? [Added Springfox Swagger-UI and it's not working, what am I missing?](https://stackoverflow.com/questions/46151540/added-springfox-swagger-ui-and-its-not-working-what-am-i-missing) – Ravi Parekh Nov 11 '20 at 19:54

0 Answers0