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

No Time to Sit Back and Rest! Develop Your RESTful Web Services with Spring Boot and JRebel

In a previous blogpost, my colleague, friend and exceptionally talented thumb war player, Adam Koblentz, discussed how Spring can be super charged in a development environment to avoid unproductive developer tasks, eliminating build, compile, redeployment and container restarts. Sounds too good to be true? Wrong! This is something many other languages, particularly scripting languages like perl or PHP take for granted. And we want you to be as productive as possible because…


In this blog post we’ll dive deeper to see what else we can do with this combination and use an exciting new technology from Pivotal called Spring Boot. If you’re new to Spring Boot, don’t worry, we’ll talk about it in this blog post, also check out the webinar we ran on Spring Boot and JRebel, with Developer Advocate and Spring legend Josh Long, talking with our very own JRebel Product Marketing Manager, Adam Koblentz.


We’re going to create a restful service in Spring and use Spring Boot to launch our code, embed it into a tomcat server and host our service! Yeh, that’s right, Spring Boot provides us with an easy to use framework that bootstraps our application so we don’t need to create a WAR or an environment to test our code. That’s really awesome. And what’s more, it uses ascii art too, check it out!

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v1.1.9.RELEASE)

Let’s first create the code that’s going to act as our service. I’m using the restful demo application code available on github and using some instructions on the spring site. So far, I’ve created a Greeting class with a couple of fields to show an identifier, id, and some greeting text, content, with a constructor to set the fields and associated getters. Nice and simple so far.

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) { = id;
        this.content = content;

    public long getId() {
        return id;

    public String getContent() {
        return content;

Next I want a controller, as shown in the next code block, that creates a new instance of Greeting, passing in a unique identifier, and a String containing the greeting text specific to the name passed in as a parameter to the method.

public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    public Greeting greeting(String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));

This is all looking good, so now we should package up our application, deploy to Tomcat, but oh, wait! These are just the kind of manual steps that ultimately kill cute kittens and you should be ashamed even for considering them! Instead we’re going to use Spring Boot to deploy this code for us by implementing an Application class as shown below:

public class Application {

    public static void main(String[] args) {, args);

The SpringApplication helper class that we invoke in our main method with our class, Application.class, will make the Spring Boot runtime use it as a component in the Spring Application Context and also make the runtime process the annotations on the Application class.

Before we run this as a Java Application, we want to enable JRebel on the project first. Why? Well, just because Spring Boot makes it easier for a developer to get up and running with their application, doesn’t mean we want to restart every few minutes just because we change a bit of code. Ideally, we want to just bootstrap our application via Spring Boot once, then make our code changes and instantly view those code changes in our runtime without restart. To enable JRebel on the project, simply right click, and select JRebel->Add JRebel Nature as shown in the image below. Seriously, that’s it! Of course if you don’t have JRebel installed in your IDE (that’s another kitten you’re responsible for) you can easily fix that following these instructions.

Screenshot 2014-11-14 21.39.43

OK, now we can run our Application class as a Java Application and see what happens. From the reduced console output below, we can see Spring Boot has bootstrapped our application and embedded Tomcat into our application, which is now ready for us to use on port 8080.

 Server initialized with port: 8080
 Starting service Tomcat
 Starting Servlet Engine: Apache Tomcat/7.0.56
 Tomcat started on port(s): 8080/http
 Started Application in 8.274 seconds (JVM running for 11.497)

Oh, but wait, we’ve totally forgotten to create a rest endpoint, so let’s do that quickly. We need to change our controller code by adding a couple of annotations as shown below:

public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    public Greeting greeting(String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));

We’ve added an annotation to the class to make it a RestController and we’ve turned the greeting method into a rest endpoint, available at the URL mapping /greeting. No need to restart to see our changes, as we’re using JRebel so we can go straight to a browser and hit http://localhost:8080/greeting which gets us the following output.

Screenshot 2014-11-14 22.01.42

Nice, viewing code changes instantly! But the interesting parts are actually in the console. First of all we see JRebel adding a new bean and initialising it.

 JRebel-Spring: Adding bean 'greetingController'
 JRebel-Spring: Reconfiguring bean 'greetingController' [hello.GreetingController]

Then we see a bunch of dispatcherServlet initialization occuring.

 Initializing Spring FrameworkServlet 'dispatcherServlet'
 FrameworkServlet 'dispatcherServlet': initialization started
 FrameworkServlet 'dispatcherServlet': initialization completed in 52 ms

Followed by a new mapping created for /greeting as we specified in our annotation.

 Mapped "{[/greeting],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public hello.Greeting hello.GreetingController.greeting(java.lang.String)

This looks great, but our implementation doesn’t really cater for the case when the user doesn’t add a parameter on the invocation, for example, if we just use http://localhost:8080/greeting as the URL we get the following output:

Screenshot 2014-11-14 22.06.01

Notice that our id has incremented correctly, now at 2, but our greeting is a little harsh! Let’s make a code change to cater for the null case. We’ll update our code to provide a default parameter value using the Spring annotation RequestParam.

    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));

Simply saving this class and refreshing the browser gives us the following:

Screenshot 2014-11-14 22.12.20

Much nicer, we can now see that without passing a parameter, we make use the new Spring parameter default we added just seconds ago. More importantly, note that although we changed the existing instance of the GreetingController object at runtime, our id has incremented from 2 to 3. Yes, we can change the class, yet maintain existing object state! Now you can bask in all your kitten saving glory! In fact, I’m going to change my request mapping to the following…


…just to prove my point, because it only takes a second to do! (Note id is now 4 ;) )

Screenshot 2014-11-14 22.15.53

What’s the Message?

In summary, Spring Boot is a really awesome way to get up and running with your Spring applications without having to manually bootstrap your environment yourself. JRebel’s Spring Boot integration really improves the development experience for a Spring developer, significantly improving productivity. You can of course get the value of JRebel if you use Spring without Spring Boot. To download your free 14 day JRebel trial visit our trial page and enjoy productive, redeploy-free development.



  • Lucky

    But in Intellij Idea IDE we need to make the project everytime before the code compile and there is autosave. I would be nice if jrebel could make changes to compile on the fly instead of make the project everytime.