pom采用这个依赖,ui不能用2.9.2的。2.9.2的ui接口顺序不是根据后端排序显示的
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
配置文件为:
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.kenny.enterpriser.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("XXXX后台")
.description("XXXXX后台")
.version("1.0")
.contact(new Contact("Kenny","blog.csdn.net","aaa@gmail.com"))
.license("The Apache License")
.licenseUrl("http://www.baidu.com")
.build());
}
}
yml:
swagger:
enable: true
想实现这种效果排序
@ApiOperation(value = "XXX)",position = 1)
@ApiOperation(value = "XXX)",position = 2)
@ApiOperation(value = "XXX)",position = 3)
则新建一个类增强ServiceModelToSwagger2MapperImpl,即可实现
@Component
@Primary
public class ServiceModelToSwagger2MapperImplExt extends ServiceModelToSwagger2MapperImpl {
@Override
protected Map<String, Path> mapApiListings(Multimap<String, ApiListing> apiListings) {
Map<String, Path> paths = new LinkedHashMap<>();
Multimap<String, ApiListing> apiListingMap = LinkedListMultimap.create();
Iterator iter = apiListings.entries().iterator();
while(iter.hasNext())
{
Map.Entry<String, ApiListing> entry = (Map.Entry<String, ApiListing>)iter.next();
ApiListing apis = entry.getValue();
List<ApiDescription> apiDesc = apis.getApis();
List<ApiDescription> newApi = new ArrayList<>();
for(ApiDescription a:apiDesc){
newApi.add(a);
}
Collections.sort(newApi, new Comparator<ApiDescription>() {
@Override
public int compare(ApiDescription left, ApiDescription right) {
return Integer.compare(left.getOperations().get(0).getPosition(),right.getOperations().get(0).getPosition());
}
});
try {
//因ApiListing的属性都是final故需要通过反射来修改值
ModifyFinalUtils.modify(apis, "apis", newApi);
} catch (Exception e) {
e.printStackTrace();
}
apiListingMap.put(entry.getKey(),apis);
}
for (ApiListing each : apiListingMap.values()) {
for (ApiDescription api : each.getApis()) {
paths.put(api.getPath(), mapOperations(api, Optional.fromNullable(paths.get(api.getPath()))));
}
}
return paths;
}
private Path mapOperations(ApiDescription api, Optional<Path> existingPath) {
Path path = existingPath.or(new Path());
for (springfox.documentation.service.Operation each : nullToEmptyList(api.getOperations())) {
Operation operation = mapOperation(each);
path.set(each.getMethod().toString().toLowerCase(), operation);
}
return path;
}
}
public class ModifyFinalUtils {
public static void modify(Object object, String fieldName, Object newFieldValue) throws Exception {
Field field = object.getClass().getDeclaredField(fieldName);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true); //Field 的 modifiers 是私有的
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
if(!field.isAccessible()) {
field.setAccessible(true);
}
field.set(object, newFieldValue);
}
}