Reputation: 1
I have this xml:
<order>
<id_address_delivery>4</id_address_delivery>
</order>
and java class
@JacksonXmlRootElement(localName = "order")
public static class Order
{
@JacksonXmlProperty(localName = "id_address_delivery")
public String id_address_delivery;
}
When I try to parse xml with code:
XmlMapper XmlMapper = new XmlMapper()
XmlMapper.readValue(xml, Order.class);
I getting the error : Can not deserialize instance of java.lang.String out of START_OBJECT token
How to correct deserialize xml element to primitive type?
Upvotes: 0
Views: 246
Reputation: 542
EDIT: Could you confirm that the variable "cls" is Order.class? I get the error you reported if cls is String.class but that is not the correct approach as you are parsing the <order> element not the <id_address_delivery> element.
Do you have any bespoke configuration that might be causing the problem? Perhaps dodgy whitespace - try removing the whitespace completely?
My code ...
public class Jackson {
@JacksonXmlRootElement(localName = "order")
private static class Order
{
@JacksonXmlProperty(localName = "id_address_delivery")
public String id_address_delivery;
}
public static void main(String[] args) throws IOException {
XmlMapper mapper = new XmlMapper();
Order order = mapper.readValue("<order>\n\t<id_address_delivery>4</id_address_delivery></order>", Order.class);
System.out.println(ToStringBuilder.reflectionToString(order, ToStringStyle.JSON_STYLE));
}
}
produces
{"id_address_delivery":"4"}
Upvotes: 1