Spring Boot - RESTful API'S with Spring Boot, JPA and MySQL

You always wanted to create RESTful API's that store their data in a database? In this tutorial I will show you how you can use Spring Boot, Java Persistence API (JPA) and MySQL to create a small RESTful API that stores your data in a local MySQL database.
Spring Boot JPA Tutorial

RESTful API’s mit Spring Boot & Java Persistence API (JPA)

Preamble

You always wanted to create RESTful API’s that store their data in a database? In this tutorial I will show you how you can use Spring Boot, Java Persistence API (JPA) and MySQL to create a small RESTful API that stores your data in a local MySQL database.

Requirement

  • Docker installation or local MySQL installation
  • Basic knowledge of Apache Maven
  • Basic knowledge of REST API
  • Basic knowledge of Spring Boot

Docker Container for MySQL

As mentioned in the introduction we need a database for our RESTful API. For this you can either install a local MySQL instance or start or stop a MySQL database instance with Docker Compose.

In this tutorial we will use a MySQL database instance, which we start via Docker Compose. This has the advantage that you don’t have to install your own MySQL database.

To start a MySQL database instance with Docker Compose, we first need a configuration file named „docker-compose.yml“. Here we enter our values for the MySQL database instance.

The following section shows the complete docker-compose.yml file

We start the MySQL database instance on the console with the commands „docker-compose up“ or „docker-compose up -d“. Both commands start the database instance. The parameter „-d“ starts the MySQL database instance in Detached mode. So in the background!

Create Spring Boot project

After we have started our MySQL database we will create a Spring Boot project. For this purpose Spring offers a configurator on its website https://start.spring.io/, with which you can create a Spring Boot project. The nice thing about this page is that you can specify the libraries you need directly here.

First we enter the value for Group for our project. In my case I entered „de.ertantoker.tutorials“. Then we enter the value for Artifact. Here you can enter a name of your choice. In my case it is „spring-boot-jpa“.

Spring Boot JPA Project

For our project we need two libraries. In our case we need „JPA“ and „Web“. You can enter and select both libraries at Dependencies on the right.

After we have entered and selected everything, we create our project by clicking on the button „Generate Project“.

Directly after the click a download starts, which contains our project with the corresponding libraries. Next we have to import our project into our IDE. Which IDE you use is up to you

Configurations

Sobald unser Projekt importiert ist können wir nun die Konfigurationen vornehmen, die wir für unsere RESTful API benötigen

Java Persistence API (JPA) in Spring Boot

First we add the JPA and Spring Boot specific properties to the application.yml. In the project we just imported.

The following section shows the application.properties file.

Further configurations are not necessary for Spring Boot and JPA. Spring Boot uses Hibernate as JPA implementation in this case, so no further configuration is required.

MySQL Database Driver

Next we have to add the database driver for our MySQL database to the project. For this we extend our pom.xml file by a dependency entry with the MySQL JDBC drivers.

The following section shows the complete pom.xml

Java Persistence API (JPA) Entity Classes

We will now continue with the creation of the repository interface. In Spring Data, repository interfaces are access classes that provide predefined methods for accessing JPA objects. In all applications that I have developed so far in my career as a software developer, there were the classic CRUD methods. CRUD stands for Create, Read, Update and Delete. Instead of always reimplementing them, Spring Data offers ready-made methods that you can use.

For our CustomerRepository Interface to be defined as a Spring Data Repository Interface, it must be derived from the CrudRepository<T, ID> interface. This interface inheritance is the only thing we have to do. So we have the database access layer with Spring Data at our disposal.

The following section shows the complete CustomerRepository.java class.

Spring Boot RestController

In order to complete the implementation, we need a RestController that provides our RESTful API. For this we will create the class CustomerController.java, which will then offer a POST method for creating customer objects. As well as a GET method for getting a customer object.

The RestController Class

We create a RestController by declaring the class with the annotation @RestController. This annotation ensures that Spring Boot recognizes the class as RestController at startup and offers the methods declared with @GetMapping or @PostMapping as RESTful HTTP methods.

In addition to the @RestController annotation we add the annotation @RequestMapping(„/customers“) on the class level. This indicates that the REST methods declared in this class are all available at the URL http://<myIP>:8080/customers

An extensive tutorial on how to create RestController can be found in my tutorial RestController with Spring Boot.

Creating with the POST Method

First we will create the POST method for creating new customer objects. For this we implement a new method that accepts a customer object as RequestBody and stores it via the repository interface.

To realize this we first need the annotation @PostMapping. By this we achieve that all HTTP POST requests, which are placed on http://localhost:8080/customers, are processed by this method. The annotation @RequestBody specifies that the customer object is created from the request body of the POST request. The default behavior is that the request is made in JSON.

The following section shows the REST interface for creating a new customer object.

Getting with the GET Method

As mentioned above we implement a HTTP GET method to the HTTP POST method noche. This will return a customer object to us. As path parameter we pass the ID of the customer object to the method and then get the customer object back from the database. If there is no customer object with the ID in the database, the HTTP status 404 NOT FOUND is returned.

The following section shows the REST interface for getting a customer object.

In the implementation we see that Optional was used. You can learn how to handle Java streams and optional objects in my tutorial Java Streams.

The complete RestController

The following section shows the complete RestController code.

Launch Spring Boot RESTful API

Now that we have implemented our RESTful API with the two HTTP methods, we will start them.

First we have to build the Spring Boot application with Maven. The „mvn clean install“ command creates the Spring Boot application.

Once the Maven build process is completed with a „BUILD SUCCESS“, we will find a directory named „target“ in our project folder.

The „target“ folder now contains our finished JAR file. With the command „java -jar spring-boot-jpa-0.0.1-SNAPSHOT.jar“ we start our Spring Boot application. Please note that the MySQL database must be started via Docker Compose before starting it.

You can find a detailed description how to create RESTful API on my tutorial RestController with Spring Boot.

Conclusion

In this tutorial we learned how to create a RESTful Spring Boot application with Spring Data, Spring Repository, JPA and MySQL. More detailed tutorials can be found on my blog. I hope you enjoyed the tutorial and would love to hear your criticism, suggestions and comments.

Verwandte Beiträge

Leave a comment

Diese Website verwendet Cookies. Durch die Nutzung unserer Services erklären Sie sich damit einverstanden, dass wir Cookies setzen. weitere Informationen

Diese Website verwendet Cookies. Durch die Nutzung unserer Services erklären Sie sich damit einverstanden, dass wir Cookies setzen.

Schließen