Why does swagger annotations generate api-docs wit

2019-07-05 19:56发布

问题:

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

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>

My swagger properties looked like below

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

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

回答1:

This "default" is the default name of a "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.

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");
    }
...
}

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

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