Spring Boot Swagger配置有什么问题需要获取REST API的文档?为什么我无法访问文档?

时间:2017-01-14 15:17:48

标签: spring spring-boot swagger springfox

我正在开发 Spring Boot 应用程序,我正在尝试配置Swagger以自动生成我的REST服务文档。

我正在关注本教程:http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

但我发现有些困难。

所以基本上我已经在我的 Spring Boot 项目中创建了以下Java配置类:

@Configuration
@EnableSwagger2
public class Config {

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

我认为这是生成所有REST服务的Swagger文档的最小配置。我已将此配置类放在 Application 类(包含启动项目的 main()方法的类的同一级别)。我认为 Config 类是可以的,因为我已经尝试将一个括号点放入 api()方法中,我可以看到它在项目中输入此方法启动,所以我认为这个配置已加载。

然后在上一个教程中,它说要验证我的REST API Swagger文档是否已生成,我必须对此URL执行GET请求:http://localhost:8080/spring-security-rest/api/v2/api-docs

我认为此URL与示例项目有关,而与我的项目无关。

所以我尝试使用: http://localhost:8080/api-docs

但是这样做我得到了这个错误信息:

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

在IDE控制台中,我收到了以下消息:

[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

我觉得奇怪的是,我已经配置了与我的文档的URL无关的任何内容(我不知道是否有标准的URL)。

可能对寻找解决方案很重要的一些进一步细节。

我的 REST API 控制器类在 Spring Boot 中实现,该类处理这样的请求:

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);
    }

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

这是应用程序类,其中包含启动我的应用程序的 main()方法:

@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");
    }
}

我的服务之一的URL示例如下:

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

那么生成Swagger文档的正确URL是什么?或者我错过了什么?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

尝试在http://localhost:8080/v2/api-docs上访问swagger文档。它应该工作。我认为你正在使用swagger文档的第2版。