一、自定义EncodeResponseBodyAdvice实现ResponseBodyAdvice
- package com.onway.interceptor;
-
- import com.alibaba.fastjson.JSONObject;
- import com.onway.leaguer.center.server.common.annotation.ResponseAnnotation;
- import com.onway.leaguer.center.server.common.utils.AESUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.MethodParameter;
- import org.springframework.core.annotation.AnnotationUtils;
- import org.springframework.http.MediaType;
- import org.springframework.http.server.ServerHttpRequest;
- import org.springframework.http.server.ServerHttpResponse;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
-
- import java.lang.reflect.AnnotatedElement;
-
- /**
- * 描述:
- *
- */
- @ControllerAdvice(basePackages = "com.onway.leaguer.center.server.controller")
- @Slf4j
- public class EncodeResponseBodyAdvice implements ResponseBodyAdvice {
-
- @Override
- public boolean supports(MethodParameter methodParameter, Class aClass) {
- //默认false
- boolean isIntercept = false;
-
- //第一种:通过注解拦截,拦截@ResponseAnnotation接口
- AnnotatedElement annotatedElement = methodParameter.getAnnotatedElement();
- ResponseAnnotation responseAnnotation = AnnotationUtils.findAnnotation(annotatedElement, ResponseAnnotation.class);
- if (responseAnnotation != null) {
- isIntercept = true;
- }
-
- //第二种:通过方法名称拦截,拦截指定方法
- Method method = methodParameter.getMethod();
- if ("saveOrder".equals(method.getName())) {
- isIntercept = true;
- }
-
- return isIntercept;
- }
-
- @Override
- public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
- if (null == body) {
- return null;
- }
-
- //对返回数据加密
- return AESUtil.dataEncrypt(JSONObject.toJSONString(body));
- }
-
- }
二、添加ResponseAnnotation
- import java.lang.annotation.*;
-
- /**
- * 描述:
- *
- */
- @Target({ElementType.METHOD})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface ResponseAnnotation {
-
- }
三、需要拦截的接口,添加@ResponseAnnotation注解
-
- import com.onway.dayu.api.Ret;
- import com.onway.dayu.api.RetUtil;
- import com.onway.leaguer.center.server.common.annotation.ResponseAnnotation;
- import com.onway.leaguer.center.server.controller.vo.TakeOutCallbackReqVO;
- import com.onway.leaguer.center.server.service.CallbackService;
- import com.onway.leaguer.center.server.thirdparty.tcsl.connection.TcslException;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.annotation.Resource;
- import javax.validation.Valid;
-
- /**
- * 描述:订单接口模块
- *
- */
- @Slf4j
- @Api(value = "OrderController", tags = {"订单接口模块"})
- @RestController
- @RequestMapping("/api/")
- public class OrderController {
-
- @Resource
- private OrderService orderService;
-
- @ResponseAnnotation
- @ApiOperation(value = "修改订单状态")
- @PostMapping("/order/status/update")
- public Ret updateOrderStatus(@Valid @RequestBody OrderReqVO reqVO) {
- orderService.updateOrderStatus(reqVO);
- return RetUtil.success();
- }
-
- }
第三种、通过url拦截
-
- import com.alibaba.fastjson.JSONObject;
- import com.onway.leaguer.center.server.common.annotation.ResponseAnnotation;
- import com.onway.leaguer.center.server.common.utils.AESUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.MethodParameter;
- import org.springframework.core.annotation.AnnotationUtils;
- import org.springframework.http.MediaType;
- import org.springframework.http.server.ServerHttpRequest;
- import org.springframework.http.server.ServerHttpResponse;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
-
- import java.lang.reflect.AnnotatedElement;
-
- /**
- * 描述:
- *
- */
- @ControllerAdvice(basePackages = "com.onway.leaguer.center.server.controller")
- @Slf4j
- public class EncodeResponseBodyAdvice implements ResponseBodyAdvice {
-
- @Override
- public boolean supports(MethodParameter methodParameter, Class aClass) {
- return true;
- }
-
- @Override
- public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
- //需要拦截的url信息,建议放到配置里面
- String urlStr = "/api/center/balance/card/available/list";
- //获取请求的url路径
- String path = serverHttpRequest.getURI().getPath();
- //对拦截数据处理
- if (path.equals(urlStr)) {
- return AESUtil.dataEncrypt(JSONObject.toJSONString(body));
- }
-
- return body;
- }
-
- }