I am getting the following exception:
Unrecognized field "" (class my.package.RequestParameter), not marked as ignorable (2 known properties: "value", "name"])
at [Source: (StringReader); line: 1, column: 64] (through reference chain: my.package.RequestParameter[""])
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" (class my.package.RequestParameter), not marked as ignorable (2 known properties: "value", "name"])
when I try to deserialize the following entry:
"<requestParameter name=\"myId\">5482973821</requestParameter>"
to my target Pojo:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"value"
})
@Getter
@Setter
@NoArgsConstructor
public class RequestParameter {
@XmlValue
protected String value;
@XmlAttribute(name = "name")
protected String name;
}
using:
RequestParameter requestParameter = new XmlMapper().readValue(in, RequestParameter.class);
Does anyone know what I am missing? Thank you for your attention.
CodePudding user response:
You can use the JacksonXmlText annotation over your value field in your class:
@Getter
@Setter
@ToString
@NoArgsConstructor
public class RequestParameter {
@XmlValue
@JacksonXmlText //<-- the new annotation
protected String value;
@XmlAttribute(name = "name")
protected String name;
}
RequestParameter requestParameter = mapper.readValue(msg, RequestParameter.class);
//it will print RequestParameter(value=5482973821, name=myId)
System.out.println(requestParameter);
Note that if you want to serialize it you will not obtain the same starting xml, but it was the same scenario with the code you posted because of the name field that will be serialized in a different way from the original xml.
