Spring Boot Restful Web Services Example

Spring Boot Restful Web Services Example walks you through the process of creating a restful api with spring boot step by step. Noticed that Spring Boot is sub-project developed by developers of spring framework, Spring Boot makes it easy to create stand-alone, production-grade Spring based applications with minimum configuration possible.

Other interesting posts you may like

Let’s begin

If your are a beginner on Spring Boot, before starting you should refer to the tutorial Spring Boot Maven Example Hello World with JSP

Project Structure
In this Spring Boot Restful Web Services Example, we will create a project with directory structure like below

Spring Boot Restful Web Services Example

Maven Dependencies
To create Spring Boot Restful Web Services Example, you need to add dependencies into pom.xml file like below:

Let’s dig deeper:
In order to enable XML representations we need to add Jackson XML extension by using the following dependency:

Configure Spring Web Application
To set up application as a servlet application we extend our main class with SpringBootServletInitializer and override the configure method using SpringApplicationBuilder.

Configure properties file
You can define port and context path for your spring boot application in properties and it should be stored in application.yml like below.

Create the Restful Controller
In our Spring Boot Restful Web Services Example, We are creating a restful controller named RestfulController like below. If you are a beginner on Spring Restful, before starting you should refer to the tutorial Spring MVC RESTFul Web Service CRUD Example

Run Application
To deploy and run our Spring Boot Maven Example Hello World on web container, we run the below command in console

Note: You must be in your project directory before run that command.

While running that command, you will see the output like below

Spring Boot Restful Web Services Example

After running successfully, you can access the Restful api via URL http://localhost:8080/spring-boot-restful/user/ and see the homepage such as

Spring Boot Restful Web Services Example

You can also access this api via DHC Rest client like below

Spring Boot Restful Web Services Example

1. Retrieve one user by id

Spring Boot Restful Web Services Example

2. Create one user
Use POST, provide the information with type ‘application/json
Spring Boot Restful Web Services Example

Click Send, new user would be created and we will be accessible at the location mentioned in Location header below.

Spring Boot Restful Web Services Example

3. Update an existing user
Use PUT, provide the information with type “application/json”.
Spring Boot Restful Web Services Example

Click Send, the user should be updated on sever side like below

Spring Boot Restful Web Services Example

4. Delete an existing user
Use DELETE, provide the id in url as the screen below and click send. User should be deleted from server side.
Spring Boot Restful Web Services Example

You also try to test with other cases such as deleting all user, creating an existing user, etc.

As you know there is no difference between Spring Boot and Spring MVC when you create Rest API. That’s all on the tutorial Spring Boot Restful Web Services Example.

Download complete source code, please click link below

spring-boot-restful-web-services-example.zip (22 downloads)

Source code on GitHub https://github.com/javabycode/spring-boot-restful-web-services-example

Leave a Comment

*

Please share it if you found this useful
Hide Buttons