springboot+swagger2 接口排序无效
Posted 诺浅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot+swagger2 接口排序无效相关的知识,希望对你有一定的参考价值。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.xxx.controller.global.GlobalDummy;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {
@Autowired
private AppConfigProperties config;
@Bean
public Docket createDefaultApi() {
return createDocket("Default", defaultPackage());
}
// @Bean
public Docket createGlobalApi() {
return createDocket("Global", globalPackage());
}
private Docket createDocket(String groupName, com.google.common.base.Predicate<RequestHandler> filter) {
List<Parameter> parameters = Lists.newArrayList();
parameters.add(new ParameterBuilder().name("Content-Type").modelRef(new ModelRef("String"))
.defaultValue("application/json").parameterType("header").required(true).build());
parameters.add(new ParameterBuilder().name("uid").modelRef(new ModelRef("Long")).defaultValue("")
.parameterType("header").required(false).build());
// parameters.add(new ParameterBuilder().name("pageNum").modelRef(new
// ModelRef("Integer")).defaultValue("1").parameterType("query")
// .required(false).build());
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())//
.groupName(groupName)// Global/Organization
.select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).apis(filter)
.paths(PathSelectors.any()).build().globalOperationParameters(parameters).pathMapping("/");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title(config.getTitle()).description(config.getDescription())
.version(config.getVersion()).build();
}
private com.google.common.base.Predicate<RequestHandler> globalPackage() {
String pkg = GlobalDummy.class.getPackage().getName();
return (input) -> Optional.fromNullable(input.declaringClass())
.transform(c -> this.getPackageName(c).startsWith(pkg)).or(false);
}
private com.google.common.base.Predicate<RequestHandler> defaultPackage() {
String pkg = GlobalDummy.class.getPackage().getName();
return (input) -> Optional.fromNullable(input.declaringClass())
.transform(c -> !this.getPackageName(c).startsWith(pkg)).or(true);
}
private String getPackageName(Class<?> c) {
return c.getPackage().getName();
}
}
public class GlobalDummy {
}
一个测试的controller
@PostMapping("xxxx")
@ApiOperation(value = "xxxxxx", notes = "xxxxx")
@ApiOperationSupport(order = 1)
public ResultList<xx> dayList(@RequestBody xx condition) {
}
@PostMapping("xxxx/xxx")
@ApiOperation(value = "xxxx", notes = "xxxx")
@ApiOperationSupport(order = 2)
public ResultList<xx> dayListAcl(@RequestBody xx condition) {
}
启用下面这个这个才能排序,但是高版本的knife4j-spring-boot-starter
没有这个选项,据说是默认开启,但反正我用高版本的还是没能排序,不知道什么原因。
这里的2.0.1
版本是亲测有效的。
以上是关于springboot+swagger2 接口排序无效的主要内容,如果未能解决你的问题,请参考以下文章