hyperlink hyperlink - 5 months ago 30
Java Question

What is the fastest way to stand up a REST service using Java?

I have a few utility methods that I wrote in Java, for test data creation against a service. A couple of my colleagues who have the same use case thought it would be cool if they could re-use it, but they are writing their tests in Perl and Python respectively. So I am thinking of standing up a JSON REST service that would expose these methods.

I have limited experience writing web-apps. What would be the fastest way to stand up and deploy my service?

I am thinking of using eclipse/spring to do it and ant to deploy on a Jetty container. But I would like to hear your experiences about this. Also it seems some IDEs/Plugins have better support for some technologies than others. So I would like to hear what IDE/Plugins + J2EE Technology stack + Server(If that matters) is a good combination for my usecase and why. I would like to stick to open source software.

Answer

I believe using Maven gets you up as fast as one can. Here's how you can do it.

It uses the RESTEasy implementation of JAX-RS (the Java API for RESTful Web Services, an official part of Java EE 6).

This is a java war maven project with the bare minimum structure. These are the files:

-pom.xml
-src
 |
  --main
    |
     --java
       |
        --rest
          |
           --HelloResource.java
           --JaxRsActivator.java

I called it simpleRest as seen below. All the archives are exactly as shown:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                        http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>simpleRest</groupId>
    <artifactId>simpleRest</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.3.1.GA</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <!-- So maven doesn't require web.xml -->
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

HelloResource.java

package rest;

import java.util.Date;

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

@Path("/hello")
public class HelloResource {
    @GET
    @Produces("text/plain")
    public String helloResource() {
        return "Hello! It's "+new Date();
    }
}

JaxRsActivator.java:

package rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {

}

This generates a simpleRest.war (through mvn clean package). You can deploy it to a freshly installed JBoss AS 7.1.1.Final (just throw the war file in the deploy folder) or a Tomcat.

After that, the URL is available as expected:

http://127.0.0.1:8080/simpleRest/rest/hello

How quick is that?