Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags more
Archives
Today
Total
관리 메뉴

요리사에서 IT개발자로

스파르타 부트캠프 Spring Master 1강 HTTP데이터를 객체로 처리하는 방법 본문

Spring

스파르타 부트캠프 Spring Master 1강 HTTP데이터를 객체로 처리하는 방법

H.S-Backend 2024. 5. 17. 20:04

@PostMapping("/form/model")
@ResponseBody
public String helloRequestBodyForm(@ModelAttribute Star star) {
         return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age);
}

@ModelAttribute

에너테이션을 사용하여 HTTP 내에 Body데이터를 받아올 Star star객체를 선언

@RequestParam 에너테이션으로 데이터를 하나씩 받아오기 힘들 때

@ModelAttribute를 사용하면 Java의 객체로 데이터를 받아올 수 있다.

@PostMapping("/form/json")
@ResponseBody
public String helloPostRequestJson(@RequestBody Star star)
{
           return String.format("Hello, @RequestBody.<br> (name = %s, age = %d) ", star.name, star.age);
}

@RequestBody

HTTP Body에 JSON 데이터를 담아서 서버에 전달할 때

해당 Body데이터를

Java의 객체로 전달 받을 수 있다.

 


데이터를 Java의 객체로 받아올 때 주의점

 

해당 객체의 필드에 데이터를 넣어주기 위해서

set 또는 get 메서드 또는 오버로딩 된 생성자가 필요하다.

없다면 받아온 데이터를 해당 객체의 필드에 담을 수 없다.

데이터가 제대로 들어오지않는다면 set, get 오버로딩된 생성자 유무를 확인하면된다.

https://hs-backend.tistory.com/102

 

스파르타 부트캠프 Spring Master 1강 Path Variable과 Request Param

// [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("He

hs-backend.tistory.com

 

반응형