2

While I try to bind xml response to POJO using jackson, I exepct to get values using getters. But I get the error an error. I tried searching about this but couldn't find relevant info. Most of folks suggested to use ignore. However I can't beause I am expecting that field. My pojos look like below.

public class Consumer {
    private String xmlns;
    private String version;
    private Info info;
    private Status status;

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Info getInfo() {
        return info;
    }

    public void setInfo(Info info) {
        this.info = info;
    }

    public String getXmlns() {
        return xmlns;
    }

    public void setXmlns(String xmlns) {
        this.xmlns = xmlns;
    }

    public String getVersion() {
        return version;
    }

    public class Info {
        private String state;
        private String id;
        private String shopId;
        private String time;

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getShopId() {
            return shopId;
        }

        public void setShopId(String shopId) {
            this.shopId = shopId;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }


    public class Status {
        private Response response;

        public Response getResponse() {
            return response;
        }

        public void setResponse(Response response) {
            this.response = response;
        }


    public class Response {
        private String value;
        private int statusCode;
        private String messageCode;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public int getStatusCode() {
            return statusCode;
        }

        public void setStatusCode(int statusCode) {
            this.statusCode = statusCode;
        }

        public String getMessageCode() {
            return messageCode;
        }

        public void setMessageCode(String messageCode) {
            this.messageCode = messageCode;
        }


    public class Temp {

        public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, JAXBException {
            readXml();
        }

        public static void readXml() throws IOException, SAXException, ParserConfigurationException, JAXBException {
            String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<Consumer xmlns=\"http://www.someurl.com\" version=\"1.5\">\n" +
                    "   <Info state=\"CA\">\n" +
                    "      <Id>160c464da1ad89a2fec50436</Id>\n" +
                    "      <ShopId>1234</ShopId>\n" +
                    "      <time>2019-08-22T23:13:47.909-04:00</time>\n" +
                    "   </Info>\n" +
                    "   <Status>\n" +
                    "      <Response value=\"Success\" statusCode=\"3456\" messageCode=\"Request processed successfully\" />\n" +
                    "   </Status>\n" +
                    "</Consumer>";

            XmlMapper xmlMapper = new XmlMapper();
            Consumer consumer = xmlMapper.readValue(xmlString, Consumer.class);

            System.out.println("Shop id "+consumer.getInfo().getShopId());

        }

    Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Info" (class com.motivatedmind.api.stackoverflow.Consumer), not marked as ignorable (3 known properties: "version", "xmlns", "info"])
     at [Source: (StringReader); line: `enter code here`3, column: 21] (through reference chain: com.motivatedmind.api.stackoverflow.Consumer["Info"])
        at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
        at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
        at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4014)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3005)
Motivated Mind
  • 108
  • 1
  • 9

1 Answers1

0

I suspect the problem is in XML string itself.

Change the <Consumersumer> to <Consumer>, that should work.

----------------------- AFTER UPDATE---------

Why the state is passes as attribute to XML tag 'Info'..? It should match the children of <Info> as instance values of your POJO class Info.java.

State is instance variable there in POJO, but is not child tag in <info> tag.

String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<Consumer xmlns=\"http://www.someurl.com\" version=\"1.5\">\n" +
            "   <Info>\n" +
            "      <State>CA</State>\n" +
            "      <Id>160c464da1ad89a2fec50436</Id>\n" +
            "      <ShopId>1234</ShopId>\n" +
            "      <time>2019-08-22T23:13:47.909-04:00</time>\n" +
            "   </Info>\n" +
            "   <Status>\n" +
            "      <Response value=\"Success\" statusCode=\"3456\" messageCode=\"Request processed successfully\" />\n" +
            "   </Status>\n" +
            "</Consumer>";

Try this..!!

----------------- AFTER UPDATE in Comments---------

As you are getting the data (xml string) from some other service.. You can change your model object to match with xml data.

For this, you can use annotation @XmlAttribute on your setter method of state field in pojo.

For more reference, you can see this answer

miiiii
  • 1,580
  • 1
  • 16
  • 29
  • Hey Thanks. That's how i am receiving the resposne from the api[State as an attribute]. All I am trying to do is consume the response as is received from api. – Motivated Mind Aug 23 '19 at 11:29
  • I tried below 2 ways. But no luck it says same error @JacksonXmlProperty(isAttribute = true) public String getState() { return state; } @javax.xml.bind.annotation.XmlAttribute public String getState() { return state; } – Motivated Mind Aug 23 '19 at 11:56