Home Converting JAVA Rest API XML response to JAVA object
Reply: 0

Converting JAVA Rest API XML response to JAVA object

user6475
1#
user6475 Published in April 23, 2018, 9:25 am

This is my XML file

<TrafficCasesResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Status>Successful</Status>
    <StatusDesc>
        5 CASE FOUND FOR B624-558-90-832-0 DRIVER LICENSE ON DATABASE
    </StatusDesc>
    <UnitsBalance>-30</UnitsBalance>
    <IPAddress>38.126.209.148</IPAddress>
    <CasesList>
        <Cases>
            <Case>
                <DriverLicenseNum>XYZ</DriverLicenseNum>
                <DefendantName>ABC</DefendantName>
                <DoB>09/12/1990</DoB>
                <CaseNumber>A8L8UTF</CaseNumber>
                <CaseType>I</CaseType>
                <ActionCode>11</ActionCode>
                <ActionDescription>D6 TO TALLAHASSE</ActionDescription>
                <AmountDue>293.00</AmountDue>
                <IssueDate>02/22/2018</IssueDate>
                <ViolationCode>3160751C1M</ViolationCode>
                <ViolationDescription>FAI/TO STOP LITE</ViolationDescription>
            </Case>
            <Case>
                <DriverLicenseNum>XYZ</DriverLicenseNum>
                <DefendantName>MEGAN VICTORI BROCCOLI</DefendantName>
                <DoB>09/12/1990</DoB>
                <CaseNumber>A2F6LNP</CaseNumber>
                <CaseType>I</CaseType>
                <ActionCode>15</ActionCode>
                <ActionDescription>CLOSED</ActionDescription>
                <AmountDue>0</AmountDue>
                <IssueDate>11/05/2017</IssueDate>
                <ViolationCode>320073A</ViolationCode>
                <ViolationDescription>REG EXP/LESS/6MO</ViolationDescription>
            </Case>
            <Case>
                <DriverLicenseNum>B624558908320</DriverLicenseNum>
                <DefendantName>ABC</DefendantName>
                <DoB>09/12/1990</DoB>
                <CaseNumber>A8KREW</CaseNumber>
                <CaseType>I</CaseType>
                <ActionCode>11</ActionCode>
                <ActionDescription>D6 TO TALLAHASSE</ActionDescription>
                <AmountDue>410.20</AmountDue>
                <IssueDate>10/27/2017</IssueDate>
                <ViolationCode>3160751C1M</ViolationCode>
                <ViolationDescription>FAI/TO STOP LITE</ViolationDescription>
            </Case></ReturnDescription>
                </CasesList>
            </TrafficCasesResponse>
        </CasesList>
    </TrafficCasesResponse>

This is my POJO object

package com.model;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@XmlRootElement(name ="TrafficCasesResponse")
@JsonIgnoreProperties(ignoreUnknown=true)
public class TrafficCasesResponse
{
    private String UnitsBalance;

    private String Status;

    private CasesList CasesList;

    private String StatusDesc;

    private String IPAddress;

    public String getUnitsBalance ()
    {
        return UnitsBalance;
    }

    public void setUnitsBalance (String UnitsBalance)
    {
        this.UnitsBalance = UnitsBalance;
    }

    public String getStatus ()
    {
        return Status;
    }

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

    public CasesList getCasesList ()
    {
        return CasesList;
    }

    public void setCasesList (CasesList CasesList)
    {
        this.CasesList = CasesList;
    }

    public String getStatusDesc ()
    {
        return StatusDesc;
    }

    public void setStatusDesc (String StatusDesc)
    {
        this.StatusDesc = StatusDesc;
    }

    public String getIPAddress ()
    {
        return IPAddress;
    }

    public void setIPAddress (String IPAddress)
    {
        this.IPAddress = IPAddress;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [UnitsBalance = "+UnitsBalance+", Status = "+Status+", CasesList = "+CasesList+", StatusDesc = "+StatusDesc+", IPAddress = "+IPAddress+"]";
    }

}

And this my REST Clent API

package com.client;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.model.*;


@Component
public class CallRestService implements CommandLineRunner {

    private static void CallRestService() {
        RestTemplate restTemplate =new RestTemplate();
        TrafficCasesResponse root = restTemplate.getForObject("http://localhost:8000/demo.xml", TrafficCasesResponse.class);

        System.out.println(root.getIPAddress());        

    }

     @Override
     public void run(String...args) throws Exception{
         CallRestService();
        }
}

When I'm trying to access this XML response I'm getting a null response:

2018-04-16 11:53:52.141[0;39m [32mDEBUG[0;39m [35m2052[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.web.client.RestTemplate [0;39m [2m:[0;39m Created GET request for "http://localhost:8000/demo.xml" [2m2018-04-16 11:53:52.188[0;39m [32mDEBUG[0;39m [35m2052[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.web.client.RestTemplate [0;39m [2m:[0;39m Setting request Accept header to [application/xml, text/xml, application/json, application/+xml, application/+json] [2m2018-04-16 11:53:52.204[0;39m [32mDEBUG[0;39m [35m2052[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.web.client.RestTemplate [0;39m [2m:[0;39m GET request for "http://localhost:8000/demo.xml" resulted in 200 (OK) [2m2018-04-16 11:53:52.204[0;39m [32mDEBUG[0;39m [35m2052[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.web.client.RestTemplate [0;39m [2m:[0;39m Reading [class com.model.TrafficCasesResponse] as "application/xml" using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@68a78f3c] null

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.36175 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO