Java Spring Boot Rest Example

In this tutorial, we will build a simple Rest Web Service with Spring Boot.

1. Project Init

a) With Spring Tool Suite or Spring Tools in Eclipse

New Spring Starter Project

Project Configuration Wizard

Select Web Starter

b) Using Spring Initializr

Generate your project using Spring Initializr

Add Web Dependancy

2. Create Resource Model

Create a Java class to represent the model. Spring (using Jackson library) will marshall instances of our class in JSON.

Jackson will detect public fields, so it’s optional to write private fields with accessors in this case.

3. Create Controller

Create a controller (@RestController annotation) and a method to handle the request.

The @RequestMapping annotation makes the mapping between the path (“/hello”) and the method.

The @RequestParam specifies a request parameter (“name”).

4. Run the App

a) With Spring Tools

Run Project As – Spring Boot App

Tip : How to Change the Port

b) With Maven

Run maven target : spring-boot:run

5. Consume the service

a) Test in browser

Go to http://localhost:8080/hello?name=xavier

The output should be :

b)  Test in Java

See : Call a Rest Web Service in Java

c) Test in Javascript

See : Call a Rest Web Service in Javascript

Useful Links

Github Repository

Official Getting Started Guide

You may also like...

1 Response

  1. I am in fact grateful to the holder of this web site who has shared this
    great article at at this time.

Leave a Reply

Your email address will not be published. Required fields are marked *