• ResponseBodyAdvice 返回数据处理


    一、自定义EncodeResponseBodyAdvice实现ResponseBodyAdvice

    1. package com.onway.interceptor;
    2. import com.alibaba.fastjson.JSONObject;
    3. import com.onway.leaguer.center.server.common.annotation.ResponseAnnotation;
    4. import com.onway.leaguer.center.server.common.utils.AESUtil;
    5. import lombok.extern.slf4j.Slf4j;
    6. import org.springframework.beans.factory.annotation.Value;
    7. import org.springframework.core.MethodParameter;
    8. import org.springframework.core.annotation.AnnotationUtils;
    9. import org.springframework.http.MediaType;
    10. import org.springframework.http.server.ServerHttpRequest;
    11. import org.springframework.http.server.ServerHttpResponse;
    12. import org.springframework.web.bind.annotation.ControllerAdvice;
    13. import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
    14. import java.lang.reflect.AnnotatedElement;
    15. /**
    16. * 描述:
    17. *
    18. */
    19. @ControllerAdvice(basePackages = "com.onway.leaguer.center.server.controller")
    20. @Slf4j
    21. public class EncodeResponseBodyAdvice implements ResponseBodyAdvice {
    22. @Override
    23. public boolean supports(MethodParameter methodParameter, Class aClass) {
    24. //默认false
    25. boolean isIntercept = false;
    26. //第一种:通过注解拦截,拦截@ResponseAnnotation接口
    27. AnnotatedElement annotatedElement = methodParameter.getAnnotatedElement();
    28. ResponseAnnotation responseAnnotation = AnnotationUtils.findAnnotation(annotatedElement, ResponseAnnotation.class);
    29. if (responseAnnotation != null) {
    30. isIntercept = true;
    31. }
    32. //第二种:通过方法名称拦截,拦截指定方法
    33. Method method = methodParameter.getMethod();
    34. if ("saveOrder".equals(method.getName())) {
    35. isIntercept = true;
    36. }
    37. return isIntercept;
    38. }
    39. @Override
    40. public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
    41. if (null == body) {
    42. return null;
    43. }
    44. //对返回数据加密
    45. return AESUtil.dataEncrypt(JSONObject.toJSONString(body));
    46. }
    47. }

    二、添加ResponseAnnotation

    1. import java.lang.annotation.*;
    2. /**
    3. * 描述:
    4. *
    5. */
    6. @Target({ElementType.METHOD})
    7. @Retention(RetentionPolicy.RUNTIME)
    8. @Documented
    9. public @interface ResponseAnnotation {
    10. }

     三、需要拦截的接口,添加@ResponseAnnotation注解

    1. import com.onway.dayu.api.Ret;
    2. import com.onway.dayu.api.RetUtil;
    3. import com.onway.leaguer.center.server.common.annotation.ResponseAnnotation;
    4. import com.onway.leaguer.center.server.controller.vo.TakeOutCallbackReqVO;
    5. import com.onway.leaguer.center.server.service.CallbackService;
    6. import com.onway.leaguer.center.server.thirdparty.tcsl.connection.TcslException;
    7. import io.swagger.annotations.Api;
    8. import io.swagger.annotations.ApiOperation;
    9. import lombok.extern.slf4j.Slf4j;
    10. import org.springframework.web.bind.annotation.PostMapping;
    11. import org.springframework.web.bind.annotation.RequestBody;
    12. import org.springframework.web.bind.annotation.RequestMapping;
    13. import org.springframework.web.bind.annotation.RequestParam;
    14. import org.springframework.web.bind.annotation.RestController;
    15. import javax.annotation.Resource;
    16. import javax.validation.Valid;
    17. /**
    18. * 描述:订单接口模块
    19. *
    20. */
    21. @Slf4j
    22. @Api(value = "OrderController", tags = {"订单接口模块"})
    23. @RestController
    24. @RequestMapping("/api/")
    25. public class OrderController {
    26. @Resource
    27. private OrderService orderService;
    28. @ResponseAnnotation
    29. @ApiOperation(value = "修改订单状态")
    30. @PostMapping("/order/status/update")
    31. public Ret updateOrderStatus(@Valid @RequestBody OrderReqVO reqVO) {
    32. orderService.updateOrderStatus(reqVO);
    33. return RetUtil.success();
    34. }
    35. }

    第三种、通过url拦截

    1. import com.alibaba.fastjson.JSONObject;
    2. import com.onway.leaguer.center.server.common.annotation.ResponseAnnotation;
    3. import com.onway.leaguer.center.server.common.utils.AESUtil;
    4. import lombok.extern.slf4j.Slf4j;
    5. import org.springframework.beans.factory.annotation.Value;
    6. import org.springframework.core.MethodParameter;
    7. import org.springframework.core.annotation.AnnotationUtils;
    8. import org.springframework.http.MediaType;
    9. import org.springframework.http.server.ServerHttpRequest;
    10. import org.springframework.http.server.ServerHttpResponse;
    11. import org.springframework.web.bind.annotation.ControllerAdvice;
    12. import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
    13. import java.lang.reflect.AnnotatedElement;
    14. /**
    15. * 描述:
    16. *
    17. */
    18. @ControllerAdvice(basePackages = "com.onway.leaguer.center.server.controller")
    19. @Slf4j
    20. public class EncodeResponseBodyAdvice implements ResponseBodyAdvice {
    21. @Override
    22. public boolean supports(MethodParameter methodParameter, Class aClass) {
    23. return true;
    24. }
    25. @Override
    26. public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
    27. //需要拦截的url信息,建议放到配置里面
    28. String urlStr = "/api/center/balance/card/available/list";
    29. //获取请求的url路径
    30. String path = serverHttpRequest.getURI().getPath();
    31. //对拦截数据处理
    32. if (path.equals(urlStr)) {
    33. return AESUtil.dataEncrypt(JSONObject.toJSONString(body));
    34. }
    35. return body;
    36. }
    37. }

  • 相关阅读:
    伦敦银如何选择最优的交易方法
    KT6368A蓝牙芯片认证客户FCC定频出现频偏的处理总结
    【Java】equals() 方法详解
    DDS层架构组成
    ssm大型商场移动导游系统的设计与实现毕业设计源码100853
    正则表达式replaceAll()方法具有什么功能呢?
    LiveData源码分析
    集合基础知识
    OpenGL-状态机 理解
    贝wa儿歌(安卓)
  • 原文地址:https://blog.csdn.net/u012406790/article/details/126782139