[英]Not able to map field in JAXB unmarshalling, fields getting populated as null
I have an XML that I would like to convert to a POJO using JAXB but I am not able to and all the elements within the parent class are getting populated as null
.我有一个 XML,我想使用 JAXB 将其转换为 POJO,但我无法转换,并且父类中的所有元素都填充为
null
。
Input XML:输入 XML:
<Response>
<parameters>
<Id>101</Id>
<Status>SUCCESS</Status>
</parameters>
</Response>
FileWriterService.writeMethod() FileWriterService.writeMethod()
JAXBContext context = JAXBContext.newInstance(Response.class);
Response response = (Response) context.createUnmarshaller().unmarshal(new
StringReader(inputXml));
System.out.println("response: " + response);
Output:输出:
response: ClassPojo [parameters = ClassPojo [Id = null, Status = null]]
Response.java响应.java
@XmlRootElement(name="Response")
public class Response {
private Parameters parameters;
public Parameters getParameters() {
return parameters;
}
public void setParameters(Parameters parameters) {
this.parameters = parameters;
}
@Override
public String toString() {
return "ClassPojo [parameters = " + parameters + "]";
}
}
Parameters.java参数.java
public class Parameters {
private String Id;
private String Status;
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id = Id;
}
public String getStatus() {
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}
@Override
public String toString() {
return "ClassPojo [Id = " + Id + ", Status = " + Status + "]";
}
}
Try with @XmlElement annotation, this annotation maps a field to an xml element尝试使用@XmlElement 批注,此批注将字段映射到 xml 元素
Response.java响应.java
@XmlElement
private Parameters parameters;
Parameters.java参数.java
@XmlElement
private String Id;
@XmlElement
private String Status;
Thanks for the pointer Lakshan.感谢拉克山的指点。 After implementing your solution the original problem got resolved but got error related
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
实施您的解决方案后,原始问题得到解决,但出现错误相关
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
In addition to your changes I had to add XmlAccessorType
as well:除了您的更改之外,我还必须添加
XmlAccessorType
:
@XmlRootElement(name = "Response")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.