gpt4 book ai didi

java - Springfox Swagger 配置和文档未被捕获

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:46 24 4
gpt4 key购买 nike

这是我第一次来这里。因此,我们在 Spring MVC 中构建了一个应用程序,使用 2 个 Java 类作为 Controller 来处理 REST 调用。其中一个用 @Controller 显式注释,而另一个用 @Component 注释。 (定义路线的两种略有不同的方法)

我们面临的问题是,尽管我们进行了配置/设置,但我们只看到一个空的 UI,没有端点和文档填写。它基本上是一 block 空白的 Canvas 。我们进行了大量的搜索,尽管尝试了 bean 和定义的多种配置,但我们仍然遇到了同样的问题。

最近我们尝试了这个示例:Swagger Springfox Configuration Issue

下面生成了我们设置/配置和相关文件的一些片段。

Controller 类:

用户设备身份验证 Controller

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

配置文件:

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>

我们的 web.xml 包含 servlet 和 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>

定义 Docket Bean 的 SwaggerConfig 类:

@EnableSwagger2
public class SwaggerConfig {

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

}

我们的 WebMvcConfig 类包含 ViewController 和 ResourceHandler:

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

}

感谢您阅读所有内容。如果您想查看更多我们的代码/文件或者我是否遗漏了任何内容,请告诉我。任何意见都将受到赞赏!

最佳答案

您可能已经找到了答案,但如果有人遇到同样的问题,这可能会对他们有所帮助:

对我来说,诀窍是将包含 SwaggerConfig 的包添加到 ComponentScan basePackages 列表中。基本上我有相同的 SwaggerConfig 但我的 Application 文件(我使用的是 Spring boot)如下所示:

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

重要的部分是 ComponentScan 中的 "my.test.config" 包 - 其中定义了 SwaggerConfig

关于java - Springfox Swagger 配置和文档未被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com