Open In App

How to Make get() Method Request in Java Spring?

Improve
Improve
Like Article
Like
Save
Share
Report

Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using JAVA is that Java tries to connect every concept in the language to the real world with the help of the concepts of classes, inheritance, polymorphism, etc.

There are several other concepts present in java that increase the user-friendly interaction between the java code and the programmer such as generic, Access specifiers, Annotations, etc these features add an extra property to the class as well as the method of the java program. In this article, we will discuss what is the GetMapping() annotation in java.

GetMapping() annotation mainly use in the spring boot applications that are used for handling the incoming request from the client by matching the incoming request header from the clientside

Syntax:

@GetMapping()

Parameters: Annotation contains an URL expression 

Now let us discuss how to initialize Spring in web projects. Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model. This model allows us to configure the list of dependencies that are supported by JVM. Here, we will create the structure of an application using spring initializer and then use an IDE to create a sample GET route.

Steps to initialize Spring in web projects

They are provided below in a sequential manner with visual aids as follows:

  1. Go to Spring Initializr
  2. Fill in the details as per the requirements. For this application shown below ascites:
Project: Maven
Language: Java 
Spring Boot: 2.2.8 
Packaging: JAR 
Java: 8 
Dependencies: Spring Web

Step 1: Click on Generate which will download the starter project. 
 

Step 2: Extract the zip file. Now open a suitable IDE and then go to File->New->Project from existing sources->Spring-boot-app and select pom.xml. Click on import changes on prompt and wait for the project to sync. 
 

Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.

Step 3: Go to src->main->java->com.gfg.Spring.boot.app, create a java class with name as Controller and add the annotation @RestController. Now create a GET API as shown below:”

@RestController

public class Controller {
  
  @GetMapping("/get") public String home() {
    return "This is the get request";
  }

  @GetMapping("/get/check") public String home1() {
    
    return "This is the get check request";
  }
}

Step 4: This application is now ready to run. Run the SpringBootAppApplication class and wait for the Tomcat server to start.

Note: The default port of the Tomcat server is 8080 and can be changed in the application.properties file.

Step 5: Now go to the browser and enter the URL localhost:8080. Observe the output and now do the same for localhost:8080/get/check

Geeks, we are done with get() method request in Spring as perceived from the above pop-up. Hence these were the steps at all.


Last Updated : 28 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads