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.
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:
<?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>
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();
}
}
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?
Here's the shortest way I could think of to a fully functional REST API requiring only Java, Gradle, and a text editor. Create two files in a clean project directory as follows:
build.gradle
buildscript {
repositories { mavenCentral() }
dependencies {
classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8'
}
}
apply plugin: 'tomcat'
repositories { mavenCentral() }
dependencies {
compile(
'com.sun.jersey:jersey-core:1.17',
'com.sun.jersey:jersey-server:1.17',
'com.sun.jersey:jersey-servlet:1.17',
'com.sun.jersey:jersey-json:1.17',
)
tomcat(
'org.apache.tomcat:tomcat-catalina:7.0.40',
'org.apache.tomcat:tomcat-coyote:7.0.40',
'org.apache.tomcat:tomcat-jasper:7.0.40',
)
}
src/main/java/org/example/TheApplication.java
package org.example;
import com.sun.jersey.api.core.ClassNamesResourceConfig;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlRootElement;
@Path("/")
@ApplicationPath("/")
public class TheApplication extends ClassNamesResourceConfig {
public TheApplication() { super(TheApplication.class); }
static Foo foo = new Foo();
@GET @Produces("application/json")
public Foo getFoo() {
return foo;
}
@POST @Consumes("application/json")
public Response setFoo(Foo foo) {
TheApplication.foo = foo;
return Response.ok().entity("Stored it!").build();
}
@XmlRootElement
static class Foo {
private String message = "Hello World";
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
}
}
Once those two files are in place, gradle tomcatRunWar
will start your REST API, and navigating to http://localhost:8080/fastest-web-service/
in a browser will get you the "Hello World" JSON message. If you then POST similar JSON, like {"message": "something else"}
to that same URL with something like curl or Poster specifying a Content-Type of "application/json", that new object will be stored and returned on successive GET requests. It's just a very bare-bones example but covers a lot of the basics of a JSON API.
IDEs: You could further develop this easily with any common Java IDE. The Community Edition of IntelliJ IDEA and Eclipse are both popular and free IDEs. <opinion>
IDEA is far superior and the Ultimate Edition is 110% worth the cost of a personal license for a regular Java developer.</opinion>
Technology Stack: JAX-RS is a leading way of writing REST APIs in Java. Pick any implementation. This example uses Jersey, the reference implementation. Spring may be overkill if you just need a simple API to expose already-written logic. It brings along more complexity than it sounds like you need. On the other hand, if you need a more flexible and capable framework that has more built-in support for virtually everything in the world, Spring may be just the ticket.
Server: Jetty or Tomcat supporting Servlet 3.0. There's not likely a reason for anything more or less.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With