简体   繁体   English

没有捕获Springfox Swagger配置和文档

[英]Springfox Swagger Configuration and Documentation not being captured

This is my first time on here. 这是我第一次来这里。 So we have an application built in Spring MVC handling REST calls with 2 java classes serving as the controller(s). 因此,我们在Spring MVC中构建了一个应用程序,它使用2个java类作为控制器来处理REST调用。 One of them is explicitly annotated with @Controller while the other is annotated with @Component. 其中一个用@Controller显式注释,而另一个用@Component注释。 (2 slightly different ways to defining our routes) (定义路线的方式略有不同)

The issue we are facing is that despite our configuration/setup, we are only seeing an empty UI with no endpoints and documentation filled out. 我们面临的问题是,尽管我们进行了配置/设置,但我们只看到一个没有端点和文档填充的空UI。 It's basically a blank canvas. 它基本上是一块空白画布。 We did a good amount of searching and despite trying out multiple configurations of beans and definitions, we were still running into the same issue. 我们进行了大量的搜索,尽管尝试了多种配置的bean和定义,但我们仍然遇到了同样的问题。

Most recently we tried out this example : Swagger Springfox Configuration Issue 最近我们尝试了这个例子: Swagger Springfox配置问题

Some snippets of our setup/configuration and relevant files are produced below. 我们的设置/配置和相关文件的一些片段在下面生成。

Controller(s) classes: 控制器类:

UserDeviceAuthenticationController 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 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
    }

Configuration Files : 配置文件:

Our relevant Swagger Springfox Dependencies in our Pom.xml 我们的Pom.xml中的相关Swagger Springfox依赖项

<!-- 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>

Our web.xml containing the servlets and url patterns: 我们的web.xml包含servlets和url模式:

<?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>

Our SwaggerConfig class where Docket Bean is defined : 我们的SwaggerConfig类定义了Docket Bean:

@EnableSwagger2
public class SwaggerConfig {

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

}

Our WebMvcConfig class with ViewControllers and ResourceHandlers : 我们的WebMvcConfig类与ViewControllers和ResourceHandlers:

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

}

Thank you for reading through all of this. 感谢您阅读所有这些内容。 Let me know if you would like to see anymore of our code/files or if I'm missing anything. 如果您想要查看我们的代码/文件或者我错过了什么,请告诉我。 Any input is appreciated! 任何输入都表示赞赏!

You probably found an answer already but if somebody has the same problem maybe this helps them: 您可能已经找到了答案,但如果有人遇到同样的问题,这可能会对他们有所帮助:

What did the trick for me was to add the package in which I have my SwaggerConfig to the list of ComponentScan basePackages . 我的诀窍是将我的SwaggerConfig包添加到ComponentScan basePackages列表中。 Basically I have the same SwaggerConfig but my Application file(I'm using Spring boot) looks like this: 基本上我有相同的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);
    }
}

The important part being the "my.test.config" package in the ComponentScan - where the SwaggerConfig is defined. 重要的部分是ComponentScan"my.test.config"包 - 其中定义了SwaggerConfig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swagger Springfox配置问题 - Swagger Springfox Configuration Issue Swagger2:无法自省 Class [springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration] - Swagger2: Failed to introspect Class [springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration] 无法处理配置类的导入候选 [springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration] - Failed to process import candidates for configuration class [springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration] 如何使用 Springfox 从 Swagger 文档中隐藏端点 - How to hide endpoints from Swagger documentation with Springfox 如何从application.properties设置springfox.documentation.swagger.v2.path - how to set the springfox.documentation.swagger.v2.path from application.properties 设置 springfox.documentation.enabled=false 不会禁用 swagger 3.0 - Setting springfox.documentation.enabled=false doesn't disable swagger 3.0 如何在生成Swagger文档时让Springfox序列化jaxb注释模型 - How to get Springfox to serialize jaxb annotated models when generating Swagger documentation @ApiParam无法与springfox swagger 2一起使用 - @ApiParam is not working with springfox swagger 2 Springfox swagger 继承支持 - Springfox swagger inheritance support Springfox挥舞着引发StackOverflowError - Springfox swagger throws StackOverflowError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM