接口自定义排序就是以下俩个步骤,直接复制粘贴即可。
1.在ApiModelProperty加上 position属性即可
@ApiModelProperty(name = "username", value = "用户名", dataType = "string" ,position = 1)
@ToString
@Setter
@Getter
@NoArgsConstructor
@TableName("表名")
public class SystemUserEntity {
/**
* 用户id
*/
@ApiModelProperty(hidden = true)
@TableId("user_id")
private Integer userId;
/**
* 用户名
*/
@ApiModelProperty(name = "username", value = "用户名", dataType = "string" ,position = 1)
@TableField("username")
private String username;
}
2. 需要重写以下类。
@Primary //同一个接口,可能会有几种不同的实现类,而默认只会采取其中一种的情况下
@Component("ServiceModelToSwagger2Mapper")
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CustomModelToSwaggerMapper extends ServiceModelToSwagger2MapperImpl{
@Override
protected List<Parameter> parameterListToParameterList(List<springfox.documentation.service.Parameter> list) {
//list需要根据order|postion排序
list = list.stream().sorted((p1, p2) -> Integer.compare(p1.getOrder(), p2.getOrder())).collect(Collectors.toList());
return super.parameterListToParameterList(list);
}
}
@Primary
@Component
public class CustomSwaggerParameterBuilder implements ExpandedParameterBuilderPlugin {
private final DescriptionResolver descriptions;
private final EnumTypeDeterminer enumTypeDeterminer;
@Autowired
public CustomSwaggerParameterBuilder(
DescriptionResolver descriptions,
EnumTypeDeterminer enumTypeDeterminer) {
this.descriptions = descriptions;
this.enumTypeDeterminer = enumTypeDeterminer;
}
@Override
public void apply(ParameterExpansionContext context) {
Optional<ApiModelProperty> apiModelPropertyOptional = context.findAnnotation(ApiModelProperty.class);
if (apiModelPropertyOptional.isPresent()) {
fromApiModelProperty(context, apiModelPropertyOptional.get());
}
Optional<ApiParam> apiParamOptional = context.findAnnotation(ApiParam.class);
if (apiParamOptional.isPresent()) {
fromApiParam(context, apiParamOptional.get());
}
}
@Override
public boolean supports(DocumentationType delimiter) {
return SwaggerPluginSupport.pluginDoesApply(delimiter);
}
private void fromApiParam(ParameterExpansionContext context, ApiParam apiParam) {
String allowableProperty = Strings.emptyToNull(apiParam.allowableValues());
AllowableValues allowable = allowableValues(
Optional.fromNullable(allowableProperty),
context.getFieldType().getErasedType());
maybeSetParameterName(context, apiParam.name())
.description(descriptions.resolve(apiParam.value()))
.defaultValue(apiParam.defaultValue())
.required(apiParam.required())
.allowMultiple(apiParam.allowMultiple())
.allowableValues(allowable)
.parameterAccess(apiParam.access())
.hidden(apiParam.hidden())
.scalarExample(apiParam.example())
.complexExamples(Examples.examples(apiParam.examples()))
.order(SWAGGER_PLUGIN_ORDER)
.build();
}
private void fromApiModelProperty(ParameterExpansionContext context, ApiModelProperty apiModelProperty) {
String allowableProperty = Strings.emptyToNull(apiModelProperty.allowableValues());
AllowableValues allowable = allowableValues(
Optional.fromNullable(allowableProperty),
context.getFieldType().getErasedType());
maybeSetParameterName(context, apiModelProperty.name())
.description(descriptions.resolve(apiModelProperty.value()))
.required(apiModelProperty.required())
.allowableValues(allowable)
.parameterAccess(apiModelProperty.access())
.hidden(apiModelProperty.hidden())
.scalarExample(apiModelProperty.example())
.order(apiModelProperty.position()) //源码这里是: SWAGGER_PLUGIN_ORDER,需要修正
.build();
}
private ParameterBuilder maybeSetParameterName(ParameterExpansionContext context, String parameterName) {
if (!Strings.isNullOrEmpty(parameterName)) {
context.getParameterBuilder().name(parameterName);
}
return context.getParameterBuilder();
}
private AllowableValues allowableValues(final Optional<String> optionalAllowable, Class<?> fieldType) {
AllowableValues allowable = null;
if (enumTypeDeterminer.isEnum(fieldType)) {
allowable = new AllowableListValues(getEnumValues(fieldType), "LIST");
} else if (optionalAllowable.isPresent()) {
allowable = ApiModelProperties.allowableValueFromString(optionalAllowable.get());
}
return allowable;
}
private List<String> getEnumValues(final Class<?> subject) {
return Lists.transform(Arrays.asList(subject.getEnumConstants()), new Function<Object, String>() {
@Override
public String apply(final Object input) {
return input.toString();
}
});
}
}
自定义接口中参数顺序就是以下俩个步骤。
1 需要加 @ApiSort(value = 5) @ApiOperation(position = 5)这俩个参数。
@ApiSort(value = 5)
@Api(tags="用户管理")
@RestController
@RequestMapping("/api/v1/user")
public class SystemUserController {
@Autowired
private SystemUserService systemUserService;
@ApiOperation(value = "1 添加用户",position = 1)
@PostMapping("/add")
//@ApiOperationSupport(order = 1)
public ResponseData save(SystemUserEntity systemUserEntity){
return ResponseData.success(systemUserService.save(systemUserEntity));
}
@ApiOperation(value = "2 修改用户",position = 2)
@PostMapping("/update")
//@ApiOperationSupport(order = 2)
public ResponseData update(SystemUserEntity systemUserEntity,Integer id){
Assert.notNull(id,"id不能为空");
systemUserEntity.setUserId(id);
systemUserService.updateById(systemUserEntity);
return ResponseData.success();
}
@ApiOperation(value = "3 删除用户",position = 3)
@PostMapping("/delete")
//@ApiOperationSupport(order = 3)
public ResponseData delete(Integer id){
systemUserService.removeById(id);
return ResponseData.success();
}
@ApiOperation(value = "4 查询所有用户",position = 4)
@PostMapping("/list")
//@ApiOperationSupport(order = 4)
public ResponseData list(){
return ResponseData.success(systemUserService.list());
}
@ApiOperation(value = "5 查询测试",position = 5)
@PostMapping("/test")
public ResponseData tset(){
return ResponseData.success(systemUserService.list());
}
}
2 在扫描注解的配置 swaggerconifg 加入@EnableSwaggerBootstrapUI注解
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig implements WebMvcConfigurer {
/**
* 创建DocketBean
* @return Docket
*/
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("包名"))
.paths(PathSelectors.any())
.build()
.apiInfo(getApiInfo());
}
}