Spring MVC 3.2 @ResponseBody拦截器

编程入门 行业动态 更新时间:2024-10-23 17:37:49
本文介绍了Spring MVC 3.2 @ResponseBody拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我们的应用程序中,我们使用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拦截器

本文发布于:2023-11-24 21:13:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拦截器   MVC   Spring   ResponseBody

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!