SPRING BOOT @RequestMapping
SPRING BOOT
@RequestMapping maps URLs to specific Java classes or methods, to determine what code should be executed when a user visits a specific URL.
Spring Boot @RequestMapping annotation
➔ @RequestMapping is an annotation in Spring Boot that is used to create API endpoints by mapping web requests to specific handler classes or methods.
Key characteristics of Spring Boot @RequestMapping annotation:
➔ This annotation is used at the class level or method level or both levels.
➔ The method level API (for example @RequestMapping("/users")) is combined with the class level API (for example @RequestMapping("/api")) to create the final API (/api/users), if @RequestMapping is used for both levels.
➔ A single method can handle multiple URLs by using a string array in the value or path attribute, for example, @RequestMapping(value={"/url1","/url2"}).
➔ The value and path attributes in the @RequestMapping annotation are aliases for each other, and there is no functional difference between them, and one can be used instead of the other.
➔ By default the @RequestMapping annotation matches all HTTP methods (GET, POST, PUT, DELETE, HEAD, PATCH and OPTIONS), the method attribute is used to explicitly map a specific HTTP method, for example @RequestMapping(method="RequestMethod.GET").
@RequestMapping supports attributes to match the requested url.
➔ params: It checks whether the specified parameter name or value is present (i.e. params="id=101").
➔ headers: It checks whether the specified request header is present (i.e. headers="name=john").
➔ consumes: This specifies the media types consumed by the controller coming from the client (i.e. consumes="application/json")
➔ produces: This specifies the media types to be produced and sent to the client by the controller (e.g. produces="application/xml").
Manage dynamic URLs using @PathVariable.
➔ For example, @RequestMapping("/hello/{id}") can work with handlerName(@PathVariable Long id) to handle dynamic URLs.
Request URL is localhost:8090/users/hello/101
Note: Here '/users' is the class level request mapping.
Syntax
@RequestMapping(value="/hello/{id}", method = RequestMethod.GET)
public String helloHandler(@PathVariable Long id) {
return "RequestMapping & PathVariable is working fine, ID="+id;
}
Manage request parameters using @RequestParam.
➔ For example, @RequestMapping(/hello) can work with handlerName(@RequestParam(name = "name") String name) to handle query parameters.
The request URL is localhost:8090/users/hello?name=john
Note: Here '/users' is the class level request mapping.
Syntax
@RequestMapping(value="/hello", method = RequestMethod.GET)
public String helloHandler(@RequestParam("name") String n) {
return "RequestMapping & RequestParam is working fine, name="+n;
}
Best Practice:
➔ It is recommended to use HTTP method specific annotations such as @GetMapping, @PostMaping, @PutMapping, @DeleteMapping.
01 Open Spring Tool Suite (STS) IDE.
02 Create new project with Spring Starter Project.
Create porject in STS:
➔ name (Project Name): This is the name of the project as displayed in the development environment. It is optional, descriptive, and often uses the same name as artifactId. The project name can contain any mixed-letter words separated by hyphens. Spring Boot follows the Java naming convention.
➔ groupId: This identifier uniquely identifies the groupID across all projects. This can help avoid naming conflicts with other projects. The group ID can be viewed as a reserved Internet domain name, it does not necessarily have to be an actual domain name. The groupId can be named using any lowercase letter, separated by a dot (.), for example com.example, com.mydomain, etc.
➔ artifactId: This is the unique identifier and base name for the project within the groupId. This is usually the same as the project name in lowercase letters and is separated by a hyphen if there are multiple words. Spring Boot uses this name to create artifact files in .jar or .war format, for example hello-world-1.0.0.jar.
➔ packageName: This is the base Java package of the application and is used to organize the source code files. This helps avoid naming conflicts between classes in the application. The package name can be similar to a reserved domain name followed by words separated by dots. For example com.myexample.myapp, org.exam.app, com.exam.demo etc. By default Spring Boot uses groupId.arifactId as the package name and it is recommended to avoid using the default package name.
03 Provide information to the Spring Starter project.
04 Select dependency.
05 Create Controller.
06 Code for Controller.
Request URL is /users/101
Syntax
package com.exam.helloworld;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/users")
public class HelloController {
@RequestMapping(value="/hello", method = RequestMethod.GET)
public String helloHandler() {
return "RequestMapping is working fine";
}
}
07 Run HelloworldApplication.java
08 If there is a error like bellow, change the server default port.
09 Change the server default port in application.properties file.
10. After successfully run HelloworldApplication.java.
11. Test Restfull api in the browser. Type localhost:8090/users/hello in address bar of a browser.
Example @PathVariable
package com.exam.helloworld;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/users")
public class HelloController {
@RequestMapping(value="/hello/{id}", method = RequestMethod.GET)
public String helloHandler(@PathVariable Long id) {
return "RequestMapping & PathVariable is working fine, ID="+id;
}
}
@PathVariable output
Example @RequestParam
package com.exam.helloworld;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/users")
public class HelloController {
@RequestMapping(value="/hello", method = RequestMethod.GET)
public String helloHandler(@RequestParam("name") String n) {
return "RequestMapping & RequestParam is working fine, name="+n;
}
}
@RequestParam output