Development of RESTful API with Spring Boot in less than 20 minutes

Development of RESTful API with Spring Boot in less than 20 minutes

March 28, 2017
frontpage, Java
No Comment
The purpose of this blog is to show how to build restful web services using Java and Spring Boot. I will try to show you some of the best practices you should know when you have to develop that kind of service.
Web APIs has become a very important topic. Typically we use a RESTful design for them. The concept of REST is to separate the API structure into logical resources.

There are a few best practices which are for designing a clean RESTful API. Some of them will be used for the purpose of this blog.

Using nouns for url naming convention is strongly preferred
Use only plural nouns – /cars instead of /car
Sub – resources for relations
/api/cars – returns a list of all cars
/api/cars/4 – returns a car with id=4
Provide filtering and sorting for collections
Filtering – /cars?car_engine=V12 – returns a list of cars with that engine
Sorting – /cars?sort=horsepower
Use HTTP status codes
Version your API – /v1/api/

Martin Fowler blog was used as a reference.

Service overview and requirements:

Store new car object into data base
Change parameters for current existing car
Delete existing car
Retrieve a car from data base by id
Retrieve a list of all cars stored in data base

The source code can be found on my GitHub profile.

Work flow
First I will list the anchor points for developing this tutorial and then all of them will be explained in same order.

Create DB
Add project dependencies
Add a main method which will be used for project execution
Create DAO
Add Service layer
Short explanation and example for adding resource for the implementation of our REST endpoint

Let’s start !

1. Setting up the data base

2. Create project

2.1 Maven
These are the dependencies which are need for this tuorial. The complete code can be found in GitHub repository.

2.2 Add main method

The execution of the project will start when fire up this main method.

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, @ComponentScan with their default attributes.

3. Setting up the DAO

For the purpose of this tutorial I will use the abstract class which Spring provides – JdbcDaoSupport. By extending JdbcDaoSupport , set the datasource and Jdbctemplate in your class is no longer required, you just need to inject the correct datasource into MuscleCarDaoImpl. And you can get JdbcTemplate by using a getJdbcTemplate() method.

4. Adding a Service layer

The service will do some basic verifications for the data which we will retrieve. If a layer like this is need in a project which you are working on the verifications must be more detailed.

5. Adding a Resource

Next up, we have the implementation of our REST endpoint. We will use this class to map our service to incoming HTTP requests.
The @RestController is a convenience annotation between @Controller and @ResponseBody and marks this class as a web component discovered during class path scanning. The @RequestMapping annotation at the class level defines the base path mapping used for any other @RequestMapping annotations in this class. In this case all endpoints will start with path – /api/muscle.
The @RequestMapping annotation has many options and with this tutorial we are using small part.

value=”/url/{id}” used in conjunction with @PathVariable(“id”) int id maps the {id} part of the url path to the given method argument.
method = RequestMethod.GET/POST/PUT/DELETE define the HTTP method that is accepted
Method parameters annotated with @RequestBody will be populated with the incoming request JSON data.

That’s it. Run the application and see the results at http://localhost:8080

For easy testing can be used a tool like SoapUI or some browser plug-in like Postman.

Nikola Nenov

Full Stack Developer at Dreamix

More Posts

Do you want more great blogs like this?

Subscribe for Dreamix Blog now!