java - 为什么 swagger 注释会生成带有默认路径前缀的 api-docs

标签 java spring-mvc swagger swagger-maven-plugin

我使用下面的 maven 插件将 swagger 与我的应用程序集成 https://github.com/martypitt/swagger-springmvc

我在我的 spring servlet xml 中配置了以下内容

<mvc:annotation-driven/> <!-- Required so swagger-springmvc can access spring's RequestMappingHandlerMapping  -->
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />

<mvc:default-servlet-handler/>

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" >
            <list>

                <value>/WEB-INF/swagger.properties</value>
            </list>
        </property>
    </bean>

我的 swagger 属性如下所示

documentation.services.basePath= http://payrollservice.com/customservice documentation.services.version=1.0

我生成的 api-docs.json 如下所示,我不确定为什么它没有基本路径以及为什么它有前缀“/default”

{
apiVersion: "1.0",
swaggerVersion: "1.2",
apis: [
{
path: "/default/custom-controller",
description: "backupset API"
}
],
info: {
title: "default Title",
description: "Api Description",
termsOfServiceUrl: "Api terms of service",
contact: "Contact Email",
license: "Licence Type",
licenseUrl: "License URL"
}
}

最佳答案

这个“default”是一个“swagger group”的默认名称

https://github.com/martypitt/swagger-springmvc#swagger-group

A swagger group is a concept introduced by this library which is simply a unique identifier for a Swagger Resource Listing within your application. The reason this concept was introduced was to support applications which require more than one Resource Listing.

您通常只有一个组,它被命名为“默认”。如果你想改变它,你应该在SwaggerSpringMvcPlugin中设置一个组名。由您的 Swagger 配置创建。像这样:

@Configuration
@EnableSwagger
public class MySwaggerConfig {
    private SpringSwaggerConfig springSwaggerConfig;

    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
      this.springSwaggerConfig = springSwaggerConfig;
    }


    @Bean
    public SwaggerSpringMvcPlugin customImplementation() {
      return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
            .swaggerGroup("my-group");
    }
...
}

之后,您应该在 Swagger 中生成如下所示的 API JSON URL:

...
apis: [
{
    path: "/my-group/custom-controller",
    description: "backupset API"
}
....

关于java - 为什么 swagger 注释会生成带有默认路径前缀的 api-docs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25674863/

相关文章:

java - Jackson 使用可变键序列化对象的数组列表

java - 在 Mac OSX 上设置 lucene 时遇到问题

java - Tomcat 无法启动组件

java - Spring3 与 Tapestry 5.3

javascript - 部署时获取空路径是 swagger 的关键。本地正在工作

java - 在 spring mvc 中自定义 swagger 注解

java - Mockito 模拟 void 方法

java - 仅让类的一部分等待执行,无论是否有线程?

java - 在构造函数结束后调用方法

spring-mvc - 自己的Spring OAuth2服务器以及3rdparty OAuth提供程序