2017-06-24 56 views
1

這是我第一次在這裏。所以我們有一個使用Spring MVC構建的應用程序來處理帶有2個Java類作爲控制器的REST調用。其中一個用@Controller顯式註釋,另一個用@Component註釋。 (2個稍微不同的方式來定義我們的路線)Springfox Swagger配置和文檔不被捕獲

我們面臨的問題是,儘管我們的配置/設置,我們只看到一個空的用戶界面,沒有端點和文件填寫。它基本上是一張空白的畫布。我們進行了大量的搜索,儘管嘗試了多個bean和定義的配置,但我們仍遇到同樣的問題。

最近,我們嘗試了這個例子:Swagger Springfox Configuration Issue

我們的一些設置/配置和相關文件的下面產生片段。

控制器(S)類:

UserDeviceAuthenticationController

@RestController 
public class UserDeviceAuthenticationController { 

private static final Logger LOGGER = Logger 
     .getLogger(UserDeviceAuthenticationController.class); 

/** 
* 
* @param response 
* @param request 
* @param deviceID: a misnomer. This field is different for every client set up on Fitbit 
* @throws IOException 
* @throws ParseException 
*/ 
@RequestMapping(value = "/fitbitEndPoint", method = RequestMethod.POST) 
public void fitbitEndPoint(HttpServletResponse response, 
     //Body logic redacted 

} 

PHDDeviceRestService

@Path("device/v2.0") 
@Component 
public class PHDDeviceRestService extends BaseServiceImpl { 

    @POST 
    @Path("/{deviceId}/url") 
    @Produces({ MediaType.APPLICATION_JSON }) 
    @Consumes({ MediaType.APPLICATION_JSON }) 
    public Response getDeviceURL(GetDeviceURLRequest getDeviceURLRequest, @PathParam("deviceId") Long deviceId) throws PHDWebServiceException { 
     //Logic redacted 
    } 

配置文件:

我們的相關揚鞭Springfox依賴於我們的pom.xml

<!-- Swagger dependency --> 
<dependencies> 
    <dependency> 
     <groupId>io.springfox</groupId> 
     <artifactId>springfox-swagger2</artifactId> 
     <version>2.6.0</version> 
    </dependency> 
    <dependency> 
     <groupId>io.springfox</groupId> 
     <artifactId>springfox-swagger-ui</artifactId> 
     <version>2.6.0</version> 
    </dependency> 
</dependencies> 

我們含有servlet和URL模式的web.xml:其中案卷豆被定義

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 

<servlet> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/mvc/webmvc-config.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<!-- Map all *.spring requests to the DispatcherServlet for handling --> 
<servlet-mapping> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <url-pattern>/devices/*</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <url-pattern>/healthyweight/*</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <url-pattern>/DevicesGateway/*</url-pattern> 
</servlet-mapping> 
<servlet> 
    <servlet-name>jersey</servlet-name> 
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> 
    <init-param> 
    <param-name>com.sun.jersey.config.property.packages</param-name> 
    <param-value>com.uhg.phd.rest;com.uhg.phd.rest.MongoDB</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet> 
    <servlet-name>CXFSevlet</servlet-name> 
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>CXFSevlet</servlet-name> 
    <url-pattern>/services/*</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
    <servlet-name>CXFSevlet</servlet-name> 
    <url-pattern>/ws/*</url-pattern> 
</servlet-mapping> 
<!-- <servlet-mapping> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> --> 
<servlet-mapping> 
    <servlet-name>CXFSevlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
<!-- <servlet-mapping> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <url-pattern>/swagger-ui.html</url-pattern> 
</servlet-mapping> --> 
</web-app> 

我們SwaggerConfig類:

@EnableSwagger2 
public class SwaggerConfig { 

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

} 

我們與ViewControllers和ResourceHandlers WebMvcConfig類:

@EnableWebMvc 
public class WebMvcConfig extends WebMvcConfigurerAdapter { 

@Override 
public void addViewControllers(ViewControllerRegistry registry) { 
    registry.addRedirectViewController("/documentation/v2/api-docs", "/v2/api-docs?group=restful-api"); 
    registry.addRedirectViewController("/documentation/swagger-resources/configuration/ui","/swagger-resources/configuration/ui"); 
    registry.addRedirectViewController("/documentation/swagger-resources/configuration/security","/swagger-resources/configuration/security"); 
    registry.addRedirectViewController("/documentation/swagger-resources", "/swagger-resources"); 
} 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("documentation/swagger-ui.html") 
    .addResourceLocations("classpath:/META-INF/resources/"); 

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

} 

謝謝你閱讀所有這些。讓我知道你是否希望看到我們的代碼/文件,或者是否遺漏了任何東西。任何輸入是讚賞!

回答

0

你可能找到答案了,不過如果有人有相同的問題,也許這可以幫助他們:

什麼做的把戲對我來說是增加中,我有我的SwaggerConfigComponentScan basePackages列表中的包。基本上我有同樣的SwaggerConfig但我Application文件(我使用Spring啓動)看起來是這樣的:

@Configuration 
@EnableAutoConfiguration 
@ComponentScan(basePackages = { 
     "my.test.config", 
     "my.test.controllers", 
     "my.test.models"}) 
public class SwaggerTestApplication { 

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

的重要組成部分,作爲"my.test.config"包在ComponentScan - 其中SwaggerConfig定義。

+0

嗨都鐸王朝。感謝您的建議。我們在Spring MVC中,而不是Spring引導。無論如何,我們定義了一個web.xml文件,該文件指向一個webmvc-config.xml和applicationContext.xml文件。 在這些文件中,「」已被設置和定義。基礎包包含所有的類和java文件,包括我已經定義了SwaggerConfig的地方。例如,SwaggerConfig文件屬於com。uhg.phd.Configuration並且應該已經被包含性地掃描了。 XML沒有像直接內聯Java那樣強健? – JWU1000

+0

xml和在Java中定義它們一樣健壯 - 所以這不是問題。 如果你還沒有,你可能想看看在官方存儲庫中的例子: https://github.com/springfox/springfox-demos/tree/master/spring-xml-swagger 並使確定你的SwaggerConfig被找到了(從示例中): <! - 包含一個swagger配置 - > ...這就是我現在能想到的就是這些。 – tudor