JAX-RS is perfect for implementing REST. What do y

2019-03-27 10:18发布

问题:

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?

回答1:

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!



回答2:

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.



回答3:

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.



回答4:

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



回答5:

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.