将对象列表提交到 apache-cxf jax-rs (REST) 服务时出错

发布于 2024-11-04 11:57:30 字数 808 浏览 0 评论 0原文

我想通过以下方式发布 JSON 中的客户列表:

@POST
@Path("/addCustomers/")
@Consumes(MediaType.APPLICATION_JSON)
public List<Customer> addCustomers(List<Customer> list){
logger.debug(list);
    return list;
}

Request Header:
Content-Type: application/json

Request Body:
{"Customer":[{"id":2999,"name":"Som Awasthi"},{"id":3000,"name":"Arnav Awasthi"}]}

Response: "415: Unsupported Media Type" error.

此请求的输入与我在 listCustomers 调用中得到的内容相同,如下所示:

@GET
@Path("/listCustomers")
public List<Customer> listCustomers(){
List<Customer> list = new ArrayList<Customer>();
list.add(new Customer("Som Awasthi", 2999L));
list.add(new Customer("Arnav Awasthi", 3000L));

return list;
}

所以我期望输入应该给我 List 对象。但它给了我不支持的媒体类型错误。

问候, 阿尔纳夫

I want to post List of Customers in JSON in the following way:

@POST
@Path("/addCustomers/")
@Consumes(MediaType.APPLICATION_JSON)
public List<Customer> addCustomers(List<Customer> list){
logger.debug(list);
    return list;
}

Request Header:
Content-Type: application/json

Request Body:
{"Customer":[{"id":2999,"name":"Som Awasthi"},{"id":3000,"name":"Arnav Awasthi"}]}

Response: "415: Unsupported Media Type" error.

Input to this request is same, what I have got in the listCustomers call, which is as follows:

@GET
@Path("/listCustomers")
public List<Customer> listCustomers(){
List<Customer> list = new ArrayList<Customer>();
list.add(new Customer("Som Awasthi", 2999L));
list.add(new Customer("Arnav Awasthi", 3000L));

return list;
}

So I expected that input should give me List object. But it is giving me Unsupported Media Type error.

Regards,
Arnav

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗼ふ静 2024-11-11 11:57:30

最后我通过nabble中的一次讨论找到了解决方案:

http://cxf.547215.n5.nabble.com/How-to-submit-JSON-data-as-request-body-in- Apache-CXF-jax-rs-REST-td4361669.html

总结为:
Apache-CXF 2.3.0 和 Jettison 1.2 不支持此功能。所以我不得不将版本更改为2.3.4并且它起作用了。

应用示例:
https://bitbucket.org/arnavawasthi/apache-cxf-jaxrs-spring

希望这能帮助其他面临同样问题的人。

谢谢,
阿尔纳夫

Finally I found the solution through one discussion in nabble:

http://cxf.547215.n5.nabble.com/How-to-submit-JSON-data-as-request-body-in-Apache-CXF-jax-rs-REST-td4361669.html

Summary is:
Apache-CXF 2.3.0 and Jettison 1.2 doesn't support this. So I had to change the version to 2.3.4 and it worked.

Sample Application:
https://bitbucket.org/arnavawasthi/apache-cxf-jaxrs-spring

Hope that will help the others facing the same problem.

Thanks,
Arnav

奶茶白久 2024-11-11 11:57:30

尝试在您的请求中添加 Accept 标头:

Accept: application/json

另外,请确保您的请求绝对是 POST。

编辑:

所以你传入的 json 是一个内部有数组的对象。尝试传递它,其中是客户对象的数组:

[{"id":2999,"name":"Som Awasthi"},{"id":3000,"name":"Arnav Awasthi"}]

Try adding an Accept header to your request:

Accept: application/json

Also, make sure your request is definitely a POST.

EDIT:

So the json you are passing in is an object with an array inside. Try passing this in which is an array of customer objects:

[{"id":2999,"name":"Som Awasthi"},{"id":3000,"name":"Arnav Awasthi"}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文