RequestBodyAdvice,对所有以@RequestBody的参数的方法进行增强
/**
* 对所有以@RequestBody的参数的方法进行增强
*/
@Slf4j
@ControllerAdvice("com.haier.hopm.biz.service.controller")
public class DateTimeRequestBodyAdvice implements RequestBodyAdvice {
@Autowired
private RedisUtil redisUtil;
//此处如果返回false , 则不执行当前Advice的业务
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
I18nTimeHandle i18nTimeHandle = methodParameter.getMethodAnnotation(I18nTimeHandle.class);
//controller上有 I18nTimeHandle 这个注解的方法才进行处理
if (i18nTimeHandle != null) {
return true;
}
return false;
}
/**
* @title 读取参数前执行
* @description 在此做些编码 / 解密 / 封装参数为对象的操作
*/
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return httpInputMessage;
}
/**
* @title 读取参数后执行
* @author yuchen
*/
@Override
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter,
Type type, Class<? extends HttpMessageConverter<?>> aClass) {
String responesBody = JSONObject.toJSONStringWithDateFormat(o, JSON.DEFFAULT_DATE_FORMAT);
//获取请求json中时间字符串
List<String> jsonStrDateStrList = DateZoneUtil.getJsonStrDateStr(responesBody);
if (CollectionUtil.isEmpty(jsonStrDateStrList)) {
return o;
}
// log.info("RequestBody-处理前:{}", responesBody);
//获取的当前国家所在时区,如果Redis查不到,则默认 +8
String timeZone = redisUtil.get(Constants.COUNTRY_TIME_ZONE + UserUtil.getCountryCode());
String sourceTimeZone = StrUtil.isEmpty(timeZone) ? "+8" : timeZone;
for (String originDate : jsonStrDateStrList) {
//将前端传入的时间 转换成 东八区的时间
String targetDate = DateZoneUtil.converDateGMT(originDate, sourceTimeZone, "+8");
responesBody = StrUtil.replace(responesBody, originDate, targetDate);
}
// log.info("RequestBody-处理后:{}", responesBody);
return JSONObject.parseObject(responesBody, type);
}
/**
* @title 无请求时的处理
*/
@Override
public Object handleEmptyBody(Object body, HttpInputMessage httpInputMessage, MethodParameter methodParameter,
Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return body;
}
}
ResponseBodyAdvice ,对controller的响应体进行增强
/**
* 对controller的响应体进行增强
*/
@Slf4j
@ControllerAdvice("com.haier.hopm.biz.service.controller")
public class DateTimeResponseBodyAdvice implements ResponseBodyAdvice {
@Autowired
private RedisUtil redisUtil;
//此处如果返回false , 则不执行当前Advice的业务
@Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
return true;
}
@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass,
ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
String responesBody = JSONObject.toJSONStringWithDateFormat(o, JSON.DEFFAULT_DATE_FORMAT);
//获取请求json中时间字符串
List<String> jsonStrDateStrList = DateZoneUtil.getJsonStrDateStr(responesBody);
if (CollectionUtil.isEmpty(jsonStrDateStrList)) {
return o;
}
// log.info("ResponseBody-处理前:{}", responesBody);
//获取的当前国家所在时区,如果Redis查不到,则默认 +8
String timeZone = redisUtil.get(Constants.COUNTRY_TIME_ZONE + UserUtil.getCountryCode());
String sourceTimeZone = StrUtil.isEmpty(timeZone) ? "+8" : timeZone;
for (String originDate : jsonStrDateStrList) {
//将前端传入的时间 转换成 东八区的时间
String targetDate = DateZoneUtil.converDateGMT(originDate, "+8", sourceTimeZone);
responesBody = StrUtil.replace(responesBody, originDate, targetDate);
}
// log.info("ResponseBody-处理后:{}", responesBody);
return JSONObject.parseObject(responesBody);
}
}