Spring boot Get Method Example with Path Variables and Request Params

Spring boot Get Method Example with Path Variables and Request Params

This article helps you to understand the GET method of Spring Boot. In this article, I used IntelliJ as my IDEA and Postman to send the request to my application, so you have to install them before going to the implementation part and you have to read my previous article as well.

This is my scenario, I want to search for a student from his name and age from the database. Then I need to send name and age to the controller as my parameters. There are two types of parameters, those are

  1. PathVariable – Those data are sending in the URL. In the application end define as /test/{version}/hello or /courses/{courseid}
  2. RequestParam – This kind of data sent through the request body as a JSON formate.

The below steps help you to build the application.

Firstly thing is you need to understand the structure of the project. If you make any mistake when the runtime you will be able to see the white label error page. Don’t worry it can easily fix placing correct file structure.

This is the structure of a project.

Below is the pom file. It is used to store the all dependencies those are needed to run the application.

<?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 http://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.1.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>learnspringboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>learnspringboot</name>
	<description>test app</description>

	<properties>
		<java.version>1.8</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>

Then you can create or copy this Controller class – you should carefully read this. I used PathVariable and RequestParam discussed above. The other thing is you have to aware of annotations here I used @RestController since this is rest service and send a response.

package com.example.learnspringboot.Controller;

import org.springframework.web.bind.annotation.*;

@RestController
public class GetTestController {

    @RequestMapping(method = RequestMethod.GET, value = "/test/{version}/hello")
    public String getData(@PathVariable(value = "version") String version,
                          @RequestParam(value = "name") String name,
                          @RequestParam(value = "age") String age) {
        return "Version is : " + version + " Name is : " + name + " Age is :" + age;
    }
}

This is my Sample JSON Payload. You have to use Postman to send the requests. Once you build the application you can send requests and can understand GET method of Spring Boot.

{
	"name" : "Sarah"
	"age" : "29"
}

Git Location – https://github.com/rajithabhanuka/Learnspringboot.git