Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

java - deserialize xml to pojo using jackson xml mapper

I am using Jackson XML mapper to deserialize XML to POJO. The XML looks like

<person>
 <agency>
        <phone>111-111-1111</phone>
 </agency>
</person>

And my class looks like

class Person
{
 @JacksonXmlProperty(localName="agency", namespace="namespace")
 private Agency agency;
 //getter and setter
}
class Agency
{
 @JacksonXmlElementWrapper(useWrapping = false)
 @JacksonXmlProperty(localName="phone", namespace="namespace")
 private List<AgencyPhone> phones;
 //getter and setter
}
class AgencyPhone
{
  private Phone phone;
  //getter and setter
}
class Phone
{
 private String number;
 //getter and setter
}

I want to set the phone number to number in Phone class. I cannot change XML or the way the class has been structured. I am getting Cannot construct instance of resolved.agency.AgencyPhone error and I created a AgencyPhone constructor

class AgencyPhone{
{
  private Phone phone;
  public AgencyPhone(Phone phone)
  {
      this.phone = phone;
   }
  }

But that did not work. So how to deserialize to nested instances.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can write your own custom deserialiser to achieve this. Here is the code to get you started:

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;

public class Test {
  public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
    XmlMapper mapper = new XmlMapper();
    final SimpleModule module = new SimpleModule("configModule",   com.fasterxml.jackson.core.Version.unknownVersion());
    module.addDeserializer(Person.class, new DeSerializer());
    mapper.registerModule(module);
    // Person readValue = mapper.readValue(<xml source>);
  }
}

class DeSerializer extends StdDeserializer<Person> {

  protected DeSerializer() {
    super(Person.class);
  }

  @Override
  public Person deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    // use p.getText() and p.nextToken to navigate through the xml and construct Person object
    return new Person();

  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...