简体   繁体   English

Spring REST 多个@RequestBody 参数,可能吗?

[英]Spring REST multiple @RequestBody parameters, possible?

I've implemented a Spring RESTful web service.我已经实现了一个 Spring RESTful Web 服务。 Using Jackson JSON for Object Mapping.使用 Jackson JSON 进行对象映射。 I have a method that accepts two parameters.我有一个接受两个参数的方法。

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

How would the client construct a request where in multiple JSON objects are to be passed in the body?客户端将如何构造一个请求,其中多个 JSON 对象将在正文中传递?

Is this possible?这可能吗?

-- Sri -- 斯里

I'm pretty sure that won't work.我很确定那行不通。 There may be a workaround, but the much easier way would be to introduce a wrapper Object and change your signature:可能有一种解决方法,但更简单的方法是引入包装对象并更改您的签名:

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


public Person createPerson(@RequestBody PersonContext personContext)

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

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