@RequestBody와 @ModelAttribute

Haechan Kim·2023년 5월 10일
0

Spring

목록 보기
40/68

@RequestBody와 @ModelAttribute 둘 다 클라이언트에서 보낸 데이터(JSON 등)를 오브젝트로 만들어 줌.

But 세부 수행 동작에서 크게 다르다. 프로젝트 도중 이 둘 때문에 에러 발생,,

@RequestBody

클라에서 보낸 HTTP 요청 본문(JSON)을 자바 오브젝트로 변환.
JSON은 스프링에서 제공하는 HttpMessageConverter 통해 변환.

Controller.java

@PostMapping("/requestbody")
public ResponseEntity<RequestBodyDto> testRequestBody(@RequestBody RequestBodyDto requestBodyDto) {
    return ResponseEntity.ok(requestBodyDto);
}

RequestBodyDto.java

public class RequestBodyDto {

    private String name;
    private long age;
    private String password;
    private String email;

    public RequestBodyDto() {
    }

    public RequestBodyDto(String name, long age, String password, String email) {
        this.name = name;
        this.age = age;
        this.password = password;
        this.email = email;
    }

    //Getter 및 Setter 추가
}

작성 중 ...

0개의 댓글