简体   繁体   English

如何连接到Spring的@RequestBody参数解析,以使用请求的主体解析我自己的参数

[英]How can I hook into Spring's @RequestBody argument resolving to resolve my own argument using the body of the request

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;
}

The pushClientDataToServerData(clientData, serverData); pushClientDataToServerData(clientData, serverData); call gets duplicated in every POST request. 呼叫在每个POST请求中重复。 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. 现在,我了解到确实不需要将clientData推送到serverData,但是有一些遗留代码迫使我们执行此操作。

Currently we are implementing the HandlerMethodArgumentResolver interface to resolve the ServerData argument. 当前,我们正在实现HandlerMethodArgumentResolver接口来解析ServerData参数。 To remove all the duplicated pushClientDataToServerData calls I want to be able to resolve the ServerData argument with the ClientData already pushed into it. 要删除所有重复的pushClientDataToServerData调用,我希望能够将ClientData已经推送到其中的ServerData参数进行解析。

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. 现在,在参数解析器中,我可以访问NativeWebRequest因此我可以从那里获取请求主体并反序列化所有内容。 However, I don't want to have to reimplement what Spring is already doing with the @RequestBody annotation. 但是,我不需要重新实现Spring已经使用@RequestBody注释执行的操作。

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? 因此,我的问题可以归结为:有没有一种方法可以在参数解析器中调用Spring的内容,以便可以在参数解析器中获取反序列化的ClientData对象并调用pushClientDataToServerData 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;
}

And the pushClientDataToServerData call would be in only one place. 并且pushClientDataToServerData调用将只放在一个位置。

Thanks in advance. 提前致谢。

Looks like a good candidate for Spring AOP. 看起来是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);
    }   

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM