pom.xml添加依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
spring.mvc添加配置
首先是最顶层beans:
xmlns:aop="http://www.springframework.org/schema/aop"
在已有的依赖中添加:
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
缺少xmlns或者xsi:schemalocation都会报错
拦截代码
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.converter.json.MappingJacksonValue;
public class ResponseInterceptor implements MethodInterceptor{
//private final static ObjectMapper jsonMapper=new ObjectMapper();
@Override
public Object invoke(MethodInvocation invocation) throws Throwable{
Object result=invocation.proceed();
System.out.println(invocation.getMethod());
MappingJacksonValue value=new MappingJacksonValue(result);
value.setJsonpFunction("callback"); //怎么获取回调函数?待解决
//随意处理,result就是返回结果
System.out.println(result);
return result;
}
}