java - Swagger 与 spring MVC 不生成服务文档

标签 java spring spring-mvc swagger swagger-ui

我正在使用 swagger 和 spring mvc,pom.xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

在 spring 配置.xml 中

<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**"
    location="classpath:/META-INF/resources/webjars/" />

我也使用基于java的配置

@Configuration
@EnableSwagger2
@PropertySource("classpath:application.properties")
public class SwaggerConfig extends WebMvcConfigurerAdapter {

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(" Admin Api").description("Admin Api").version("V1")
                .termsOfServiceUrl("http://terms-of-services.url").license("LICENSE")
                .licenseUrl("http://url-to-license.com").build();
    }
}

在我使用的 Controller 中

@Controller
@RequestMapping({ "/" })
@Api(value = "product", description = "Products Web Services") // Swagger annotation
public class ProductController {
    @ApiOperation(value = "products", nickname = "Get list of all products", response = ProductListResponse.class)
    @RequestMapping(value = "/products", method = RequestMethod.GET)
    public @ResponseBody ProductListResponse getAllProducts(HttpServletResponse httpServletResponse) {

application.propeties中我添加了springfox.documentation.swagger.v2.path=/api-docs。 这就是我用来生成文档的所有信息 使用网址http://localhost:8080/admin-api/admin/api-docs ,它不会生成端点的文档

{
swagger: "2.0",
info: {
description: " Admin Api",
version: "V1",
title: "Admin Api",
termsOfService: "http://terms-of-services.url",
license: {
name: "LICENSE",
url: "http://url-to-license.com"
}
},
host: "localhost:8080",
basePath: "/admin-api/admin"
}

最佳答案

已解决。在错过了很多事情之后,我找到了解决方案,我将方法 Docket api() 更改为

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

我认为旧方法中的 PathSelectors.regex("/api/.*") 部分限制了端点的查找过程

关于java - Swagger 与 spring MVC 不生成服务文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44284956/

相关文章:

java - 从字符串java中拆分文本

java - Hibernate 刷新和 JTAUnexpectedRollbackException

java - Datatables Spring 4 Hibernate Tiles 3配置错误

java - Thymeleaf:使用#dates.format() 函数来格式化具有国际化的日期。

java - 有条件地动态将表单的目标设置为 iframe

java - 一个 Web 应用程序中的 Spring MVC 和 Spring WS 调度程序 Servlet 配置

java - 具有 Spring Security 的 OTP

Java - volatile 和可见性

java - 错误:调用需要API级别23(当前最小值为15):

java - NoSuchBeanDefinitionException : No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate