本文介绍了Web服务输入中的Java + Jersey实体列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要接收JSON实体列表作为对WS的输入.
I need to receive a list of JSON entity as input to my WS.
这是我的实体定义:
@XmlRootElement
public class ContactClass {
public String action;
public Long userId;
public String phone;
public String fbId;
}
这里是我的WS函数定义:
here my WS function definition:
@PUT
@Path("/{userId}/adBook")
public String synchAdBookContacts(@PathParam("userId") Long userId, ArrayList<ContactClass> contacts)
删除ArrayList<>效果很好,但是我需要一个ContactClass数组作为输入.
Removing ArrayList<> It works fine, but I need an array of ContactClass as input.
你能帮我吗?
谢谢!
更新:最后,我找到了解决方案,在这里找到了解决我的问题的文章: https://blogs.oracle.com/japod/entry/missing_brackets_at_json_one
Update:Finally I found the solution, here the article that have solved my issue:https://blogs.oracle.com/japod/entry/missing_brackets_at_json_one
推荐答案
Bean 1:
@XmlRootElement
public class Contact {
private String name;
private String phoneNumber;
// Getters, setters, default constructor
}
Bean 2:
@XmlRootElement
public class Contacts {
private List<Contact> contacts;
//Getter for contacts
@XMLElement(name = "listContacts")
public List<Contact> getContacts() {
....
// Getters, setters, default constructor
}
您的Json字段应采用以下格式:
You Json fiel should have the following format:
"listContacts":[{"json for contact1"},{"json for contact2"},{"json for contact3"}...]
您的资源:
@PUT
@Path("/{userId}/adBook")
public String synchAdBookContacts(@PathParam("userId") Long userId, Contacts contacts) {
//Here you can get your contacts contacts.
这篇关于Web服务输入中的Java + Jersey实体列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!