我正在开发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-文档
我认为这个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 by Controller类中实现,它处理这样的请求:
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 ResponseEntitygetRoomTipologyByRoomId(@PathVariable Long id) throws DataAccessException{ log.debug("getRoomTipologyByRoomId START"); RoomTipologyDTO result = roomService.getRoomTipologyByRoom(id); log.debug("getRoomTipologyByRoomId END"); return ResponseEntity.ok(result); } .................................................................... .................................................................... .................................................................... }
这是Application类,它包含启动我的应用程序的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是什么?或者我错过了什么?我该如何解决这个问题?
尝试访问http:// localhost:8080/v2/api-docs上的swagger 文档.它应该工作.我认为你正在使用swagger文档的第2版.