JAX-RS is perfect for implementing REST. What do you use to call REST services in Java? [closed]

StackOverflow https://stackoverflow.com/questions/923566

  •  06-09-2019
  •  | 
  •  

Question

Ideally, I am looking for something like JAX-RS (using annotations to describe the services I want to call), but allowing to call REST services implemented using other technologies (not JAX-RS). Any suggestion?

Was it helpful?

Solution

You wrote in a comment that you were "hoping for something more high level" than HttpClient. It sounds like Restlet would be perfect. It provides a high-level API for implementing and using RESTful web applications, with plug-and-play adapters for the lower-level implementations.

For example, to POST a webform to a resource using Restlet 1.1:

Client client = new Client(Protocol.HTTP);

Form form = new Form();
form.add("foo", "bar");
form.add("abc", "123");

Response response = client.post("http://host/path/to/resource", form.getWebRepresentation())

if (response.getStatus().isError()) {
    // deal with the error
    return;
}

if (response.isEntityAvailable()) {
    System.out.println(response.getEntity().getText());
}

If you need to set more options on the request, you can use a Request object:

Form form = new Form();
form.add("foo", "bar");
form.add("abc", "123");

Request request = new Request(Method.POST, "http://host/path/to/resource");

request.setEntity(form.getWebRepresentation());

request.setReferrerRef("http://host/path/to/referrer");

Response response = client.handle(request);

HTH!

OTHER TIPS

JAX-RS (JSR311) does not define a client API, but most JAX-RS implementations have one, like Jersey, RESTeasy or Apache CXF. The Restlet framework also has client support as well as a seperate HTTP Client extension.

Since these are specialized libraries/frameworks for REST, I'd suggest you look into these.

I suggest you take a look at the WADL project. WADL is to REST what WSDL is to SOAP.

You first need to define the REST interface using WADL. Then you can run the wadl2java tool on it to generate client stubs for the REST calls.

Once you have the WADL for a web service you can implement tools which do other things with it as well, e.g. generate server side stubs, generate documentation, etc.

HttpClient from apache mostly (see http://hc.apache.org/httpcomponents-client-ga/).

As Gandalf and Darrel Miller said, HttpClient.

You don't need to be constructing XML from HTTP params. XML should only be your interface between two separate systems. Using HttpClient puts you in Java with the String values at your disposal, goinn back into XML from there is needless.

If you are going back into XML at this point in order to send out to another system, you should be thinking about moving the values from the HTTP params back in to your domain/app logic before communicating with another system. This is necessary to keeps proper seperation of application logic from inter system communications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top