Java Spring Boot REST Application

RESTful webservices are a simple and effective way to provide access to your application’s data. With Spring Boot you are able to create such an interface within minutes. The example code can be found here on GitHub.

Start by creating a new Maven project and set the spring-boot-starter-parent artifact as parent. For the RESTful interface you need the spring-boot-starter-rest and for the database access the spring-boot-starter-data dependency. In this example the embedded in-memory database H2 is used.

<project xmlns="" xmlns:xsi=""






Now that you have declared all your dependencies, you can create a domain class. In this example i created a employee entity. Use JPA annotations to tell Spring Boot that this is an entity class of your application. As you work with a relational database here, you also need to define an id attribute and annotate it as an auto-generated id.

package at.grisa.example.rest_example;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Employee {
 private Long id;
 private String firstName;
 private String lastName;
 private Double salary;
 private Date dateOfBirth;
 private Date hire;
 private Date fire;

 public Employee() {
 @GeneratedValue(strategy = GenerationType.AUTO)
 public Long getId() {
 return id;

 public void setId(Long id) { = id;

 public String getFirstName() {
 return firstName;

 public void setFirstName(String firstName) {
 this.firstName = firstName;

 public String getLastName() {
 return lastName;

 public void setLastName(String lastName) {
 this.lastName = lastName;

 public Double getSalary() {
 return salary;

 public void setSalary(Double salary) {
 this.salary = salary;

 public Date getDateOfBirth() {
 return dateOfBirth;

 public void setDateOfBirth(Date dateOfBirth) {
 this.dateOfBirth = dateOfBirth;

 public Date getHire() {
 return hire;

 public void setHire(Date hire) {
 this.hire = hire;

 public Date getFire() {
 return fire;

 public void setFire(Date fire) { = fire;


To support paging and sorting in an easy way, you have to create a new Interface which extends the PagingAndSortingRepository Interface. In this example I called it EmployeeRepository.

package at.grisa.example.rest_example;


public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {

Now you would be able to create a WAR file and deploy it on an application server. But in this example I take the easier way by creating a single runnable JAR. I use Springs support for embedding the Tomcat servlet container. The @SpringBootApplication annotation handles the configuration of the application for you.

package at.grisa.example.rest_example;

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

public class Application {

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

That’s all of the code you need to write, now you can package your application with Maven.

mvn clean package

And run it.

java -jar target/rest_example-0.0.1-SNAPSHOT.jar

Test your new application with a REST client like Postman.

Get a description of your interface with a GET request on http://localhost:8080/employees.


Insert a new record by sending a POST request with header „Content-Type: application/json“ and the employee data in the body.



A new GET request on http://localhost:8080/employees will show your first employee record.



Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.