本文介绍了Spring REST多个@RequestBody参数可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've implemented a Spring RESTful web service. Using Jackson JSON for Object Mapping. I have a method that accepts two parameters.

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

客户端如何构造一个请求,其中要传递多个JSON对象在身体?

How would the client construct a request where in multiple JSON objects are to be passed in the body?

这可能吗?

- 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)

这篇关于Spring REST多个@RequestBody参数可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 22:19