Saturday, 4 May 2019

Explain @RequestMapping Annotation in Spring MVC

@RequestMapping annotation is used for mapping web requests to particular handler classes or handler methods. The classes or methods that are annotated with @RequestMapping will be scanned and registered as the URI mappings for that specific class or method. Any future web requests will be redirected to that class or method. This annotation is part of Spring MVC module.



This annotation can be used in class level or method level. If you are using at the class level, that will be set as the common relative path for all the methods inside the class. If you are using at the only method level, each will be acting as its own relative path from the application’s context root. This tutorial has examples to makes you understand the difference when you are annotating at the class level and method level.

RequestMapping in Spring MVC
RequestMapping in Spring MVC
RequestMapping Annotation Parameters
The following are the parameters accepted by this annotation:

consumes: This argument narrows down the acceptable media format from the request.

headers: The headers of the mapped request, narrowing the primary mapping.

method: This argument specifying the HTTP methods can be redirected to that method. More than one can be specified by separating using commas. The HTTP methods that are used are GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE

name: This is simply a name to the mapping. This can be specified at the class level and the method level. If you specify at both the levels, then a combined name is derived by concatenation with “#” as a separator.

params: The parameters of the mapped request, narrowing the primary mapping.

path: This is an alias for the value argument. the path mapping URIs (e.g. “/myPath.do”). Ant-style path patterns are also supported (e.g. “/myPath/*.do”). At the method level, relative paths (e.g. “edit.do”) are supported within the primary mapping expressed at the type level.

produces: This argument narrows down the returned media format to the response.

value: This is the primary mapping used for mapping the web request to that method.

Class Level Mapping

If you declare @RequestMapping at the class level, the path will be applicable to all the methods in the class. Let’s look at the following example code snippet:

@Controller
@RequestMapping("/millets")
public class SpringMVCController {
	@RequestMapping(value="/foxtail")
	public String getFoxtail(){
		return "foxtail";
	}
	@RequestMapping(value="/finger")
	public String getFinger(){
		return "finger";
	}	
	public String getCommon(){
		return "common";
	}	
}


In the above code, the relative path /millets is enforced to all the methods inside the class. The method getFoxtail() will be invoked when user enter the URI /millets/foxtail. However, the last method getCommon() will not be invoked by any request because this method is not annotated with the @RequestMapping annotation.

Method Level Mapping

When there is no class level mapping and multiple method-level mappings are defined, each method would form a unique relative path. Let’s look at the below example code:

@Controller
public class SpringMVCController {
	@RequestMapping(value="/foxtail")
	public String getFoxtail(){
		return "foxtail";
	}
	@RequestMapping(value="/finger")
	public String getFinger(){
		return "finger";
	}	
}


In the above code, both the method have their individual relative paths /foxtail and /finger respectively.

HTTP Methods Mapping

One of the other ways to do the Request Mapping is to use the HTTP methods. You can specify in the @RequestMapping annotation that for which HTTP method request the method has to be invoked. Let’s look at the following code:


@Controller
public class SpringMVCController {
	@RequestMapping(value="/foxtail",method=RequestMethod.GET)
	public String getFoxtail(){
		return "foxtail";
	}

	@RequestMapping(value="/foxtail",method=RequestMethod.POST)
	public String getList(){
		return "foxtailList";
	}	

	@RequestMapping(value="/",method=RequestMethod.GET)
	public String getMillets(){
		return "millets";
	}	
}


In the above code, if you look at the first two methods of mapping to the same URI, but both have the different HTTP methods. The first method will be invoked when HTTP method GET is used and the second method is invoked when HTTP method POST is used. This kind of design is very useful for writing the REST APIs for your project.

Mapping using Params
Another very useful mapping technique is to use the parameters in the query string to filter the handler mappings. For example, you can restrict the handler mapping by configuring like if the URL query string has this parameter then go to this method or redirect to some other method.

Let’s look at this example snippet:

@RequestMapping(value="/millets",method=RequestMethod.GET,params="foxtail")

public String getFoxtail(){

	return "foxtail";

}


In the above example code, the request goes to this method only when the following criteria are satisfied:

If the URL is /millets and
If the HTTP method is GET and
If the query request has the parameter with the name foxtail

Spring Boot @ConfigurationProperties and Properties File

 In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s ...