HY,
我有一个由HashMap插入到ArrayList中的对象列表
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());
}
}
我的Rest服务通过以下注释为我提供了正确的信息
@GET
@Path("/listareF")
@Produces("application/xml")
public static UsersList listme(){
UsersList ul = new UsersList();
HashMap m = new HashMap();
for(Map.Entry<Integer, Users> utiliz : usersH.entrySet()){
// m = new HashMap();
m.put(utiliz.getKey(), utiliz.getValue());
//ul.setVariabila();
}
ul.setVariabila(m);
return ul;
}
JAXB:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
//@XmlRootElement(name ="USER")
public class Users {
private int userID;
private String userName;
private String userPass;
private String userRight;
public Users(int uI, String uN, String uP, String uR) {
this.userID = uI;
this.userName = uN;
this.userPass = uP;
this.userRight = uR;
}
public Users() {
}
public Users(int uI) {
this.userID = uI;
}
@XmlElement
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
@XmlElement
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@XmlElement
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
@XmlElement
public String getUserRight() {
return userRight;
}
public void setUserRight(String userRight) {
this.userRight = userRight;
}
包含根元素的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;
}
}
示例XML:
<?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>
unmarshaller:
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);
我在代码行上得到错误:HashMap ul2 =(HashMap)jaxbUnmarshaller.unmarshal(reader);
我也尝试过:UsersList ul2 =(UsersList)jaxbUnmarshaller.unmarshal(reader); 但是会发生同样的错误。
如何创建输出:
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);
线程中的异常&#34; main&#34;显示java.lang.NullPointerException at com.sun.xml.internal.bind.v2.runtime.reflect.Lister $ CollectionLister.addToPack(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.reflect.Lister $ CollectionLister.addToPack(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty $ ReceiverImpl.receive(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(未知来源) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source) 在javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(未知来源)
请帮助我,
此致
答案 0 :(得分:0)
我解决了这个问题,问题出在我的包装类上。我正在包装到一个ArrayList中,我正在使用Map。
解决方案:
@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;
}
}