Build REST Web Service using Spring Boot

This tutorial provides a step-by-step guide on how to build a restfull web service using Spring Boot.

Prerequisites:

  • Eclipse IDE (neon release)
  • Maven 4
  • Java 1.8

1. Create maven web project

Open eclipse then create a new maven web project and name it as SpringBootRest.

The structure of the generated project looks like the following:Spring Boot Rest Project

2. pom.xml

After creating the web project, the first step is to configure Spring Boot inside pom.xml, so we add the following as a parent dependency:

Spring Boot exposes a starter dependency called spring-boot-starter-web which automatically imports all the required jars needed to develop and expose REST controllers. So we add it as a dependency:

In this tutorial, we use the embedded tomcat provided by Spring Boot, so we’re gonna build our application as a runnable jar file by setting the packaging attribute as jar:

P.S: in case you want to use external tomcat refer to “Deploy Spring Boot application on external tomcat”.

The final configuration step is add the Spring Boot plugin:

Literally, that’s all we need to start developing our REST controllers.

Following are the jars automatically imported by Spring Boot:Maven Dependencies

This is the whole pom.xml for reference:

3. Application.java

The second step is to create the Spring Boot initializer class, this is the entry point of our application. Annotating a class with @SpringBootApplication is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes in the traditional Spring applications.

P.S: By default, the servlet container automatically scans for REST controllers defined in the same package of the initializer, any controllers defined outside the package would be ignored.

4. Implement REST resources

We’re going to implement a very basic payment API which charges customers for buying items.

Our API would only accept JSON requests and respond back with JSON responses, thanks to jackson library which allows us to deal with requests and responses as POJO classes without worrying about JSON/POJOconversions.

Following is the payment request class which should be submitted by clients on each payment request:

And this is the base response returned back from our service:

Now we define our controller named as PaymentController under com.programmer.gate:

The only service exposed by our controller is the pay() method which looks very straightforward, it validates the client request using a predefined shared key, processes the request and respond back with the operation status.

Following are the common annotations used by our controller:

  • @RestController: this annotation marks the class as a Resource, it defines implicitly both @Controller and @ResponseBody mvc annotations, when annotating a class with @RestController, it’s not necessary to write @ResponseBody beside the POJO classes returned from your methods.
  • @RequestMapping: this annotation defines the url of the resource in addition to the method type: GET/POST, in our example we expose the payment service as POST which is accessed through/payment/pay.
  • @RequestParam: this annotation represents a specific request parameter, in our example, we map a request parameter called key to an argument key of type String.
  • @RequestBody: this annotation represents the body of the request, in our example, we map the body of the request to a POJO class of type PaymentRequest (jackson handles the JSON/POJO conversion)

As noticed the response is represented as BaseResponse and there is no need to annotate it, jackson converts it implicitly to JSON.

5. Deploy the application

Following are the steps to deploy our application:

  • Right click pom.xml -> run-as -> Maven install
  • Maven generates a jar file called SpringBootRest-0.0.1-SNAPSHOT.jar inside target folder
  • Open cmd, then run the jar using: java -jar SpringBootRest-0.0.1-SNAPSHOT.jar

Here we go, our application is up and ready to serve requests at the default port 8080.

6. Test the service

In order to test our API, we use Advanced REST client plugin from chrome and we initiate 2 different requests:

Successful request: in this request we pass a valid shared key as a request parameter along with item details in the request body. This is how it looks like:Test the service

And this is our response:

Failure request: this request looks similar to the above but with invalid shared key, this is what we get from our API:

That’s it, hope you find it useful.

 

Hussein Terek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Himanshu Singh Recent comment authors
newest oldest most voted
Himanshu Singh
Guest
Himanshu Singh

Hi Hussein,
What is being passed for PaymentRequest from your browser?
I can see item details will be passed in the request body, but in what format?