支持spring
package com.hdd.oms.service.utils;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Component;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;
import java.lang.reflect.Field;
import static springfox.documentation.schema.Annotations.findPropertyAnnotation;
import static springfox.documentation.swagger.schema.ApiModelProperties.findApiModePropertyAnnotation;
/**
* @Auther mr.dong
* @Date 2020/6/16
*/
@Component
@Slf4j
public class ModelSortInit implements ModelPropertyBuilderPlugin {
@Override
public void apply(ModelPropertyContext context) {
Optional<BeanPropertyDefinition> beanPropertyDefinitionOpt = context.getBeanPropertyDefinition();
Optional<ApiModelProperty> annotation = Optional.absent();
if (context.getAnnotatedElement().isPresent()) {
annotation = annotation.or(findApiModePropertyAnnotation(context.getAnnotatedElement().get()));
}
if (context.getBeanPropertyDefinition().isPresent()) {
annotation = annotation.or(findPropertyAnnotation(context.getBeanPropertyDefinition().get(), ApiModelProperty.class));
}
if (beanPropertyDefinitionOpt.isPresent()) {
BeanPropertyDefinition beanPropertyDefinition = beanPropertyDefinitionOpt.get();
if (annotation.isPresent() && annotation.get().position() != 0) {
return;
}
AnnotatedField field = beanPropertyDefinition.getField();
Class<?> clazz = field.getDeclaringClass();
Field[] declaredFields = clazz.getDeclaredFields();
Field declaredField;
try {
declaredField = clazz.getDeclaredField(field.getName());
}
catch (NoSuchFieldException | SecurityException e) {
log.error("", e);
return;
}
int indexOf = ArrayUtils.indexOf(declaredFields, declaredField);
if (indexOf != -1) {
context.getBuilder().position(indexOf);
}
}
}
@Override
public boolean supports(DocumentationType delimiter) {
return SwaggerPluginSupport.pluginDoesApply(delimiter);
}
}
package cn.superdesk.uniorder.config;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Component;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;
import java.lang.reflect.Field;
import static springfox.documentation.schema.Annotations.findPropertyAnnotation;
import static springfox.documentation.swagger.schema.ApiModelProperties.findApiModePropertyAnnotation;
/**
* @Description: swagger接口模型排序和定义顺序保持一致
* @Author: linjinyu
* @CreateDate: 2021/6/23 下午8:51
* @UpdateUser: linjinyu
* @UpdateDate: 2021/6/23 下午8:51
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
@Slf4j
@Component
public class CustomApiModelPositionBuilder implements ModelPropertyBuilderPlugin {
@Override
public boolean supports(DocumentationType delimiter) {
return SwaggerPluginSupport.pluginDoesApply(delimiter);
}
@Override
public void apply(ModelPropertyContext context) {
Optional<BeanPropertyDefinition> beanPropertyDefinitionOpt = context.getBeanPropertyDefinition();
Optional<ApiModelProperty> annotation = Optional.absent();
if (context.getAnnotatedElement().isPresent()) {
annotation = annotation.or(findApiModePropertyAnnotation(context.getAnnotatedElement().get()));
}
if (context.getBeanPropertyDefinition().isPresent()) {
annotation = annotation.or(findPropertyAnnotation(context.getBeanPropertyDefinition().get(), ApiModelProperty.class));
}
if (beanPropertyDefinitionOpt.isPresent()) {
BeanPropertyDefinition beanPropertyDefinition = beanPropertyDefinitionOpt.get();
if (annotation.isPresent() && annotation.get().position() != 0) {
return;
}
AnnotatedField field = beanPropertyDefinition.getField();
Class<?> clazz = field.getDeclaringClass();
Field[] declaredFields = clazz.getDeclaredFields();
Field declaredField;
try {
declaredField = clazz.getDeclaredField(field.getName());
} catch (NoSuchFieldException | SecurityException e) {
log.error("", e);
return;
}
int indexOf = ArrayUtils.indexOf(declaredFields, declaredField);
if (indexOf != -1) {
context.getBuilder().position(indexOf);
}
}
}
}
package cn.superdesk.uniorder.config;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Component;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;
import java.lang.reflect.Field;
import static springfox.documentation.schema.Annotations.findPropertyAnnotation;
import static springfox.documentation.swagger.schema.ApiModelProperties.findApiModePropertyAnnotation;
/**
* @Description: swagger接口模型属性排序和定义顺序保持一致
* @Author: linjinyu
* @CreateDate: 2021/6/23 下午8:49
* @UpdateUser: linjinyu
* @UpdateDate: 2021/6/23 下午8:49
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
@Slf4j
@Component
public class CustomApiModelPropertyPositionBuilder implements ModelPropertyBuilderPlugin {
@Override
public boolean supports(DocumentationType delimiter) {
return SwaggerPluginSupport.pluginDoesApply(delimiter);
}
@Override
public void apply(ModelPropertyContext context) {
Optional<BeanPropertyDefinition> beanPropertyDefinitionOpt = context.getBeanPropertyDefinition();
Optional<ApiModelProperty> annotation = Optional.absent();
if (context.getAnnotatedElement().isPresent()) {
annotation = annotation.or(findApiModePropertyAnnotation(context.getAnnotatedElement().get()));
}
if (context.getBeanPropertyDefinition().isPresent()) {
annotation = annotation.or(findPropertyAnnotation(context.getBeanPropertyDefinition().get(), ApiModelProperty.class));
}
if (beanPropertyDefinitionOpt.isPresent()) {
BeanPropertyDefinition beanPropertyDefinition = beanPropertyDefinitionOpt.get();
if (annotation.isPresent() && annotation.get().position() != 0) {
return;
}
AnnotatedField field = beanPropertyDefinition.getField();
Class<?> clazz = field.getDeclaringClass();
Field[] declaredFields = clazz.getDeclaredFields();
Field declaredField;
try {
declaredField = clazz.getDeclaredField(field.getName());
} catch (NoSuchFieldException | SecurityException e) {
log.error("", e);
return;
}
int indexOf = ArrayUtils.indexOf(declaredFields, declaredField);
if (indexOf != -1) {
context.getBuilder().position(indexOf);
}
}
}
}
Required type: com.google.common.base.Optional <BeanPropertyDefinition>
Provided: java.util.Optional <BeanPropertyDefinition>
原网址: 访问
创建于: 2021-06-23 20:42:32
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论