xmpp 서버의 응답 일부를 Java 객체에 매핑하는 데 Jaxb unmarshaller를 사용하고 있습니다.@XmlElement는 네임 스페이스가 없을 때 null입니다.
Test.java
public class Test {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(MucUser.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
ByteArrayInputStream bais = new ByteArrayInputStream((
"<x xmlns='http://jabber.org/prot" +
"ocol/muc#user'><item role='moderator'" +
" affiliation='owner'/></x>").getBytes());
MucUser mucUser = (MucUser) unmarshaller.unmarshal(bais);
System.out.println(mucUser.getItem());
}
}
MucUser.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(namespace = "http://jabber.org/protocol/muc#user", name = "x")
public class MucUser {
@XmlElement(name = "item")
private Item item;
// getter and setter without annotations
}
Item.java는
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
@XmlAttribute(name = "role")
private String role;
@XmlAttribute(name = "affiliation")
private String affiliation;
// getters and setters without annotations
}
나는 ObjectFactory.java와 package-info.java를 사용하지 마십시오. 필드 item
이 비 정렬 화되지 않고 mucUser.getItem()
이 null을 반환합니다. 그러나 명시 적으로 네임 스페이스 tst
을 Test.java의 요소 <item>
에 추가하고 MuchUser.java가 주석 @XmlElement(name = "item", namespace="tst")
을 사용하도록 수정하면 제대로 작동합니다!
그러나 내 의지로 문자열을 수정할 수는 없으며 프로토콜의 일부입니다.
내 MucUser 클래스에 item
을 어떻게 올바르게 매핑해야합니까?
감사합니다. @MmlElement (name = "item", namespace = "http://jabber.org/protocol/muc#user")'''에''MucUser.java'''에서 주석을 수정하고 시작했습니다. 예상대로 작동합니다. – Alexey