如何连接到Spring的@RequestBody参数解析,以使用请求的主体解析我自己的参数 [英] How can I hook into Spring's @RequestBody argument resolving to resolve my own argument using the body of the request

查看:40
本文介绍了如何连接到Spring的@RequestBody参数解析,以使用请求的主体解析我自己的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发应用程序的过程中,我们发现自己在控制器中执行了以下操作:

In the process of developing our app, we found ourselves doing something like this in our controller:

@RequestMapping(value = "foo", method = RequestMethod.POST)
@ResponseBody
public SimpleMasterWidget doStuff(@RequestBody ClientData clientData, ServerData serverData) throws Exception
{
        pushClientDataToServerData(clientData, serverData);
        //stuff specific to this operation on the serverData
        return whatever;
}

pushClientDataToServerData(clientData,serverData); 调用在每个POST请求中重复.现在,我知道确实不需要将clientData推送到serverData,但是有一些遗留代码迫使我们执行此操作.

The pushClientDataToServerData(clientData, serverData); call gets duplicated in every POST request. Now, I understand that there really shouldn't be any need to push clientData to serverData, but there is legacy code that forces us to do this.

当前,我们正在实现 HandlerMethodArgumentResolver 接口来解析 ServerData 参数.要删除所有重复的 pushClientDataToServerData 调用,我希望能够使用已插入其中的 ClientData 来解析 ServerData 参数.

Currently we are implementing the HandlerMethodArgumentResolver interface to resolve the ServerData argument. To remove all the duplicated pushClientDataToServerData calls I want to be able to resolve the ServerData argument with the ClientData already pushed into it.

现在在参数解析器中,我可以访问 NativeWebRequest ,因此我可以从那里获取请求正文,然后对它进行反序列化.但是,我不想用 @RequestBody 注释重新实现Spring已经做的事情.

Now in the argument resolver, I have access to the NativeWebRequest so I could probably get the request body through there and deserialize it and all that. However, I don't want to have to reimplement what Spring is already doing with the @RequestBody annotation.

所以我的问题可以归结为:有一种方法可以调用参数解析器中Spring的内容,以便获得反序列化的 ClientData 对象并调用 pushClientDataToServerData 就在参数解析器中吗?然后,所有控制器方法都将如下所示:

So my question boils down to this: Is there a way I can call into Spring's stuff in the argument resolver so that I can get the deserialized ClientData object and call pushClientDataToServerData right within the argument resolver? Then all the controller methods would just look like this:

@RequestMapping(value = "foo", method = RequestMethod.POST)
@ResponseBody
public SimpleMasterWidget doStuff(ServerData serverData) throws Exception
{
        //stuff specific to this operation on the serverData
        return whatever;
}

pushClientDataToServerData 调用将只放在一个位置.

And the pushClientDataToServerData call would be in only one place.

谢谢.

推荐答案

看起来像是Spring AOP的理想人选.下面只是切入点的示例,您应该对其进行改进以捕获所有可能的方法:

Looks like a good candidate for Spring AOP. Below is just an example of pointcut, you should improve it to catch all possible methods:

@Component
@Aspect
public class PushClientDataAspect {

    @Before(value = "execution(* com.xyz.myapp.dao.*.*(..) && args(clientData, serverData) && @annotation(org.springframework.web.bind.annotation.ResponseBody))")
    public void pushClientData(ClientData clientData, ServerData serverData) {
        pushClientDataToServerData(clientData, serverData);
    }   

}

这篇关于如何连接到Spring的@RequestBody参数解析,以使用请求的主体解析我自己的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆