spring boot aop应用,请求参数的某个值的转换,返回参数的某个值的转换

      之前开发遇到一个问题,就是emoji表情的存储和回显,因为很多地方需要emoji表情,所以单独抽取出来一个公共方法,以emoji表情为例

需求: emoji表情的存储和回显(emoji表情存储需要二次转码才能入库,有兴趣的可以百度一下)

技术选型:通过Spring 的aop功能实现该需求;(以下代码是在SPringboot基础实现的)

准备工作:

/**
 * 基类的某个属性存在emoji表情
 * @Author: ZHS
 * @Date: 2019/3/21 17:16
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.TYPE, ElementType.METHOD,ElementType.FIELD})
@Documented
@Inherited
public @interface FieldAnnotation {

}
/**
 * Controller的方法参数注解的注解,用来描述该值有emoji表情
 * @Author: ZHS
 * @Date: 2019/3/21 17:16
 */
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ParamAnnotation {
    /**
     * 字段名字
     * @return
     */
    String field() default "";
}
/**
 * @Author: ZHS
 * @Date: 2019/3/21 17:18
 */
@Data
public class Stu {
    private Long id;
    @FieldAnnotation
    private String name;//可能存在emoji表情
    private String sex;
}

 

主要处理类:

/**
 * 切点进行参数和返回结果的某个参数转换
 * @Author: ZHS
 * @Date: 2019/3/21 17:31
 */
@Component
@Aspect
@Slf4j
public class FieldAnnAop {
    @Pointcut("execution(public * com.skill.skillmodel.controller..*.*(..))")
    public void excuteService() {
    }
    @Before("excuteService()")
    public void doBeforeMethod1(JoinPoint joinPoint) throws IllegalAccessException {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Annotation[][] methodAnnotations = method.getParameterAnnotations();
        Object[] objs = joinPoint.getArgs();
        System.out.println(methodAnnotations);
        for (int i=0;i stuClass = obj.getClass();
                    Field[] fields = stuClass.getDeclaredFields();
                    for (Field field : fields) {
                        Annotation fieldAnn = field.getAnnotation(FieldAnnotation.class);
                        if (fieldAnn != null) {
                            //设置值
                            field.setAccessible(true);
                            String param = (String) field.get(obj);
                            //转换emoji表情 略
                            String emoji = param+",转换后的值";
                            field.set(obj, emoji);
                        }
                    }
                }
            }
        }
    }

    @AfterReturning(pointcut ="execution(public * com.skill.skillmodel.controller..*.*(..))", returning = "keys")
    public void doAfterReturningAdvice1(JoinPoint joinPoint, Object keys) throws IllegalAccessException {
        if (keys instanceof ResponseEntity){
            Object obj = ((ResponseEntity) keys).getBody();
            Class stuClass = obj.getClass();
            Field[] fields = stuClass.getDeclaredFields();
            for (Field field : fields) {
                Annotation fieldAnn = field.getAnnotation(FieldAnnotation.class);
                if (fieldAnn != null) {
                    //设置值
                    field.setAccessible(true);
                    String param = (String) field.get(obj);
                    //转换emoji表情 略
                    String emoji = param+",第二次转换后的值";
                    field.set(obj, emoji);
                }
            }
            log.info("------>>返回结果:"+keys);
        }
    }

}

写个测试Controller方法:

@Controller
@RequestMapping("field")
@Slf4j
public class FieldAnnController {

    @GetMapping("ann")
    public ResponseEntity ann(@ParamAnnotation Stu stu){
        log.info("----->>入参数:"+stu.getName());
        return ResponseEntity.ok().body(stu);
    }
}

调用---->>localhost:8199/field/ann?name=123456

运行后结果

spring boot aop应用,请求参数的某个值的转换,返回参数的某个值的转换_第1张图片

你可能感兴趣的:(aop)