我正在努力将Springfox 2.2.2集成到我的Spring MVC项目中,但是没有生成api-docs,因为我应该这样做。下面有关我的配置的一些信息。
我提供了以下依赖项(以及其他库,如quickxml,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",
"me@wherever.com",
"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/");
}
}
示例性控制器如下所示:
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时,几乎没有任何内容可以显示,但没有错误消息。
然后,我在src / main / resources / META-INF中添加了我在spring-fox-swagger-ui-2.2.2.jar中找到的内容(我解压缩并粘贴到给定的文件夹)。现在,当我去localserver:8080 / myApp / swagger-ui时,所有绿色图形都显示但没有api文档。我在服务器日志中注意到swagger-ui寻找swagger-resources端点,但它获得了404。当我浏览服务器日志时,我发现没有创建这样的端点:swagger-resources,v2 / api-docs等。但是,我注意到这些类被过滤掉了swagger注释...有一个springfox。在包含swagger-resorces端点的META-INF / resources / webjars / springfox-swagger-ui文件夹中的js文件 - 也许它应该切换到不同的名称?
我不知道如何使它工作......我应该以某种方式声明这些端点还是应该自动创建它们?也许我只是错过了一些小事,但我在最后几天与这个问题作斗争,并且无法弄清楚应该配置什么才能让它发挥作用。
答案 0 :(得分:3)
我已经设法在@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",
"me@wherever.com",
"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>
控制器可以与问题中提供的控制器相同,但是使用上面的swagger配置,声明包中的所有控制器都会被swagger扫描和记录。
答案 1 :(得分:0)
@gromajus,您可以尝试在示例控制器类中使用@RestController
注释而不是@Controller
注释吗?