[TIL] Spring Basics - Request Data Handling
This is a TIL that summarizes how to receive request data using Path Variable and Request Param from Spring.
한국어 원문은 여기에서 볼 수 있습니다.
[TIL] Spring Basics - Request Data Handling
What to do today
What I studied
Path Variable and Request Param
- When sending an HTTP request from the client (browser) to the server, data can be sent together.
- Path Variable method
- You can add the data you want to send to the server to the URL path. 🌐 GET http://localhost:8080/hello/request/star/Robbie/age/95
1 2 3 4 5 6 7 8 // [Request sample] // GET http://localhost:8080/hello/request/star/Robbie/age/95 @GetMapping("/star/{name}/age/{age}") @ResponseBody public String helloRequestPath(@PathVariable String name, @PathVariable int age) { return String.format("Hello, @PathVariable.<br> name = %s, age = %d", name, age); }
- Request Param method
- You can add the data you want to send to the server using
?and&at the end of the URL path. 🌐 GET http://localhost:8080/hello/request/form/param?name=Robbie&age=95
1 2 3 4 5 6 7 // [Request sample] // GET http://localhost:8080/hello/request/form/param?name=Robbie&age=95 @GetMapping("/form/param") @ResponseBody public String helloGetRequestParam(@RequestParam String name, @RequestParam int age) { return String.format("Hello, @RequestParam.<br> name = %s, age = %d", name, age); }form tag POST
- You can send an HTTP request using the POST method using the HTML form tag. 🌐 POST http://localhost:8080/hello/request/form/param
- At this time, the data is delivered to the server in the form of
name=Robbie&age=95in the HTTP body. ``` html
1
2
3
4
5
6
7
8
9
10
11
12
``` java
// [Request sample]
// POST http://localhost:8080/hello/request/form/param
// Header
// Content type: application/x-www-form-urlencoded
// Body
// name=Robbie&age=95
@PostMapping("/form/param")
@ResponseBody
public String helloPostRequestParam(@RequestParam String name, @RequestParam int age) {
return String.format("Hello, @RequestParam.<br> name = %s, age = %d", name, age);
}
@RequestParamcan be omitted -@RequestParam(required = false)- If the required option is set to false, an error will not occur even if the corresponding value is not included in the values received from the client.
- The variable that did not receive a value from the client is initialized to null. (If
required = falseis not set, an error occurs)
How to process HTTP data as objects
@ModelAttribute
- form tag POST 🌐 POST http://localhost:8080/hello/request/form/model
1 2 3 4 5 6 7 8 9 10 11 // [Request sample] // POST http://localhost:8080/hello/request/form/model // Header // Content type: application/x-www-form-urlencoded // Body // name=Robbie&age=95 @PostMapping("/form/model") @ResponseBody public String helloRequestBodyForm(@ModelAttribute Star star) { return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age); }
- You can send an HTTP request using the POST method using the HTML form tag. -> At this time, the data is delivered to the server in the form of
name=Robbie&age=95in the HTTP body.- After using the
@ModelAttributeannotation, body data is declared as an object to receiveStar star.- Query String method 🌐 GET http://localhost:8080/hello/request/form/param/model?name=Robbie&age=95
1 2 3 4 5 6 7 // [Request sample] // GET http://localhost:8080/hello/request/form/param/model?name=Robbie&age=95 @GetMapping("/form/param/model") @ResponseBody public String helloRequestParam(@ModelAttribute Star star) { return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age); }
- If there are only two pieces of data like
?name=Robbie&age=95, it is okay, but if there are multiple pieces of data, it may be difficult to receive them one by one with the@RequestParamannotation. -> At this time, you can receive data as a Java object by using@ModelAttribute. - TheStarobject declared in the parameter is created and contains the value ofname & agerequested through the overloaded constructor or Setter method.
@ModelAttributecan be omitted.- Both
@ModelAttributeand@RequestParamcan be omitted. How to distinguish them in Spring- Spring considers the parameter as
@RequestParamif it is SimpleType; otherwise, it determines that@ModelAttributeis omitted.
- Spring considers the parameter as
@RequestBody
- When sending JSON data to the server in the HTTP Body, the body data can be delivered as a Java object.
- Body JSON data 🌐 POST http://localhost:8080/hello/request/form/json
1 2 3 4 5 6 7 8 9 10 11 // [Request sample] // POST http://localhost:8080/hello/request/form/json // Header // Content type: application/json // Body // {"name":"Robbie","age":"95"} @PostMapping("/form/json") @ResponseBody public String helloPostRequestJson(@RequestBody Star star) { return String.format("Hello, @RequestBody.<br> (name = %s, age = %d) ", star.name, star.age); }- When data is delivered to the server in the form of
{"name":"Robbie","age";"95"}JSONin the HTTP Body, the data can be received in object form using the@RequestBodyannotation.
- Things to keep in mind when receiving data as an object
- A set or get method or overloaded constructor is required to insert data into the field of the object.
####