@ControllerAdvice
public class responAop implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
return true;
}
@Override
public Object beforeBodyWrite(Object resBody, MethodParameter arg1, MediaType arg2, Class aClass, ServerHttpRequest req,
ServerHttpResponse resp) {
String requestUri = req.getURI().getPath();
Object dataedPermission = null;
if (requestUri.contains("/test/list")) {//拦截的路径
dataedPermission = handledata(JSONObject.toJSONString(resBody, SerializerFeature.WriteMapNullValue));
}
return dataedPermission;
}
private Object handledata (String jsonStr){
jsonStr = replaceSpecialStr(jsonStr, "money");//money为需要替换的属性
return jsonStr;
}
public static String replaceSpecialStr (String orgStr, String fieldName){
String beginChar = "\"" + fieldName + "\":";
String newValue = "\"***\"";
String newStr = orgStr.replaceAll(beginChar + "([-]{0,1}[0-9.]{1,}(E-8){0,1})", beginChar + newValue);
return newStr;
}
}
如何在aop实现返回值替换成新值
猜你喜欢
转载自blog.csdn.net/linyiwwy/article/details/131741736
今日推荐
周排行