加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 04:22:12 所属栏目:Java 来源:网络整理
导读:我使用下面的maven插件将swagger与我的应用程序集成 https://github.com/martypitt/swagger-springmvc 我在我的spring servlet xml中配置了以下内容 mvc:annotation-driven/ !-- Required so swagger-springmvc can access spring's RequestMappingHandlerMa
我使用下面的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>

我的招摇属性如下所示

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"
}
}

解决方法

这个“默认”是“招摇组”的默认名称

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.

您通常只有一个组,它被命名为“默认”.如果要更改它,则应在swagger配置创建的SwaggerSpringMvcPlugin中设置组名.像这样的东西:

@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"
}
....

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读