Search code examples
jacksonfasterxmlxmlmapper

Issue while converting simple xml to java object using jackson faster XmlMapper


I have a simple xml which looks like this:

<token>
  <merchant>MyMerchant</merchant>
</token>

this xml I want to convert into the following class:

@JsonTypeName( value = "token" )
public class Token{
  @JacksonXmlProperty( localName = "merchant")
  private String merchant;
}

I'm using Jackson version 2.3.2 with the following code:

   String simpleTokenXml = 
       "<token><merchant>MyMerchant</merchant></token>";
    XmlMapper xmlMapper = new XmlMapper(  );
    Token token = xmlMapper.readValue(simpleTokenXml, Token.class);

following exception is thrown:

com.fasterxml.jackson.databind.JsonMappingException: Could not resolve type id 'merchant' into a subtype of [simple type, class data.model.Token]

If I try to convert the Token Object into the xml the XmlMapper returns a xml String like that:

<Token xmlns="">
<token>
    <merchant>MyMerchant</merchant>
</token>
</Token>

How can I configure ObjectMapper to ignore the ObjectNode? I need my mapper to understands my xml without doing some string operations on it. Thanks for help.


Solution

  • I solved the issue. The problem was that a parent class of the Token class contained the annotation @JsonTypeInfo(use = Id.NAME). After set the use to NONE everything works.