First RESTful Service with Spring Boot

Introduction

Having the Spring Boot setup ready in Eclipse, we will create a simple RESTful Service application with Spring Boot. If you are still struggling with the Spring Boot setup in Eclipse, click here.

Implementation

With the Spring Boot initial project setup, you will notice that the SpringBootDemoApplication class is already available inside the project, which is actually the starting point of your application.

SpringBootDemoApplication.java

For further understanding about the main SpringBootDemoApplication class and the default generated POM dependencies, click here.


Further to create our RESTful service, let’s make sure the needed dependencies are present in the pom file.

pom.xml

Now let’s create a simple Employee class, so we can set one of its object in the controller and see it transform into JSON in the HTTP response body.

Employee.java

Next step is to write the controller, which should be like –

DemoController.java

With @ResponseBody annotation, Spring MVC does not render a model into a view, but rather writes the returned object into the response body, using one of the Spring’s Message Converters

Running the RESTful service application

Run the main SpringBootDemoApplication class as –

Restful service with Spring Boot

When the Spring Boot server is started, hit the local URL – http://localhost:8080/demo-rest-service
REST service with Spring Boot

Download the source code

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.