Spring Boot | Sample REST Application
In this post we are going to write a Simple REST Application in Spring Boot. Spring Boot has inbuilt tomcat which makes an application start without the need to have any application/web server. Though we can always deploy applications in web servers too.
For this tutorial we will be using Maven for dependency management.
Step 1. Add all required Spring dependencies in your pom file
We can use below sample pom file to start with or go to https://start.spring.io/ to kick start your spring boot application.
Step 2. Spring starter class
Spring Boot uses @SpringBootApplication to auto configure some of spring configuration. You can find more info on SpringBoot details
Step 3. Application Controller
We will be using MVC pattern to separate our restful layer from the service layer.
In our REST service we use @RestController annotation. It is combination of @Controller and @ResponseBody annotations.
We created CustomerController to return customer details.
Step 4. Application Service
In any application, the service takes a request from the endpoint and processes it and gives back a response.
In our application we will be returning mocked data for each request. In the real scenario this generally calls any other service (third-party etc), db using a repository to fetch data.
Step 5. Disable Spring security
As this is a demo application, we will be disabling the spring authentication. To do that, we will be adding security.basic.enabled=false to the application.properties file.
Step 6. Test Application
Once all changes added we can run command mvn clean install to build our application and create jar file.
Run this command to start application java -jar target\SpringBootHelloWorld-0.0.1-SNAPSHOT.jar
Comments
Post a Comment