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="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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>at.grisa.example</groupId>
 <artifactId>rest_example</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.2.RELEASE</version>
 </parent>

 <properties>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-rest</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

 <repositories>
  <repository>
   <id>spring-releases</id>
   <url>https://repo.spring.io/libs-release</url>
  </repository>
 </repositories>
 <pluginRepositories>
  <pluginRepository>
   <id>spring-releases</id>
   <url>https://repo.spring.io/libs-release</url>
  </pluginRepository>
 </pluginRepositories>
</project>

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;

@Entity
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() {
 }
 
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 public Long getId() {
 return id;
 }

 public void setId(Long id) {
 this.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) {
 this.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;

import org.springframework.data.repository.PagingAndSortingRepository;

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;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
 SpringApplication.run(Application.class, 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.

spring-boot-rest-data-1

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

spring-boot-rest-data-2

spring-boot-rest-data-3

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

spring-boot-rest-data-4

 

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.