In Spring Boot, we can get parameter values in a controller method in various ways depending on the type of parameter and its source.
Here are several common ways to retrieve parameter values in spring boot controller method:
- Path Variables : @PathVariable
- Request Parameters : @RequestParam
- Request Body : @RequestBody
- Request Headers : @RequestHeader
- Cookie Values : @CookieValue
- Matrix Variables : @MatrixVariable
- Form Data : @ModelAttribute
- File Uploads : @RequestParam with MultipartFile
- Servlet API : HttpServletRequest and HttpServletResponse
- Session Attributes : HttpSession
- Spring Security Principal : Principal
1. Path Variables
Path variables are part of the URL path.
@GetMapping("/user/{id}")public ResponseEntity<User> getUserById(@PathVariable("id") Long id) {// Use the id parameter}
2. Request Parameters
Request parameters are typically used in query strings. Like getting employee's information with employee ID numbers.
@GetMapping("/search")public ResponseEntity<List<User>> searchUsers(@RequestParam("id") String empId) {// Use the empId parameter}
3. Request Body
Request bodies are used to send complex data in a request. Typically receive JSON data to save/update.
@PostMapping("/user")public ResponseEntity<User> createUser(@RequestBody User user) {// Use the user object}
4. Request Headers
Request headers are additional information sent with the request. Like Authorization key or Token.
@GetMapping("/user")public ResponseEntity<User> getUser(@RequestHeader("Authorization") String authHeader) {// Use the authHeader}
5. Cookie Values
Cookies can be used to store small pieces of data on the client side.
@GetMapping("/cookie")public ResponseEntity<String> getCookieValue(@CookieValue("sessionId") String sessionId) {// Use the sessionId cookie}
6. Matrix Variables
Matrix variables are a way to pass parameters in the path segment.
@GetMapping("/user/{id}")public ResponseEntity<User> getUserById(@PathVariable("id") Long id, @MatrixVariable("name") String name) {// Use the id and name parameters}
7. Form Data
Form data is used for form submissions. Specially use for Java EE Applications.
@PostMapping("/submit")public ResponseEntity<String> submitForm(@ModelAttribute Form form) {// Use the form data}
8. File Uploads
File uploads are typically used for uploading files.
@PostMapping("/upload")public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {// Use the uploaded file}
9. Servlet API
we can access raw HTTP request and response objects.
@GetMapping("/servlet")public ResponseEntity<String> servletApiExample(HttpServletRequest request, HttpServletResponse response) {String param = request.getParameter("param");// Use the request and response objects}
10. Session Attributes
Session attributes are stored in the session scope.
@GetMapping("/session")public ResponseEntity<String> sessionExample(HttpSession session) {String attribute = (String) session.getAttribute("attribute");// Use the session attribute}
11. Spring Security Principal
we can access the authenticated user details.
@GetMapping("/principal")public ResponseEntity<String> principalExample(Principal principal) {String username = principal.getName();// Use the principal}
Using these annotations and objects, we can handle a wide variety of request parameters and data in our Spring Boot controller methods. In the next blog, we will see practically the most useful ways to get parameters.
0 Comments