这是我第一次来这里。因此,我们在Spring MVC中构建了一个应用程序,它使用2个java类作为控制器来处理REST调用。其中一个用@Controller显式注释,而另一个用@Component注释。 (定义路线的方式略有不同)
我们面临的问题是,尽管我们进行了配置/设置,但我们只看到一个没有端点和文档的空UI。它基本上是一块空白画布。我们进行了大量的搜索,尽管尝试了多种配置的bean和定义,但我们仍然遇到了同样的问题。
最近我们尝试了这个示例:Swagger Springfox Configuration Issue
我们的设置/配置和相关文件的一些片段在下面生成。
控制器类:
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
}
配置文件:
我们的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>
我们的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();
}
}
我们的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/");
}
}
感谢您阅读所有这些内容。如果您想再查看我们的代码/文件或者我是否遗漏了任何内容,请告诉我们。任何意见都表示赞赏!
答案 0 :(得分:0)
你可能已经找到了答案,但如果有人遇到同样的问题,这可能会对他们有所帮助:
我的诀窍是将我SwaggerConfig
的包添加到ComponentScan 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
。