Over a million developers have joined DZone.

Using the Spring Boot Rest Service

DZone 's Guide to

Using the Spring Boot Rest Service

Want to learn more about how to use the Spring Boot Rest service in your application? Check out this tutorial to find out!

· Java Zone ·
Free Resource

I have used many frameworks in the past, even Spring. Yesterday, I was trying to help out one of the new joiners, and while trying to showcase the power of Spring Boot, I wrote this blog.

What I am using:

  1.  For POM generation, I will need the initializer.

  2. I only needed the WEB dependency, so I just chose the version 5.0.10.

Image title

It was a quick generation of the zip file that I extracted and opened in IntelliJ.

My folder structure looks like this:

Image title

 You can verify by opening POM and looking at the dependencies.

I made one change after the project generation. I moved my Services application from com.samarthya.services to com.samarthya as it is the bootstrap class for my Spring Boot application. 

Next, we need to add the following code:

  1. Create a new package com.samarthya.controller. This will contain my Rest controller.

  2. Create a model class for a response.

First, create a model class in the com.samarthya.model:

public class MessageModel {
     * Default constructor.
    public MessageModel() {


     * Object constructor.
     * @param infoMessage
     * @param infoCode
    public MessageModel(String infoMessage, int infoCode) {
        this.infoMessage = infoMessage;
        this.infoCode = infoCode;

    private String infoMessage;
    private int infoCode;

    public String getInfoMessage() {
        return infoMessage;

    public void setInfoMessage(String infoMessage) {
        this.infoMessage = infoMessage;

    public int getInfoCode() {
        return infoCode;

    public void setInfoCode(int infoCode) {
        this.infoCode = infoCode;

The class name is MessageModel. Then, create a controller named FoundationController in com.samarthya.controller.

public class FoundationController {

    @GetMapping( path= "/hello")
    MessageModel sayHello() {
        return new MessageModel("Welcome mate!", 101);

I did a simple GetMapping that requires us to mark the method as the HTTP Get handler with a specific path /hello that it will respond to once you hit the URL in the browser or any other tool.

The ResponseBody annotation indicates that the return value should be bound to the response body of the request.

The RestController is used to mark the class as the controller exposing Rest endpoints.

Please note that if you are using  RestController  and the  ResponseBody, semantics are assumed by default, but as a practise I tend to still mark it.

The source code is ready to execute. Time to fire the button!

Once you execute the application, you can see that there is a mapping defined in the console (LOGS)

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped " {[/hello],methods=[GET]}" onto com.samarthya.model.MessageModel com.samarthya.controller.FoundationController.sayHello()

This denotes that the method and the corresponding definition have been resolved. This magic is hidden under the hood and is the power of Spring. 

Now, it's time to fire an HTTP Get request. So, open a browser and fire http://localhost:8080/hello:

Image title

And, you can see the corresponding message.

In case you have the main Spring Boot annotated main class in a different package, ensure that you are using the scanBasePackages attribute.
@SpringBootApplication( scanBasePackages = "com.samarthya" )

See you in the next blog where I try to protect this via LDAP-Spring security. Stay tuned!

spring ,rest api ,Java ,Spring Boot ,REST ,tutorial ,rest service

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}