在java中从arraylist解组
Hy 我有一个通过HashMap插入ArrayList的对象列表在java中从arraylist解组,java,rest,jaxb,unmarshalling,Java,Rest,Jaxb,Unmarshalling,Hy 我有一个通过HashMap插入ArrayList的对象列表 private static Map<Integer, Users> usersH = new HashMap<Integer, Users>(); static { ArrayList<Users> users = new ArrayList(); UserImpl u = new UserImpl(); users =
private static Map<Integer, Users> usersH = new HashMap<Integer, Users>();
static {
ArrayList<Users> users = new ArrayList();
UserImpl u = new UserImpl();
users = u.getUsers();
//int j = users.size();
for(int i=0; i<users.size(); ++i){
Users u1 = new Users();
u1.setUserID(users.get(i).getUserID());
u1.setUserName(users.get(i).getUserName());
u1.setUserPass(users.get(i).getUserPass());
u1.setUserRight(users.get(i).getUserRight());
usersH.put(users.get(i).getUserID(), u1);
// System.out.println(usersH.toString());
}
}
包含根元素的Java类:
@XmlRootElement
public class UsersList {
ArrayList<Users> variabila;
public UsersList() {
}
/*public List<Users> getVariabila() {
return variabila;
}*/
public void setVariabila(HashMap<Integer,Users> collection) {
this.variabila = new ArrayList(collection.values());
}
@XmlElement(name="getusers")
public ArrayList<Users> getUsers() {
return variabila;
}
}
线程“main”java.lang.NullPointerException中出现异常
位于com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(未知源)
位于com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(未知源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(未知源)
位于com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(未知源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(未知源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(未知源)
位于com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(未知源代码)
位于com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.ScannedElement(未知来源)
位于com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(未知源)
在com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(未知来源)
位于com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(未知源)
在com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.ScandDocument(未知来源)
位于com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知源)
位于com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知源)
位于com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(未知源)
位于com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(未知源代码)
位于com.sun.org.apache.xerces.internal.jaxp.saxpasserimpl$jaxpsaxpasser.parse(未知源代码)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(未知源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(未知源)
位于javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(未知源)
请帮帮我
诚恳地说,我解决了这个问题,问题出在我的包装器类上。我正在包装成一个ArrayList,我正在使用一个地图 解决方案如下:
@XmlRootElement
public class UsersList {
Map<Integer,Users> variabila;
public UsersList() {
}
public void setVariabila(HashMap<Integer,Users> collection) {
this.variabila = new ArrayList(collection.values());
}
@XmlElement(name="getusers")
public ArrayList<Users> getUsers() {
return variabila;
}
}
@XmlRootElement
公共类用户列表{
瓦里比拉地图;
公共用户列表(){
}
公共void setVariabila(HashMap集合){
this.variabila=newArrayList(collection.values());
}
@XmlElement(name=“getusers”)
公共ArrayList getUsers(){
返回variabila;
}
}
需要代码来显示“输出”是如何创建/定义的。我添加了你要求的代码。你的setVariabila到底想实现什么。一旦调用该方法,就会丢失所有地图键。当一张地图不存在时,你到底希望如何神奇地向地图施法。如果您想要地图,那么为什么不让用户列表中的字段成为地图?你为什么要把它转换成一个列表呢。成功了:-)
@XmlRootElement
public class UsersList {
ArrayList<Users> variabila;
public UsersList() {
}
/*public List<Users> getVariabila() {
return variabila;
}*/
public void setVariabila(HashMap<Integer,Users> collection) {
this.variabila = new ArrayList(collection.values());
}
@XmlElement(name="getusers")
public ArrayList<Users> getUsers() {
return variabila;
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><usersList><getusers><userID>1</userID><userName>maracineanu</userName><userPass>12345</userPass><userRight>u</userRight></getusers></usersList>
StreamSource ss = new StreamSource(output);
JAXBContext jaxbContext = JAXBContext.newInstance(UsersList.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(output);
System.out.println(output);
// UsersList ul = new UsersList();
HashMap<Integer,Users> ul2 = (HashMap<Integer, Users>) jaxbUnmarshaller.unmarshal(reader);
System.out.println(ul2);
URL url = new URL("http://localhost:8081/Pictori/rest/weby/listareF");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//conn.
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/xml");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
System.out.println("Content type:" + conn.getContentType());
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output=br.readLine();
// while (output != null) {
// output = br.readLine();
// }
System.out.println(output);
StreamSource ss = new StreamSource(output);
@XmlRootElement
public class UsersList {
Map<Integer,Users> variabila;
public UsersList() {
}
public void setVariabila(HashMap<Integer,Users> collection) {
this.variabila = new ArrayList(collection.values());
}
@XmlElement(name="getusers")
public ArrayList<Users> getUsers() {
return variabila;
}
}