Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.

Easy JAX-RS with Jersey and JRebel

jaxteller

In a previous blog post, my colleague Simon Maple (voted the most handsome of Java Champions) provided an excellent walkthrough creating REST services with Pivotal’s Spring Boot. He also introduced JRebel which lets you view code changes instantly, really he’s quite a guy!

FOLLOW ALONG WITH JREBEL NOW

Jersey is the reference implementation of JAX-RS, the RESTful web service spec for Java, there are maven archetypes which make great examples. For this blog post, we’ll use the jersey-quickstart-webapp archetype from the catalog. You should be familiar with the typical maven project layout, and we’re left with a very simple RESTful web service that can be packaged as a war file and deployed with an app server, like tomcat or wildfly.

Our starting point MyResource.class contains a simple GET that returns plain text and specifies a PATH for the URI.

@Path("/myresource")
public class MyResource {
  @GET 
  @Produces("text/plain")
  public String getIt() {
    return "Hi there!";
  }
}

In our web.xml we specify the servlet mapping

<servlet-mapping>
  <servlet-name>Jersey Web Application</servlet-name>
  <url-pattern>/webresources/*</url-pattern>
</servlet-mapping>

First, let’s make sure we have JRebel installed in our IDE. JRebel is readily available in the plugin directory or marketplace for all three of the major IDEs: Eclipse, IntelliJ, and NetBeans. Simply install it, restart your IDE, and enable JRebel for your application server (usually just a checkbox is required).

You can verify that JRebel is running when you see the banner in the appserver console:

JRebel:  #####################################
JRebel:  
JRebel:  JRebel Agent 6.0.2 (201412191331)
JRebel:  (c) Copyright ZeroTurnaround AS, Estonia, Tartu.

And you can verify that JRebel is configured for your application by “monitoring” messages in the console as well:

JRebel: Directory '/path/to/workspace/jerseyblogpost/target/classes' will be monitored for changes.

Let’s do something simple like a normal text change, especially helpful for those typos that don’t cause compilation errors, but would definitely make a spell checker raise its eyebrows. If we notice the welcome message returned by the REST call:
jersey_post_1

Let’s fix that quick type from “theare” to “there”, just change the code and hit save. Then you can verify the new code was picked up by JRebel and reloaded into the JVM by looking at the text returned by the call and also the reload messages in the console:

JRebel: Reloading class 'com.zeroturnaround.jerseyblogpost.MyResource'.
JRebel-Jersey: Reloading Jersey configuration

jersey_post_2

So that was pretty cool, we were able to change the content of a REST endpoint, let’s add a new one too. Let’s create a new @Path mapping /newpath:

@GET
@Path("/newpath")
@Produces("text/plain")
public String getNewPath(){
  return "This is a new path!";
}

On a real application though, we may want to take some input and do something with it, so let’s use the JAX-RS QueryParam annotation and take in a name for a custom greeting and feed that into a new NameProcessor class.

Here’s the new method using the QueryParam annotation

@GET
@Path("/namepath")
@Produces("text/plain")
public String getTest(@QueryParam("name") String name){
  if (name != null) {
    return "Hey " + NameProcessor.processName(name);
  }
  return "Hey name";
}

and here’s the NameProcessor class’s processName method

public static String processName(String name) {
  return name.toUpperCase();
}

Remember, we won’t have to rebuild or redeploy and can just hit save and hit the URL in your browser. If we go to localhost:8080/jerseyblogpost/webresources/myresource/namepath?name=Adam
we see that everything works as expected:
jersey_post_3

and in the console we see:

JRebel: Reloading class 'com.zeroturnaround.jerseyblogpost.MyResource'.
JRebel-Jersey: Reloading Jersey configuration

To recap, we started a new project using a maven archetype for jersey, installed JRebel into our IDE, fixed a quick typo, added new paths, and even added a new class to process our input from the web and didn’t have to rebuild our entire app or even restart it thanks to JRebel.

If you have any questions you can find me on twitter at @akoblentz or leave a comment below!


SET UP JREBEL IN THE NEXT 10 MIN!