Reader small image

You're reading from  Full Stack Development with Spring Boot and React - Third Edition

Product typeBook
Published inApr 2022
Reading LevelIntermediate
PublisherPackt
ISBN-139781801816786
Edition3rd Edition
Languages
Right arrow
Author (1)
Juha Hinkula
Juha Hinkula
author image
Juha Hinkula

Juha Hinkula is a software development lecturer at Haaga-Helia University of Applied Sciences in Finland. He received an MSc degree in Computer Science from the University of Helsinki and he has over 17 years of industry experience in software development. Over the past few years, he has focused on modern full stack development. He is also a passionate mobile developer with Android-native technology, and also uses React Native.
Read more about Juha Hinkula

Right arrow

Chapter 4: Creating a RESTful Web Service with Spring Boot

In this chapter, we will first create a RESTful web service using the controller class. After that, we will demonstrate how to use Spring Data REST to create a RESTful web service that also provides all CRUD functionalities automatically. After you have created a RESTful API for your application, you can implement the frontend using a JavaScript library such as React. We will be using the database application that we created in the previous chapter as a starting point.

Web services are applications that communicate over the internet using the HTTP protocol. There are many different types of web service architectures, but the principal idea across all designs is the same. In this book, we are creating a RESTful web service from what is nowadays a really popular design.

In this chapter, we will cover the following topics:

  • Basics of a RESTful web service
  • Creating a RESTful web service with Spring Boot
  • Using...

Technical requirements

The Spring Boot application created in the previous chapters is required. Postman, cURL, or another suitable tool for transferring data using various HTTP methods is also necessary.

The following GitHub link will also be required: https://github.com/PacktPublishing/Full-Stack-Development-with-Spring-Boot-and-React/tree/main/Chapter04.

Check out the following video to see the Code in Action: https://bit.ly/3PP2SOn

Basics of REST

Representational State Transfer (REST) is an architectural style for creating web services. REST is not a standard, but it defines a set of constraints defined by Roy Fielding. The six constraints are as follows:

  • Stateless: The server doesn't hold any information about the client state.
  • Client: The client and server act independently. The server does not send any information without a request from the client.
  • Cacheable: Many clients often request the same resources; therefore, it is useful to cache responses in order to improve performance.
  • Uniform interface: Requests from different clients look the same. Clients may include, for example, a browser, a Java application, and a mobile application.
  • Layered system: REST allows us to use a layered system architecture.
  • Code on demand: This is an optional constraint.

The uniform interface is an important constraint, and it means that every REST architecture should have the following elements...

Creating a RESTful web service

In Spring Boot, all HTTP requests are handled by controller classes. To be able to create a RESTful web service, first, we have to create a controller class. We will create our own Java package for our controller:

  1. Activate the root package in the Eclipse Project Explorer and right-click. Select New | Package from the menu. We will name our new package com.packt.cardatabase.web:

Figure 4.1 – New Java package

  1. Next, we will create a new controller class in a new web package. Activate the com.packt.cardatabase.web package in the Eclipse Project Explorer. Right-click and select New | Class from the menu; we will name our class CarController:

Figure 4.2 – New Java class

  1. Now, your project structure should look like the following screenshot:

Figure 4.3 – Project structure

Important Note

If you create classes in the wrong package accidentally...

Using Spring Data REST

Spring Data REST (https://spring.io/projects/spring-data-rest) is part of the Spring Data project. It offers an easy and fast way to implement RESTful web services with Spring. To start using Spring Data REST, you have to add the following dependency to the pom.xml file:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

By default, Spring Data REST finds all public repositories from the application and creates RESTful web services for your entities automatically. In our case, we have two repositories: CarRepository and OwnerRepository, therefore Spring Data REST creates RESTful web services automatically for those repositories.

You can define the endpoint of service in your application.properties file as follows:

spring.data.rest.basePath=/api

Now, you can access the RESTful web service from the localhost:8080...

Summary

In this chapter, we created a RESTful web service with Spring Boot. First, we created a controller and one method that returns all cars in JSON format. Next, we used Spring Data REST to get a fully functional web service with all CRUD functionalities. We covered different types of requests that are needed to use CRUD functionalities of the service that we created. Finally, we also included our queries to RESTful web service.

In the next chapter, we will secure our backend using Spring Security.

Questions

  1. What is REST?
  2. How can you create a RESTful web service with Spring Boot?
  3. How can you fetch items using our RESTful web service?
  4. How can you delete items using our RESTful web service?
  5. How can you add items using our RESTful web service?
  6. How can you update items using our RESTful web service?
  7. How can you use queries with our RESTful web service?

Further reading

Packt has other great resources available for learning about Spring Boot RESTful web services:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Full Stack Development with Spring Boot and React - Third Edition
Published in: Apr 2022Publisher: PacktISBN-13: 9781801816786
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Juha Hinkula

Juha Hinkula is a software development lecturer at Haaga-Helia University of Applied Sciences in Finland. He received an MSc degree in Computer Science from the University of Helsinki and he has over 17 years of industry experience in software development. Over the past few years, he has focused on modern full stack development. He is also a passionate mobile developer with Android-native technology, and also uses React Native.
Read more about Juha Hinkula