将对象列表提交到 apache-cxf jax-rs (REST) 服务时出错
我想通过以下方式发布 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后我通过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
尝试在您的请求中添加 Accept 标头:
另外,请确保您的请求绝对是 POST。
编辑:
所以你传入的 json 是一个内部有数组的对象。尝试传递它,其中是客户对象的数组:
Try adding an Accept header to your request:
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: