Skip to main content

Request Mapping Annotation in Spring Boot

The @RequestMapping is a class level (also called type level) and method level annotation, it is used to process HTTP requests with specified URL patterns. It is used in and along with both @Controller and @RestController.

1. How @RequestMapping annotation it is used?

@Controller
@RequestMapping("/student")
public class StudentController{

  @RequestMapping("/dashboard")
  public String dashboard(){
  return "dashboard";
  }

 @RequestMapping("result")
 public String result(){
 return "result";
 }
}

We can see in above code sample "/student" , "/dashboard" and "result" passed with annotation are called request value/path present in the URL patterns.

The class StudentController will handle all the requests like example.com/students

And the member functions(methods) inside the class will handle the requests followed by the example.com/students/**

    dashboard() method will handle the request coming to example.com/students/dashboard

    result() method will handle the request coming to example.com/students/result

2. Optional Elements of @RequestMapping

All fields are public and abstract

Modifier and Type Optional Element and Description
String[] consumes
Narrows the primary mapping by media types that can be consumed by the mapped handler.
String[]

headers

The headers of the mapped request, narrowing the primary mapping.
RequestMethod[] method
The HTTP request methods to map to, narrowing the primary mapping: GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
String name
Assign a name to this mapping.
String[] params
The parameters of the mapped request, narrowing the primary mapping.
String[] path
The path mapping URIs
String[] produces
Narrows the primary mapping by media types that can be produced by the mapped handler.
String[] value
The primary mapping expressed by this annotation.

 

2.1 name, value and path

These elements are quit confusing, seems they are same.

  • name - As in servlet mapping we assing name to the servlet class and url pattern and later it used in web.xml, in spring we can also assign name to the mapping. 
  • value & path - according to official documentation both are alias of each other, both accept string array (see in table) ,both works similar, we can conversely use any of them. But the question arises, if they are doing same work then why they exist?

2.2 headers, consumes and produces

  • headers - Headers are the meta data or extra message attached to HTTP requests. If we want to filter the request mappings on the basis of headers we need to specify that. It narrows down the mapping e.g. 
     @RequestMapping(value = "/something", headers = "content-type=text/*")
    it will accept all content types like "text/html", "text/plain", etc. Also we can see it contains expression "content-type=text" we can also negate that with != i.e "content-type!=text/" which means it will accept all "/something" having headers with content-type "application/json", "application/xml" , "image/png", "image/, etc except "text/". Here * is wild card used for all.

 

3. Specialization of @RequestMapping

@RequestMapping specialiszations are created on basis of HTTP request methods, the other elements provided in the above table are used similarly as per the requirement.

  1. @GetMapping
  2. @PutMapping
  3. @PostMapping
  4. @DeleteMapping
  5. @PatchMapping

 

 

Popular posts from this blog

How to Implement AWS RDS Database IAM Authentication in Spring Boot

Amazon RDS for MySQL allows authentication using AWS Identity and Access Management (IAM) database authentication. With this authentication method, you don't need to use a password when you connect to a DB instance. Instead, you use an authentication token. Let us understand how this works? An authentication token is a unique string of characters that Amazon RDS generates on request. Authentication tokens are generated using AWS Signature Version 4. Each token has a lifetime of 15 minutes. You don't need to store user credentials in the database, because authentication is managed externally using IAM. You can also still use standard database authentication. Since IAM authentication tokens are short-lived access tokens that are valid for 15 minutes. For the RDS database this token works as a database password that is required to establish a connection and does not determine how long the existing connection can last. The default value for connection to be alive without activit

How to upload files in Amazon S3 Bucket using Spring Boot

As stated in the title, we are going to demonstrate that how we can upload and retrieve files from the amazon s3 bucket in spring boot. For this, we must have an account on amazon web services (AWS) . And the next thing you need to have is an IAM user that has programmatic access to the s3 bucket. Follow the steps below to create an IAM user and s3 bucket. Table of Contents 1. Steps to create an IAM user in AWS with S3 bucket full access permission Step 1.1 Login to your AWS account   Step 1.2 Set the user details Step 1.3 Set user permissions Step 1.4 Create a user group and set the access policy Step 1.5 Add user to the group Step 1.6  Set the tags (optional) Step 1.7  Review the user details and permission summary Step 1.8 Download the user credentials 2. See, how to create s3 bucket. Step 2.1 Click on the "Create bucket" button. Step 2.2 Enter the bucket name and select bucket region. Step 2.3 Set file accessibility for bucket items as public/private

What Is SSL Certificate and how it works?

Deep Dive into SSL Certificate What Is an SSL Certificate? SSL (Secure Sockets Layer) is the common name for TLS (Transport Layer Security), a security protocol that enables encrypted communications between two machines. An SSL certificate is a small data file leveraging this security protocol to serve two functions: Authentication – SSL certificates serve as credentials to authenticate the identity of a website. They are issued to a specific domain name and web server after a Certificate Authority, also known as a Certification Authority (CA), performs a strict vetting process on the organization requesting the certificate. Depending on the certificate type, it can provide information about a business or website's identity and authenticate that the website is a legitimate business. Secure data communication - When SSL is installed on a web server, it enables the padlock to appear in the web browser. It activates the HTTPS protocol and creates a secure connection between th

How to Implement Spring Security in Spring Boot

Security Example in Spring Boot Implementation of Spring Security in the Spring Boot application is the key point to learn for spring boot developers. Because Authentication and Authorization are the backbones of the whole application. Getting started with the Spring Security Series, this is the first part, in this article we are going to focus on the authentication part with minimal registration. The implementation of registration flow with email verification, customizing password encoding, and setting up password strengths and rules will be explored in another separate article for each.  This article will be the base of the spring security series, the other security features will be explained on the basis of this implementation, so be focused and let's understand. The code contains proper naming & brief comments that makes it very comprehensive. If you feel any difficulty or find any issue, please drop a comment below this post The main goal of this article is to impleme

Basic Annotations in Spring Boot

Annotations are the key components. Spring Boot annotations are simple and self-explanatory. There are a large number of annotations are implemented in Spring  Core. But In Spring Boot most of the annotations are enhanced and combined in simpler ones to write fewer lines of code. For example: If you are ever worked with spring core you need to apply @Configuration , @EnableAutoConfiguration and @ComponentScan . Now In Spring Boot, these three are combined into @SpringBootApplication . Here we have listed 32 annotations and explained, in brief, what every beginner should know. If you understand the use and start playing with these annotations no one can stop you to become a master in spring boot. We will provide the detailed example later on so please subscribe to this blog from the footer section by providing an email id: Table of Contents Basic Annotations in Spring Boot 1.@SpringBootApplication 2.@Component 3.@Service 4.@Repository 5.@Controller 6.@RestController 7.@A