在我们的应用程序中,我们使用JSON进行请求和响应.控制器方法用@RequestBody()注释.返回的对象,例如TransferResponse.我想从@ResponseBody获取该对象.我已经设置了一个拦截器postHandle方法:
In our application we are using JSON for request and response. The controller methods are annotated with @RequestBody(). The object being returned e.g. TransferResponse. I would like to get hold of this object form the @ResponseBody. I have setup a interceptor postHandle method:
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws java.lang.Exception { .... }那么如何在此postHandle方法中获取JSON?
So how do I get the JSON in this postHandle method?
先谢谢了 总经理
推荐答案如 Pavel Horal 所述,调用postHandle()方法后,响应主体对象已转换为JSON并写入响应.您可以尝试编写自己的自定义注释和方面,以拦截控制器响应主体对象.
As Pavel Horal already mentioned, when postHandle() method is called, the response body object is already converted to JSON and written to response. You can try to write your own custom annotation and aspect in order to intercept the controller response body objects.
// custom annotation import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyCustomAnnotation { } // aspect import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class MyCustomAnnotationAspect { @Around(value = "@annotation(org.package.MyCustomAnnotation)", argNames = "pjp") public Object aroundAdvice(final ProceedingJoinPoint pjp) { // this is your response body Object responseBody = pjp.proceed(); return responseBody; } }使用@EnableAspectJAutoProxy
更多推荐
Spring MVC 3.2 @ResponseBody拦截器
发布评论