<< Did Google Just Update Chrome Without Telling Me? | Home | Google Chrome Annoyances (Part 1) >>

Friday Java Quiz: RPC vs. REST

I've been hearing a lot about the merits of REST over the deficiencies of RPC lately. For example, the latest blog entries from RPC-hero-turned-REST-advocate Steve Vinoski.

While it's fun to listen to famous people debate the issues, I've always had the impression that REST is something that is not as concrete as RPC. In Java, for example, If I want to do RPC, I have several different tools that I can use right there in the JDK: one is RMI, the other is Java IDL (not that I'll recommend it).

That leads to todays question:

Q: How the bleep do you get started with doing REST with Java?

(I know you are going to wave your hands every which way and utter phrases like "uniform interface" or "hypermedia as the engine of application state" or "internet scale" or "small joins, loosely parted".)

Q: Show me! (Send a string "Howdy" from host A to host B the REST way.)

No peeking at the internet or the REST book. Just write the demo.

Tags :


Re: Friday Java Quiz: RPC vs. REST

Hmm... I rarely write code in unfamiliar frameworks without looking on the internet... I could do this with some of the tools I already use, but JAX-RS is the right tool to do REST in a Java environment.

Would I pass your quiz if I used a JAX-RS environment I previously downloaded from the Internet?

Re: Friday Java Quiz: RPC vs. REST

Ehh.. OK, as long as the string gets sent over the wire.

Re: Friday Java Quiz: RPC vs. REST

From the Jersey code: The server:

import com.sun.net.httpserver.HttpServer;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import java.io.IOException;

public class Main {
    
    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServerFactory.create("http://localhost:9998/");
        server.start();
        
        System.out.println("Server running");
        System.out.println("Visit: http://localhost:9998/helloworld");
        System.out.println("Hit return to stop...");
        System.in.read();
        System.out.println("Stopping server");   
        server.stop(0);
        System.out.println("Server stopped");
    }    
}

import javax.ws.rs.GET;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.Path;

// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {
    
    // The Java method will process HTTP GET requests
    @GET 
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @ProduceMime("text/plain")
    public String getClichedMessage() {
        // Return some cliched textual content
        return "Hello World";
    }
}
The client: Any browser: http://localhost:9998/

There you go... Look ma, no need for a web server!

Re: Friday Java Quiz: RPC vs. REST

Here's the brute force, basic Java API way ... minus exception handling.
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
Writer writer = new OutputStreamWriter(conn.getOutputStream());
writer.write("Howdy");
writer.flush();

Add a comment Send a TrackBack