2015-08-25 29 views
10

Tôi có một ứng dụng khởi động mùa xuân mà tôi đang chạy bằng cách sử dụng máy chủ tomcat nhúng. Tôi là một phần thành công trong việc springfox-swagger tích hợp với các ứng dụng. Nếu tôi thực hiện /v2/api-docs, tôi có thể xem tất cả tài liệu về tất cả các api trong webapp. Tuy nhiên, khi tôi đang cố gắng truy cập tương tự từ giao diện người dùng, nó không hoạt động. Dưới đây là kết quả chi tiết.Không thể mở swagger-ui từ ứng dụng khởi động mùa xuân

Sản lượng - localhost: 8080/api/vênh vang-nguồn

[ { 
    "name" : "default", 
    "location" : "/v2/api-docs", 
    "swaggerVersion" : "2.0" 
} ] 

Sản lượng - localhost: 8080/api/v2/api-docs

I get valid results. I can confirm that and the output is too large to paste here 

Nhưng khi tôi cố gắng truy cập vào swagger-ui, nó không hoạt động. Dưới đây là các URL khác nhau của tôi gọi để truy cập vào swagger-ui.

http://localhost:8080/swagger-ui.html - UI is loading, but no documentation of API's is present 
http://localhost:8080/api/swagger-ui.html - 404 Not Found 
http://localhost:8080/springfox - 404 Not Found 
http://localhost:8080/api/springfox - 404 Not Found 

Dưới đây là lớp SwaggerConfig.java tôi

package com.vmware.vrack.lcm; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import springfox.documentation.builders.RequestHandlerSelectors; 
import springfox.documentation.service.ApiInfo; 
import springfox.documentation.spi.DocumentationType; 
import springfox.documentation.spring.web.plugins.Docket; 
import springfox.documentation.swagger.web.UiConfiguration; 
import springfox.documentation.swagger2.annotations.EnableSwagger2; 

import static springfox.documentation.builders.PathSelectors.regex; 

@Configuration 
@EnableSwagger2 
public class SwaggerConfig { 

    @Bean 
    public Docket api() { 
     return new Docket(DocumentationType.SWAGGER_2) 
       .select() 
       .apis(RequestHandlerSelectors.any()) 
       .paths(regex("/.*")) 
       .build() 
       .apiInfo(apiInfo()); 
    } 

    private ApiInfo apiInfo() { 
     ApiInfo apiInfo = new ApiInfo(
       "My Project's REST API", 
       "This is a description of your API.", 
       "version-1", 
       "API TOS", 
       "[email protected]", 
       "API License", 
       "API License URL" 
     ); 
     return apiInfo; 
    } 

} 

Dưới đây là các phụ thuộc vênh vang tôi đang sử dụng

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

Dưới đây là công cụ chuyển đổi tập tin nhắn webconfig

@Configuration 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
     converters.add(jackson2Converter()); 
    } 

    @Bean 
    public MappingJackson2HttpMessageConverter jackson2Converter() { 
     MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 
     converter.setObjectMapper(objectMapper()); 
     return converter; 
    } 

    @Bean 
    public ObjectMapper objectMapper() { 
     ObjectMapper objectMapper = new ObjectMapper(); 
     objectMapper.enable(SerializationFeature.INDENT_OUTPUT); 
     return objectMapper; 
    } 
} 

Liên kết dưới đây ys rằng @EnableWebMvc không nên được sử dụng trong một ứng dụng web khởi động vào mùa xuân và việc sử dụng chú thích có thể gây ra các vấn đề trong việc nâng cấp lên. Nhưng, nếu tôi không sử dụng các chú thích, các ứng dụng web được không đến (i đã dán lỗi stack trace dưới đây)

http://springfox.github.io/springfox/docs/current/#configuring-the-objectmapper

Lỗi dấu vết khi tôi không sử dụng các @EnableWebMvc Chú thích

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/Users/ngorijala/.m2/repository/io/springfox/springfox-spring-web/2.2.2/springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [springfox.documentation.spi.service.RequestHandlerProvider]: : Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/Users/ngorijala/.m2/repository/io/springfox/springfox-spring-web/2.2.2/springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.List]: : No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] found for dependency [collection of org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] found for dependency [collection of org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/Users/ngorijala/.m2/repository/io/springfox/springfox-spring-web/2.2.2/springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.List]: : No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] found for dependency [collection of org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] found for dependency [collection of org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) 
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:799) 
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:499) 
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:790) 
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:337) 
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1343) 
    at org.eclipse.jetty.maven.plugin.JettyWebAppContext.startWebapp(JettyWebAppContext.java:296) 
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1336) 
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:742) 
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:499) 
    at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:365) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) 
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61) 
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:163) 

Tôi có cảm giác rằng tôi đang thiếu thứ gì đó tầm thường. Ai đó có thể vui lòng xem và cho tôi biết những gì tôi đang thiếu. ?? Cảm ơn trước.!!

Trả lời

5

springfox-swagger-ui là một jar web và yêu cầu bạn thiết lập trình xử lý tài nguyên để thông báo cho servlet dispatch cách thức và tài nguyên nào cần phân phối khi bạn yêu cầu ../swagger-ui.html. Thông thường trong một ứng dụng khởi động mùa xuân auto-configuration sẽ đảm bảo thiết lập nó cho bạn. Lý do không tải trong trường hợp của bạn là vì bạn đã báo hiệu khởi động mùa xuân rằng ứng dụng sẽ được định cấu hình thủ công qua kết hợp WebMvcConfigurerAdapter/@EnableWebMvc.

Bạn sẽ có thể đặt chú thích @SpringBootApplication vào cấu hình chính mùa xuân của mình và loại bỏ lớp học WebConfig tất cả cùng nhau.

WebConfig của bạn không thêm bất kỳ giá trị nào ngoài việc đảm bảo JSON được thụt vào, nên tôi khuyên bạn nên loại bỏ tất cả cùng nhau và thay thế bằng một hạt Jackson2ObjectMapperBuilder thay thế.

Ví dụ về cách làm điều tương tự trong spring-mvc/spring-boot, v.v. hãy xem springfox-demos project. Cụ thể, hãy xem SpringConfig để xem cách định cấu hình thủ công các trình xử lý tài nguyên.

+0

này đã làm việc cho tôi. Chúc mừng – Yonkee

9

Nếu bạn muốn giữ @EnableWebMvc chú thích bất kỳ cách nào, bạn phải thêm dòng sau

@Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 

      registry.addResourceHandler("swagger-ui.html") 
        .addResourceLocations("classpath:/META-INF/resources/"); 

      registry.addResourceHandler("/webjars/**") 
        .addResourceLocations("classpath:/META-INF/resources/webjars/"); 

    } 
+0

+1 để giải quyết vấn đề của tôi. Cảm ơn bạn. Cũng lưu ý rằng phương thức này đi vào bên trong lớp mở rộng WebMvcConfigurerAdapter. – idungotnosn

Các vấn đề liên quan