Rest clients for Java?

With JSR 311 and it's implementations we have a powerful standard for exposing Java objects via Rest. However on the client side there seems to be something missing that is comparable to Apache Axis for SOAP - something that hides the web service and marshals the data transparently back to Java objects.

How do you create Java RESTful clients? Using HTTPConnection and manual parsing of the result? Or specialized clients for eg Jersey or Apache CXR?


This is an old question (2008) so there are many more options now than there were then:

  • Apache CXF has three different REST Client options
  • Jersey (mentioned above).
  • Spring also has its own called RestTemplate
  • Commons HTTP Client build your own for older Java projects.
  • UPDATE circa 2014:

  • Async-http-client by Sonatype. Ning Async-http-client.
  • The new kid on the block which provides NIO support (although truthfully I don't think this really improves performance for clients like it does servers).

  • Apache HTTP Components (4.2) Fluent adapter - Better than old Commons HTTP Client 3 and easier to use for building your own REST client. You'll have to use something like Jackson for JSON parsing support and you can use HTTP components URIBuilder to construct resource URIs similar to Jersey/JAX-RS Rest client. HTTP components also supports NIO but I doubt you will get better performance than BIO given the short request nature of REST.
  • UPDATE 2016 :

  • OkHttp - Supports newer HTTP protocols (SPDY and HTTP2). Works on Android. Unfortunately it does not offer a true reactor-loop based async option (see Ning and HTTP components above). However if you use the newer HTTP2 protocol this is less of a problem (assuming connection count is problem).
  • Retrofit - Will auto create clients based on interface stubs similar to some Jersey and CXF extensions. Uses OkHttp.
  • Apache HttpComponents 5 will supposedly have HTTP2 support
  • A caveat on picking HTTP/REST clients. Make sure to check what your framework stack is using for an HTTP client, how it does threading, and ideally use the same client if it offers one. That is if your using something like Vert.x or Play you may want to try to use its backing client to participate in whatever bus or reactor loop the framework provides... otherwise be prepared for possibly interesting threading issues.


    As I mentioned in this thread I tend to use Jersey which implements JAX-RS and comes with a nice REST client. The nice thing is if you implement your RESTful resources using JAX-RS then the Jersey client can reuse the entity providers such as for JAXB/XML/JSON/Atom and so forth - so you can reuse the same objects on the server side as you use on the client side unit test.

    For example here is a unit test case from the Apache Camel project which looks up XML payloads from a RESTful resource (using the JAXB object Endpoints). The resource(uri) method is defined in this base class which just uses the Jersey client API.

    eg

        clientConfig = new DefaultClientConfig();
        client = Client.create(clientConfig);
    
        resource = client.resource("http://localhost:8080");
        // lets get the XML as a String
        String text = resource("foo").accept("application/xml").get(String.class);        
    

    BTW I hope that future version of JAX-RS add a nice client side API along the lines of the one in Jersey


    You can use the standard Java SE APIs:

    private void updateCustomer(Customer customer) { 
        try { 
            URL url = new URL("http://www.example.com/customers"); 
            HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
            connection.setDoOutput(true); 
            connection.setInstanceFollowRedirects(false); 
            connection.setRequestMethod("PUT"); 
            connection.setRequestProperty("Content-Type", "application/xml"); 
    
            OutputStream os = connection.getOutputStream(); 
            jaxbContext.createMarshaller().marshal(customer, os); 
            os.flush(); 
    
            connection.getResponseCode(); 
            connection.disconnect(); 
        } catch(Exception e) { 
            throw new RuntimeException(e); 
        } 
    } 
    

    Or you can use the REST client APIs provided by JAX-RS implementations such as Jersey. These APIs are easier to use, but require additional jars on your class path.

    WebResource resource = client.resource("http://www.example.com/customers"); 
    ClientResponse response = resource.type("application/xml");).put(ClientResponse.class, "<customer>...</customer."); 
    System.out.println(response); 
    

    For more information see:

  • http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-55.html
  • 链接地址: http://www.djcxy.com/p/71512.html

    上一篇: 我可以在Android应用程序中使用REST和SOAP吗?

    下一篇: 为Java留下客户端?