我正在努力将 Springfox 2.2.2 集成到我的 Spring MVC 项目中,但没有生成我认为应该生成的 api 文档。下面是一些有关我的配置的信息。
我提供了以下依赖项(以及其他库,如 fastxml、webjars、使用正确版本的 spring 等)
<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>
Springfox配置如下:
package com.exemplarypackage.config;
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.exemplarypackage.controller")
public class SwaggerConfig extends WebMvcConfigurerAdapter{
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo(
"My Project's REST API",
"This is a description of your API.",
"API TOS",
"url",
"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e58880a5928d809780938097cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>",
"API License",
"API License URL");
return apiInfo;
}
@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/");
}
}
示例性 Controller 如下所示:
package com.exemplarypackage.controller;
@Api(value = "test class for springfox")
@Controller
public class TestController {
@ApiOperation(value = "Returns test details")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful retrieval", response = Test.class),
@ApiResponse(code = 404, message = "Test does not exist"),
@ApiResponse(code = 500, message = "Internal server error")}
)
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(Locale locale, Model model) {
logger.info("TEST");
return "test";
}
}
通过上述设置,当我执行 url: localserver:8080/myApp/swagger-ui 时,几乎没有任何内容可显示,但也没有错误消息。
然后,我将在 spring-fox-swagger-ui-2.2.2.jar 中找到的内容添加到 src/main/resources/META-INF 中(我将其解压缩并粘贴到给定文件夹)。现在,当我转到 localserver:8080/myApp/swagger-ui 时,会显示所有绿色图形,但没有 api 文档。我在服务器日志中注意到 swagger-ui 会查找 swagger-resources 端点,但随后得到 404。当我查看服务器日志时,我发现没有创建这样的端点:swagger-resources、v2/api-docs 等。但是,我注意到这些类已针对 swagger 注释进行了过滤...在 META-INF/resources/webjars/springfox-swagger-ui 文件夹中包含 swagger-resorces 端点的 js 文件 - 也许应该切换到不同的名称?
我不知道如何让它工作...我应该以某种方式声明这些端点还是应该自动创建它们?也许我只是错过了一些小东西,但我最近几天一直在与这个问题作斗争,不知道还应该配置什么才能使它工作。
最佳答案
在@zubactick 的帮助下我成功解决了这个问题。 目前,我有 springfox 和 mvc 的单独配置类。所以,我的springfox配置是这样的:
package com.myPackage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
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.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig{
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.groupName("test")
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo(
"My Project's REST API",
"This is a description of your API.",
"API TOS",
"url",
"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="34595174435c5146514251461a575b59" rel="noreferrer noopener nofollow">[email protected]</a>",
"API License",
"API License URL");
return apiInfo;
}
}
我的 MVC 配置是这样的:
package com.myPackage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@Import({SwaggerConfig.class})
@ComponentScan("com.myPackage.controller")
public class WebSpringConfig extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry
.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public ViewResolver configureViewResolver() {
InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
viewResolve.setPrefix("/WEB-INF/views/");
viewResolve.setSuffix(".jsp");
return viewResolve;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
此外,web.xml 文件中指定了以下内容:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myPackage.config.WebSpringConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Controller 可以与问题中提供的相同,但是使用上面的 swagger 配置,声明的包中的所有 Controller 都会由 swagger 扫描和记录。
关于springfox 2.2.2 没有生成 api 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33780237/