在我的春季启动应用程序中,我有多个Rest控制器,需要单独为每个控制器生成swagger。 通过在我的Spring启动应用程序类中为每个控制器使用以下Docket配置,我可以通过转到/ v2 / api-docs下载控制器特定的swagger?group = ai其中i = 1到n 但是在swagger-ui.html中,当我选择a1(/ v2 / api-docs?group = a1)时,它会将路径显示为“/ api / a1 / a1”,同时选择a2(/ v2 / api-docs?greoup = a2),它显示正确的路径即/ api / a2 我试过改变Docket,路径正则表达式是绝对的,例如“api / a1”等,但这没有帮助。
@Bean
public Docket a1Api() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("a1")
.apiInfo(a1Info())
.select().apis(RequestHandlerSelectors.any())
.paths(regex("/api/a1.*"))
.build()
.pathMapping("/");
}
@Bean
public Docket a2Api() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("a2")
.apiInfo(a1Info())
.select().apis(RequestHandlerSelectors.any())
.paths(regex("/api/a2.*"))
.build()
.pathMapping("/");
}
private ApiInfo a1Info() {
return new ApiInfoBuilder()
.title("a1 Swagger 2.0")
.description("a1")
.license("a1")
.version("1.0")
.build();
}
private ApiInfo a2Info() {
return new ApiInfoBuilder()
.title("a2 Swagger 2.0")
.description("a2")
.license("a2")
.version("1.0")
.build();
}
休息控制器
@RestController
@Api(tags = "A1")
@RequestMapping("/api/a1")
public class a1Controller {
@ApiOperation(value = "a1")
@RequestMapping(value = "", method = RequestMethod.POST)
public a1Response invoke(@RequestBody a1Request va1Request) {
.....;
}
}
@RestController
@Api(tags = "An")
@RequestMapping("/api/an")
public class a1Controller {
@ApiOperation(value = "an")
@RequestMapping(value = "", method = RequestMethod.POST)
public anResponse invoke(@RequestBody anRequest vanRequest) {
.....;
}
}
知道我怎么能解决这个问题.... 我正在使用springfox swagger版本2.6.1
您可以使用以下Swagger配置添加多个控制器类:
1)创建一个Swagger配置类。
2)然后指定控制器的基础包。
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
private static final ApiInfo DEFAULT_API_INFO = null; //Swagger info
@Bean
public Docket api()
{
return new Docket(DocumentationType.SWAGGER_2)
.forCodeGeneration(Boolean.TRUE)
.select()
.apis(RequestHandlerSelectors.basePackage("com.user.controller"))
.paths(PathSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/logout.*")))
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"REST API",
"REST description of API.",
"API TOS",
"Terms of service",
new Contact("Rajib Garai", "https://www.linkedin.com/in/rajibgarai90/", "[email protected]"),
"License of API", "API license URL", Collections.emptyList());
}
}
这是我编写的代码,用于在每个控制器的运行时查找和自动创建Docket,还有一个默认Docket以显示所有在一个组中。
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Autowired
ConfigurableApplicationContext context;
//Default Docket to show all
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(metaData())
.forCodeGeneration(Boolean.TRUE)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
//Creating Docket Dynamically per Rest Controller
@PostConstruct
public void postConstruct() throws ClassNotFoundException {
ClassPathScanningCandidateComponentProvider provider
= new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
for (BeanDefinition beanDef : provider.findCandidateComponents("com.blah.blah.package")) {
Class<?> cl = Class.forName(beanDef.getBeanClassName());
RequestMapping requestMapping = cl.getAnnotation(RequestMapping.class);
if (null != requestMapping && null != requestMapping.value() && requestMapping.value().length > 0) {
String resource_group = requestMapping.value()[0];
SingletonBeanRegistry beanRegistry = context.getBeanFactory();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName(resource_group)
.apiInfo(metaData())
.forCodeGeneration(Boolean.TRUE)
.select()
//.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.regex(resource_group + ".*"))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
beanRegistry.registerSingleton(cl.getSimpleName() + "_docket_api", docket);
}
}
}
private ApiInfo metaData() {
return new ApiInfoBuilder()
.title("some Title Here")
.description("Some Desciption")
.version("1.0")
.contact(new Contact("Asad Abdin", "", "[email protected]"))
.build();
}