为什么 swagger 注释会生成带有默认路径前缀的 api-docs [英] Why does swagger annotations generate api-docs with default path prefix

查看:44
本文介绍了为什么 swagger 注释会生成带有默认路径前缀的 api-docs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used the below maven plugin to integrate swagger with my application https://github.com/martypitt/swagger-springmvc

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

I configured the below in my 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/customservicedocumentation.services.version=1.0

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

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

My api-docs.json that gets generated looks like as below and I am not sure why it does not have a base path and why it has a prefix "/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"的默认名称

This "default" is the default name of a "swagger group"

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

swagger group 是这个库引入的一个概念,它只是应用程序中 Swagger 资源列表的唯一标识符.引入这一概念的原因是为了支持需要多个资源列表的应用程序.

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 配置创建.像这样:

You usually will have only one group and it is named "default". If you want to change it, you should set a group name in the SwaggerSpringMvcPlugin created by your swagger config. Something like this:

@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:

After that you should have in your Swagger generated API JSON URLs like this:

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

这篇关于为什么 swagger 注释会生成带有默认路径前缀的 api-docs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆