In this post, we will learn how to create rest service using spring boot
What is Rest Service
REST is Web services which is lightweight, maintainable, and scalable in nature.
The underlying protocol for REST is HTTP, which is the basic web protocol. REST stands for REpresentational State Transfer
What You Will learn
End of this tutorial, you will learn to create a spring boot application with Rest service
Rest Service Annotation
// This annotation used to mention a class as Controller class
@RestController
// Here we are using this to retrieve value
@GetMapping
application.properties
server.port=8080
Dependency
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Sample Code
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
// This annotation used to mention a class as Controller class
@RestController
public class StudentController {
// To Retrieve we can use Get method
@GetMapping
public String getName() {
return "BeginnersBug";
}
}
In above example we are using a simple method to retrieve a hard coded value from spring boot rest service
@RestController
This annotation is the combination of @Controller
and @ResponseBody
. Which will mention particular class as a Controller class
@GetMapping
This annotation for mapping HTTP GET
requests onto specific handler methods.
@PostMapping
This annotation for mapping HTTP POST
requests onto specific handler methods.
Note : Here we didn’t used this annotation
Time needed: 45 minutes
Steps
- Create Spring boot Project
Follow this tutorial to create spring boot application
https://beginnersbug.com/how-to-create-spring-boot-application/ - Create Controller Class
Once you create and import the Spring boot application in eclipse
Create a Class, Here I created as StudentController.java - Add @RestController annotation
Add @RestController annotation to the class level as like in the above sample code
- Create a method to return String
Here I created a simple method with return type as String
public String getName() {return “BeginnersBug”;} - Add @GetMapping annotation
Add @GetMapping(“/name”) annotation to the method level as like in the above sample code
- Edit application.properties
Navigate to application.properties under src/main/resources/
add this server.port=8080 - Run
Navigate to main class. Right Click and click on RunAs –>JavaApplication
- Testing
Open Browser http://localhost:8080/name . You can see the BeginnersBug in the browser
Github
https://github.com/rkumar9090/student
Related Articles
how to create spring boot application