DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Using the Spring Boot Rest Service

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!

Saurabh Sharma user avatar by
Saurabh Sharma
CORE ·
Nov. 05, 18 · Tutorial
Like (7)
Save
Tweet
Share
11.61K Views

Join the DZone community and get the full member experience.

Join For Free

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.

@RestController
public class FoundationController {

    @ResponseBody
    @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 Framework Spring Boot Web Service

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Problems of Cloud Cost Management: A Socio-Technical Analysis
  • Top Five Tools for AI-based Test Automation
  • GPT-3 Playground: The AI That Can Write for You
  • 3 Ways That You Can Operate Record Beyond DTO [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: