springBoot对返回结果,进行统一处理---ResponseBodyAdvice

ResponseBodyAdvice 是对返回结果进行处理
springBoot对返回结果,进行统一处理---ResponseBodyAdvice_第1张图片

package com.hq.zhuanche.redisweb.advice;

import java.lang.reflect.Method;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@SuppressWarnings(“rawtypes”)
@RestControllerAdvice
//使用@ControllerAdvice注解也可以
public class ResponseAdvice implements ResponseBodyAdvice{

@Override
public Object beforeBodyWrite(Object arg0, MethodParameter arg1,
		MediaType arg2, Class arg3, ServerHttpRequest arg4,
		ServerHttpResponse arg5) {
	// TODO Auto-generated method stub
	//System.out.println(arg0.toString());
	return arg0.toString()+"|处理过";
}

/**
 * 
 * 拦截策略的配置
 * 
 */
@Override
public boolean supports(MethodParameter arg0, Class arg1) {
	// TODO Auto-generated method stub
	Method method = arg0.getMethod();
	String methodName = method.getName();
	//方法名为login的,不进行拦截
	if("login".equals(methodName)){
		return false;
	}else{
		return true;
	}
}

}

你可能感兴趣的:(springBoot)