• ALL
  • CATEGORIES

How to kickstart a RESTful Java service with Spring Boot

Spring Boot

Like a lot of Java developers, most of the work I do these days touches on one of the Spring family of products. From dependency injection to transaction management, Spring framework modules can offer a variety of tools to modern Java applications. One such Spring project is Spring Boot.

What is Spring Boot?

Spring Boot enables you to quickly stand-up applications in an easy and clean fashion. Best of all, there is very little of the potentially tricky Spring configuration that could otherwise be required.

At the heart of the Spring Boot service is the Spring Boot Maven Plugin. This plugin (and there is an alternative for Gradle, if that’s your build manager of choice) offers the following benefits:

  • It builds a master runnable .jar file containing all other .jars from the classpath;
  • It finds and identifies as runnable the main() method
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.3.RELEASE</version>
</parent>

Creating an MVC system

So now we have the Spring Boot plugin, we need to design a system that we want to stand up. For this example, we will create a simple MVC system that serves content on a given endpoint.

Lets start with the core of the Spring Boot application – the Application class.

package uk.co.manifesto;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SampleApplication {

  public static void main(String[] args) {
    SpringApplication.run(SampleApplication.class, args);
  }
}

The @SpringBootApplication annotation bundles up a bunch of other annotations that enable Spring MVC and tells the application to use the class as a bean definition source. SpringApplication.run allows the application to launch from the main() method.

Building a simple controller

Now lets build a simple controller to create an endpoint for our web application to expose, and basic model.

Controller Class


package uk.co.manifesto;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SimpleController {

  private static final String template = "Hello, %s!";

  @RequestMapping("/helloworld")
  public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {

    return new Greeting(String.format(template, name));
  }
}

Model Class


package uk.co.manifesto;

public class Greeting {
private final String content;

  public Greeting(String content) {
    this.content = content;
  }

  public String getContent() {
    return content;
  }
}

These simple model and controller classes expose an endpoint at /helloworld, which takes an optional request parameter of ‘name’, and returns a formatted ‘hello world’ string with the name passed in (or a default value if no value is specified).

Spring Boot annotations

Lets break down some of the Spring annotations.

  • @RestController – combines @Controller and @ResponseBody. Identifies that all methods in said controller return objects as opposed to views.
  • @RequestMapping – maps HTTP requests to the given endpoint into the method
  • @RequestParam – pulls a parameter from the request (in this case ‘name’) into a String that can be manipulated in the method

The model class is a POJO that takes a simple piece of content. This could be extended or expanded to represent anything that your application needs to model.

Building and running our Spring Boot application

Now that we have built our simple web application, and enabled it for Spring Boot, lets build and run it. To build and run a Spring Boot application, its as simple as using the command:


mvn spring-boot:run

This will generate the .jar of .jars, identify the main() method, handle the Spring configuration, and launch the application on an embedded Tomcat server, with no need to configure or deploy any web server.

You can access your application at http://localhost:8080/helloworld, or with the optional query parameter at http://localhost:8080/helloworld?name=Matt.

Conclusion – and further reading

So there you have it – you can see how easy it is to stand up a Spring Boot application using MVC patterns, all without the need to fiddle with any Spring configuration or deploy anything to any servers.

Spring.io has a series of excellent tutorials to expand your skillset, covering everything from securing a web application to registering an app with Facebook.

 

Leave a reply

You can use these tags:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Sign up for the Manifesto newsletter and exclusive event invites