zoukankan      html  css  js  c++  java
  • Swagger模型字段排序问题

    由于springfox-swagger2的实现的问题,程序只能配置为按照英文字母排序。不过我们可以通过编写插件实现字段按类变量定义顺序排序。具体插件代码为

    import static springfox.documentation.schema.Annotations.findPropertyAnnotation;
    import static springfox.documentation.swagger.schema.ApiModelProperties.findApiModePropertyAnnotation;
    import java.lang.reflect.Field;
    import org.apache.commons.lang3.ArrayUtils;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.stereotype.Component;
    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 springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
    import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
    import springfox.documentation.swagger.common.SwaggerPluginSupport;
    
    @Component
    public class CustomApiModelPropertyPositionBuilder implements ModelPropertyBuilderPlugin {
    
        private Log log = LogFactory.getLog(getClass());
    
        @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);
                }
            }
        }
    }
  • 相关阅读:
    C# 文件操作
    Wpf ListView展示风格
    PowerShell->>获取本地计算机的用户组和组成员
    MySQL->>innodb_autoinc_lock_mode参数控制auto_increment 插入数据时相关锁的模式
    SQL Server ->> 使用CROSS APPLY语句是遇到聚合函数中包含外部引用列时报错
    【转】Kettle发送邮件步骤遇到附件名是中文名变成乱码的问题解决办法
    SSIS ->> Excel Destination无法接受大于255个字符长度的字符字段
    SQL Server ->> 存储过程sp_describe_first_result_set解析T-SQL语句的结果集结构信息
    Windows ->> 解决Windows 10下面无法多用户同时远程桌面
    SQL Server ->> 查询添加XLOCK表提示不阻塞其他线程
  • 原文地址:https://www.cnblogs.com/cnndevelop/p/13392479.html
Copyright © 2011-2022 走看看