ziggy
ziggy

Reputation: 15876

Is it possible to call a Restful web service from an EJB2 client

I have been looking around for examples of how to call a Restful service written in Spring 3 from an EJB2 client. If i understand REST correctly, it should not matter what technology/language the service is written in so i should be able to call the service from an EJB2 client.

I couldn't find a simple example or reference to guide me on how to implement an EJB2 client that can call a restful service. Does this mean that is not possible to call a Restful service from an EJB2 client? If it is possible, could you please point me to a document or example that shows or describe how the two can interface/talk to each other.

Most of the references/documentation i come across are related to how to expose an EJB as a web service whereas i am interested in how to call a web service from an EJB2.

I am particularly interested in how i can send an XML document to the service. For example, is it possible to use a Jersey client and JAXB with EJB2 and how would i pass the unmarshalled XML over HTTP using EJB2?

Thanks in advance.

Upvotes: 3

Views: 3929

Answers (1)

bdoughan
bdoughan

Reputation: 149047

Below are a couple of programmatic options for accessing a RESTful service in Java.

Using JDK/JRE APIs

Below is an example of calling a RESTful service using the APIs in the JDK/JRE

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();

Using Jersey APIs

Most JAX-RS implementations include APIs that make accessing RESTful services easier. A client API is being included in the JAX-RS 2 specification.

import java.util.List;
import javax.ws.rs.core.MediaType;
import org.example.Customer;
import com.sun.jersey.api.client.*;

public class JerseyClient {

    public static void main(String[] args) {
        Client client = Client.create();
        WebResource resource = client.resource("http://localhost:8080/CustomerService/rest/customers");

        // Get response as String
        String string = resource.path("1")
            .accept(MediaType.APPLICATION_XML)
                .get(String.class);
        System.out.println(string);

        // Get response as Customer
        Customer customer = resource.path("1")
            .accept(MediaType.APPLICATION_XML)
                .get(Customer.class);
        System.out.println(customer.getLastName() + ", "+ customer.getFirstName());

        // Get response as List<Customer>
        List<Customer> customers = resource.path("findCustomersByCity/Any%20Town")
            .accept(MediaType.APPLICATION_XML)
                .get(new GenericType<List<Customer>>(){});
        System.out.println(customers.size());
    }

}

For More Information

Upvotes: 4

Related Questions