In this post, we will build a simple Spring boot REST API that handles request or query parameters in the GET HTTP request.
Spring Boot is an opinionated framework that helps developers build Spring-based applications quickly and easily. The main goal of Spring Boot is to quickly create Spring-based applications without requiring developers to write the same boilerplate configuration again and again.
We use @RequestParam to extract query parameters, form parameters, and even files from the request.
In this example, we use @RequestParam annotation to extract query parameters from the HTTP GET request.
Let's create a Spring boot application step by step.
1. Create Spring Boot Project
Use the below details in the Spring boot creation:
Project Name: springboot-first-app
Project Type: Maven
Choose dependencies: Spring Web
Package name: com.springboot.app
2. Maven Dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.app</groupId>
<artifactId>springboot-first-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-first-app</name>
<description>Spring Boot First Application</description>
<properties>
<java.version>16</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. Spring Boot REST API with Request Param
package com.springboot.first.app;
public class Student {
private String firstName;
private String lastName;
public Student(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
// build rest API to handle query parameters
// http://localhost:8080/student/query?firstName=Ramesh&lastName=Fadatare
@GetMapping("/student/query")
public Student studentQueryParam(
@RequestParam(name = "firstName") String firstName,
@RequestParam(name = "lastName") String lastName) {
return new Student(firstName, lastName);
}
}
- The above code uses Spring 4’s new @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @ResponseBody rolled together.
- @GetMapping annotation for mapping HTTP GET requests onto specific handler methods. Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).
- The Student object must be converted to JSON. Thanks to Spring’s HTTP message converter support, you don’t need to do this conversion manually. Because Jackson 2 is on the classpath, Spring’s MappingJackson2HttpMessageConverter is automatically chosen to convert the Student object to JSON.
- With the @RequestParam annotation, we extract query parameters from the HTTP GET request.
4. Run Spring Boot Application
The below class SpringbootFirstAppApplication is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.
package com.springboot.first.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootFirstAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstAppApplication.class, args);
}
}
package com.springboot.first.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootFirstAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstAppApplication.class, args);
}
}
Run spring boot application from the IDE:
From your IDE, run the SpringbootFirstAppApplication.main() method as a standalone Java class that will start the embedded Tomcat server on port 8080 and point the browser to http://localhost:8080/.
Run spring boot application using the command line:
Just go to the root directory of the application and type the following command to run it -
$ mvn spring-boot:run
The application will start at Spring Boot’s default tomcat port 8080.
Just hit this link in a browser: http://localhost:8080/student?firstName=Ramesh&lastName=Fadatare. You will able to see the response of this REST API in the browser.
Comments
Post a Comment