拦截器与注解实现项目金额字段位数格式化
文章目录
项目中有很多字段涉及到金额,尤其是银行项目的金额字段,decimal,如果不统一处理默认金额位数就会造成代码冗余。下面就是使用拦截器与注解的方式实现金额位数处理。
要使用Java的拦截器来实现金额字段的格式化并返回给前端,可以按照以下步骤进行操作:
1.创建一个自定义的注解,用于标记需要格式化的金额字段
例如,可以创建一个名为@AmountFormat
的注解。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AmountFormat {
int scale() default 2; // 默认保留两位小数
}
2.创建一个拦截器类,用于在请求处理前对金额字段进行格式化
在下面的代码中,我们创建了一个AmountFormatInterceptor
拦截器类,并实现了HandlerInterceptor
接口。在preHandle
方法中,我们获取到处理请求的方法和类,然后遍历类中的字段,判断是否被@AmountFormat
注解标记。如果是,则对该字段进行格式化处理,将其保留指定的小数位数。
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AmountFormatInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 在请求处理前对金额字段进行格式化
Object handlerObject = handler;
if (handlerObject instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handlerObject;
Method method = handlerMethod.getMethod();
Class<?> clazz = method.getDeclaringClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(AmountFormat.class)) {
field.setAccessible(true);
Object fieldValue = field.get(handlerMethod.getBean());
if (fieldValue instanceof BigDecimal) {
AmountFormat amountFormat = field.getAnnotation(AmountFormat.class);
int scale = amountFormat.scale();
BigDecimal formattedValue = ((BigDecimal) fieldValue).setScale(scale, RoundingMode.HALF_UP);
field.set(handlerMethod.getBean(), formattedValue);
}
}
}
}
return true;
}
}
ps:
1、preHandle:请求方法前置拦截,该方法会在Controller处理之前进行调用,Spring中可以有多个Interceptor,这些拦截器会按照设定的Order顺序调用,当有一个拦截器在preHandle中返回false的时候,请求就会终止。2、postHandle:preHandle返回结果为true时,在Controller方法执行之后,视图渲染之前被调用.
3、afterCompletion:在preHandle返回ture,并且整个请求结束之后,执行该方法。
3.注册拦截器并配置拦截路径
在Spring Boot项目中,可以通过配置类来注册拦截器并设置拦截路径。例如,可以创建一个名为WebConfig
的配置类。编写完拦截器之后,通过一个配置类设置拦截器,并且可以通过addPathPatterns和excludePathPatterns执行哪些请求需要被拦截,哪些不需要被拦截。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AmountFormatInterceptor()).addPathPatterns("/**");
}
}
在上述代码中,我们创建了一个WebConfig
配置类,并实现了WebMvcConfigurer
接口。在addInterceptors
方法中,我们注册了AmountFormatInterceptor
拦截器,并设置了拦截路径为"/**"
,表示拦截所有请求。
4.在需要格式化的金额字段上添加@AmountFormat
注解
例如,可以在实体类中的金额字段上添加该注解。
public class Product {
@AmountFormat(scale = 2)
private BigDecimal price;
// getter and setter
}