deserialize xml to pojo using jackson xml mapper

后端 未结 1 1150
春和景丽
春和景丽 2020-12-21 22:38

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


 
        111-111-1111
          


        
相关标签:
1条回答
  • 2020-12-21 23:13

    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();
    
      }
    }
    
    0 讨论(0)
提交回复
热议问题