Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling Restful Service from Java

Here I am not creating a RESTful service indeed I have to call an external Restful service from my java code. Currently I am implementing this using Apache HttpClient. The response that I get from the web service is in XML format. I need to extract the data from XML and put them on Java objects. Rather than using SAX parser, I heard that we can use JAX-RS and JERSEY which automatically maps the xml tags to corresponding java objects.

I have being looking through but unable to find a source to get started. I did look at existing links Consuming RESTful APIs using Java RESTful call in Java

Any help is appreciated in moving forward.

Thanks!!

like image 418
Rishi Avatar asked Aug 24 '11 14:08

Rishi


People also ask

Can we call REST API from Java?

You can definitely interact with RESTful web services by using URLConnection or HTTPClient to code HTTP requests. However, it's generally more desirable to use a library or framework which provides a simpler and more semantic API specifically designed for this purpose.


1 Answers

UPDATE

as follow up with this: Can I do this way?? if the xml being returned as 4 ..... If I am constructing a Person object, I believe this will choke up. Can I just bind only the xml elements that I want? if Yes how can I do that.

You could map this XML as follows:

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<Persons>
    <NumberOfPersons>2</NumberOfPersons>
        <Person>
            <Name>Jane</Name>
            <Age>40</Age>
        </Person>
        <Person>
            <Name>John</Name>
            <Age>50</Age>
        </Person>
</Persons> 

Persons

package forum7177628;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Persons")
@XmlAccessorType(XmlAccessType.FIELD)
public class Persons {

    @XmlElement(name="Person")
    private List<Person> people;

}

Person

package forum7177628;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

    @XmlElement(name="Name")
    private String name;

    @XmlElement(name="Age")
    private int age;

}

Demo

package forum7177628;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Persons.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Persons persons = (Persons) unmarshaller.unmarshal(new File("src/forum7177628/input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(persons, System.out);
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Persons>
    <Person>
        <Name>Jane</Name>
        <Age>40</Age>
    </Person>
    <Person>
        <Name>John</Name>
        <Age>50</Age>
    </Person>
</Persons>

ORIGINAL ANSWER

Below is an example of calling a RESTful service using the Java SE APIs including JAXB:

String uri =
    "http://localhost:8080/CustomerService/rest/customers/1";
URL url = new URL(uri);
HttpURLConnection connection =
    (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");

JAXBContext jc = JAXBContext.newInstance(Customer.class);
InputStream xml = connection.getInputStream();
Customer customer =
    (Customer) jc.createUnmarshaller().unmarshal(xml);

connection.disconnect();

For More Information:

  • http://blog.bdoughan.com/2010/08/creating-restful-web-service-part-55.html
like image 146
bdoughan Avatar answered Sep 29 '22 13:09

bdoughan