2011-04-20 167 views
38

我已经实现了一个Spring RESTful Web服务。使用Jackson JSON进行对象映射。我有一个接受两个参数的方法。Spring REST多个@RequestBody参数可能吗?

public Person createPerson(
    @RequestBody UserContext userContext, 
    @RequestBody Person person) 

客户端如何在多个JSON对象在主体中传递请求时构造一个请求?

这可能吗?

- 斯里兰卡

回答

48

我敢肯定,这将无法正常工作。可能有一个解决方法,但更简单的方法是引入一个包装对象并更改您的签名:

public class PersonContext{ 
    private UserContext userContext; 
    private Person person; 
    // getters and setters 
} 


public Person createPerson(@RequestBody PersonContext personContext) 
+0

谢谢,我正在考虑包装数组作为最后的手段。你身边的工作是指什么? – Sri 2011-04-20 07:13:42

+1

@Srirangan:workarround是PersonContext类 - 它是某种DTO – Ralph 2011-04-20 07:24:32

+0

@Srirangan我说*可能*是一种解决方法。我不知道有一个我自己 – 2011-04-20 07:32:46