AndreaNobili
AndreaNobili

Reputation: 42957

What is wrong in this Spring Boot Swagger configuration to obtain the documentation of my REST API? Why I can't access to the documentation?

I am working on a 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 finding some difficulties to do it.

So basically I have created the following Java configuration class into my Spring Boot project:

@Configuration
@EnableSwagger2
public class Config {

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

I think that this is the minimal configuration to generate the Swagger documentation of all my REST service. I have put this configuration class at the same level of the Application class (the class that contains the main() method that start my project). I think that the Config class is ok because I have tryed to put a brack point into the api() method and I can see that it enter in this method at the project startup so I think that this configuration is loaded.

Then in the previous tutorial it say that to verify that the my REST API Swagger documentation is generated I have to perform a GET request to this URL: http://localhost:8080/spring-security-rest/api/v2/api-docs

I think that this URL is related to the example project and not to my project.

So I tried to use: http://localhost:8080/api-docs

But doing in this way I am obtaining this error message:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jan 14 16:05:23 CET 2017
There was an unexpected error (type=Not Found, status=404).
No message available

And in the IDE console I have this message:

[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:865)] [http-nio-8080-exec-4] DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/swagger-ui.html]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:310)] [http-nio-8080-exec-4] RequestMappingHandlerMapping - Looking up handler method for path /swagger-ui.html
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:320)] [http-nio-8080-exec-4] RequestMappingHandlerMapping - Did not find handler method for [/swagger-ui.html]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.lookupHandler(AbstractUrlHandlerMapping.java:190)] [http-nio-8080-exec-4] SimpleUrlHandlerMapping - Matching patterns for request [/swagger-ui.html] are [/**]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.lookupHandler(AbstractUrlHandlerMapping.java:219)] [http-nio-8080-exec-4] SimpleUrlHandlerMapping - URI Template variables for request [/swagger-ui.html] are {}
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.getHandlerInternal(AbstractUrlHandlerMapping.java:140)] [http-nio-8080-exec-4] SimpleUrlHandlerMapping - Mapping [/swagger-ui.html] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@4e671ef]]] and 1 interceptor
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:951)] [http-nio-8080-exec-4] DispatcherServlet - Last-Modified value for [/swagger-ui.html] is: -1
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1044)] [http-nio-8080-exec-4] DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1000)] [http-nio-8080-exec-4] DispatcherServlet - Successfully completed request
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:865)] [http-nio-8080-exec-4] DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:310)] [http-nio-8080-exec-4] RequestMappingHandlerMapping - Looking up handler method for path /error
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:317)] [http-nio-8080-exec-4] RequestMappingHandlerMapping - Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:951)] [http-nio-8080-exec-4] DispatcherServlet - Last-Modified value for [/error] is: -1
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.view.ContentNegotiatingViewResolver.getMediaTypes(ContentNegotiatingViewResolver.java:263)] [http-nio-8080-exec-4] ContentNegotiatingViewResolver - Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.view.BeanNameViewResolver.resolveViewName(BeanNameViewResolver.java:74)] [http-nio-8080-exec-4] BeanNameViewResolver - No matching bean found for view name 'error.html'
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.view.ContentNegotiatingViewResolver.getBestView(ContentNegotiatingViewResolver.java:338)] [http-nio-8080-exec-4] ContentNegotiatingViewResolver - Returning [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@50e8ed74] based on requested media type 'text/html'
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1251)] [http-nio-8080-exec-4] DispatcherServlet - Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@50e8ed74] in DispatcherServlet with name 'dispatcherServlet'
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1000)] [http-nio-8080-exec-4] DispatcherServlet - Successfully completed request

The thing that seems me strange is that I have configured nothing related to the URL for my documentation (I don't know if there is a standard URL).

Some further details that maybe could are important to find a solution.

My REST API are implemented in Spring Boot by Controller class that handles request like this:

package com.myapp.controller.room;

@RestController
@RequestMapping("/Room")
public class RoomController {

    private static final Logger log = LoggerFactory.getLogger(RoomController.class);

    @Autowired
    private RoomService roomService;

    @Autowired
    private RoomMediaService roomMediaService;

    public RoomController(){
        log.debug("RoomController init");
    }

    /**
     * Ritorna la tipologia di stanza associata ad una stanza
     * @param id dellla stanza di cui si intende reperire le informazioni relative alla sua tipologia
     * @return RoomTipologyDTO contenente le informazioni relative alla tipologia di stanza
     * @throws DataAccessException
     */
    @RequestMapping(value = "/{id}/RoomTipology",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<RoomTipologyDTO> getRoomTipologyByRoomId(@PathVariable Long id) throws DataAccessException{

        log.debug("getRoomTipologyByRoomId START");

        RoomTipologyDTO result = roomService.getRoomTipologyByRoom(id);

        log.debug("getRoomTipologyByRoomId END");

        return ResponseEntity.ok(result);
    }

    ....................................................................
    ....................................................................
    ....................................................................
}

This is the Application class that contains the main() method that start my application:

@SpringBootApplication
@EntityScan("com.betrivius.domain")
@ComponentScan(lazyInit = true)
@EnableAutoConfiguration
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args)  {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        //context.close();

        log.info("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = context.getBeanDefinitionNames();
        Arrays.sort(beanNames);

        log.info("_______________________________________________________");
        for (String beanName : beanNames) {
            log.info(beanName);
        }

        log.info("main() END");
    }
}

An example of an URL of one o my service is something like this:

http://localhost:8080/RoomRate/1/RoomRateOptionList

So what is the correct URL to generate my Swagger documentation? Or what am I missing? How can I fix this issue?

Upvotes: 2

Views: 5489

Answers (1)

Lindani Masinga
Lindani Masinga

Reputation: 76

Try accessing swagger docs on http://localhost:8080/v2/api-docs. it should work. i think you are using version 2 of swagger documentation.

Upvotes: 6

Related Questions