自定义参数解析器,翻译实体属性
- 自定义参数解析器,翻译实体属性 推荐度:
- 相关推荐
自定义参数解析器,翻译实体属性
一、需求背景
统计一个产品的数据,需要根据用户传递的、约定的枚举类型的值,来确定统计维度。
刚开始,打算使用的是实体类型的getter
、setter
方式翻译用户传递的枚举值;但是,在获取翻译后的统计维度的时候,发现无法翻译/翻译失败了,于是换个方式,使用自定义参数解析器
的方式来处理这个需求.
二、具体实现
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@JsonIgnoreprivate String orderBy;@Getter@Setter@JsonIgnoreprivate 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, "ductanize_id"),/*** 主体类型identity_type*/IdentityType(2, "man.identity_type");@Getter@Setterprivate int code;/*** 统计维度枚举*/@Getter@Setterprivate 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<Object> targets = Arrays.asList(FinancialLeasingStatisticsQuery.class, PettyLoanStatisticsQuery.class);@Overridepublic boolean supports(MethodParameter methodParameter, Type type,Class<? extends HttpMessageConverter<?>> aClass) {// 判断是否需要对参数进行处理return targets.contains(methodParameter.getParameterType());}@Overridepublic Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,Type targetType, Class<? extends HttpMessageConverter<?>> 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,简化处理逻辑).
最新文章
- Windows 10具有秘密的网络数据包嗅探器
- 振南技术干货集:深入浅出的Bootloader(3)
- Android 12 intent
- Mozilla 面向基于 Debian 的 Linux 发行版
- CSS 实现新拟态(Neumorphism) UI 风格
- 【CPTA——8.数组2(课内实践)】
- C# Spire.Pdf将PDF文件转换为Word文件
- 2023数字科技生态展,移远通信解锁新成就
- ElasticSearch简单操作
- 说说React render方法的原理?在什么时候会被触发?
- Maven 插件统一修改聚合工程项目版本号
- 教育局档案室智慧档案库房建设方案
- Linux上C++通过LDAP协议使用kerberos认证AES加密连接到AD服务器
- matlab导入txt数据
- Linux下MSSQL (SQL Server)数据库无法启动故障处理
- 关于AI(深度学习)相关项目 K8s 部署的一些思考
- Windows系统下使用docker部署redis