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">
		<relativePath/> <!-- lookup parent from repository -->
	<description>test app</description>






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.*;

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