自定义参数解析器,翻译实体属性

一、需求背景

      统计一个产品的数据,需要根据用户传递的、约定的枚举类型的值,来确定统计维度。
      刚开始,打算使用的是实体类型的gettersetter方式翻译用户传递的枚举值;但是,在获取翻译后的统计维度的时候,发现无法翻译/翻译失败了,于是换个方式,使用自定义参数解析器的方式来处理这个需求.

二、具体实现

1.实体类型

package xxx.operator.qo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;
import java.util.List;

/**
 * Author:   wxz
 * Date:     2023.11.1 9:08
 * Description: 小贷数据分析查询条件
 */
@Data
@NoArgsConstructor
public class PettyLoanStatisticsQuery {
    /**
     * 开始日期
     */
    @Getter
    @Setter
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
    private Date startDate;
    /**
     * 结束日期
     */
    @Getter
    @Setter
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
    private Date endDate;

    /**
     * 前端传过来的统计标记
     */
    @Getter
    @Setter
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private int dimension;
    /**
     * 统计维度
     */
    @Getter
    @JsonIgnore
    private String orderBy;
    @Getter
    @Setter
    @JsonIgnore
    private List<String> organizeIds;

    public void setOrderBy(int dimension) {
        OrderByEnum of = OrderByEnum.of(dimension);
        if (of != null) {
            this.orderBy = of.getOrderBy();
        }
    }

    public PettyLoanStatisticsQuery(int dimension) {
        this.setOrderBy(dimension);
    }

    /**
     *统计维度枚举
     
    private enum OrderByEnum {

        /**
         * 区域
         */
        Area(0, "man.area_id"),
        /**
         * 组织机构
         */
        Organize(1, "duct.organize_id"),
        /**
         * 主体类型identity_type
         */
        IdentityType(2, "man.identity_type");
        @Getter
        @Setter
        private int code;
        /**
         * 统计维度枚举
         */
        @Getter
        @Setter
        private String orderBy;

        OrderByEnum(int code, String orderBy) {
            this.code = code;
            this.orderBy = orderBy;
        }

        /**
         * 根据code查询枚举
         */
        public static OrderByEnum of(int code) {
            OrderByEnum[] values = OrderByEnum.values();
            for (OrderByEnum a : values) {
                if (a.code == code) {
                    return a;
                }
            }
            return null;
        }
    }
}

2.自定义参数解析器

package xxx.config;

import com.ncjr.businessbase.operator.qo.FinancialLeasingStatisticsQuery;
import com.ncjr.businessbase.operator.qo.PettyLoanStatisticsQuery;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义参数解析器,翻译租赁/小贷业务orderBy枚举
 */
@ControllerAdvice
public class FinancialStatisticsRequestBodyAdvice extends RequestBodyAdviceAdapter {

    /**
     * 需要处理的类
     */
    private final List targets = Arrays.asList(FinancialLeasingStatisticsQuery.class, PettyLoanStatisticsQuery.class);

    @Override
    public boolean supports(MethodParameter methodParameter, Type type,
                            Class> aClass) {
        // 判断是否需要对参数进行处理
        return targets.contains(methodParameter.getParameterType());
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
                                Type targetType, Class> converterType) {
        // 在读取请求体后执行自定义逻辑: 金融租赁
        if (body instanceof FinancialLeasingStatisticsQuery) {
            FinancialLeasingStatisticsQuery query = (FinancialLeasingStatisticsQuery) body;
            int dimension = query.getDimension();
            query.setOrderBy(dimension);
        }
        //小贷
        if (body instanceof PettyLoanStatisticsQuery) {
            PettyLoanStatisticsQuery query = (PettyLoanStatisticsQuery) body;
            int dimension = query.getDimension();
            query.setOrderBy(dimension);
        }
        return super.afterBodyRead(body, inputMessage, parameter, targetType, converterType);
    }
}

通过上述自定义的参数解析器翻译枚举的 code属性值以后,就可以拿到对应的orderBy枚举值,然后查询的时候直接使用即可.
(PS:参数解析器中,还可以使用多态等技术,优化处理各个if,简化处理逻辑).

你可能感兴趣的:(springboot系列,SpringCloud系列,java,spring,boot,参数解析器,自定义参数解析器)