ํผ์์ ๊ณต๋ถ๋ฅผ ํ๋ ์ค @RequestBody๋ฅผ ์ฌ์ฉํ๋ ํด๋์ค์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๋ง๋ค์ด์ฃผ์ง ์์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์์ต๋๋ค.
@RequestBody๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ธ๋ฉ ํ ๋๋ ObjectMapper๊ฐ ์ฌ์ฉ๋๋ฉฐ, ์ด๋ ๊ฐ์ ๋ฐ์ธ๋ฉํด์ฃผ๊ธฐ ์ํด์๋ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ํ์์ ๋๋ค.
์ฐธ๊ณ ๋ก ObjectMapper๊ฐ ์ฌ์ฉ๋๋ฉด setter๊ฐ ์์ด๋ ๋ฐ์ดํฐ์ ๊ฐ์ด ๋ฐ์ธ๋ฉ ๋ ์ ์์ต๋๋ค.
์ด์ ๊ด๋ จํด์๋ ์๋ ๋ธ๋ก๊ทธ๋ค์ ์ฐธ๊ณ ํ์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
@RequestBody ๋ชจ๋ธ์ ๊ธฐ๋ณธ์์ฑ์, setter/getter๊ฐ ํ์ํ๊ฐ?
์๋ก ์ต๊ทผ์ Entity ๋ชจ๋ธ์ ์์ฑ์ ์ฑ๊ฒฉ์ ์ ์๊ฐํด๋ณด๊ณ ๋ถ๋ณํด์ผ ํ๋ ๊ฒ๋ค์ final๋ก ์ ์ธํด ๋ช ํํ๊ฒ ํ๋ผ๋ ์ฝ๋๋ฆฌ๋ทฐ๋ฅผ ๋ฐ์๋ค. ํ๋ฒ ์์ฑ๋๋ฉด ๋ณํ๋ฉด ์๋๋ ๊ฒ๋ค, ์ด ์์ฑ ์์ด ์์ฑ๋๋ฉด ์
bbbicb.tistory.com
https://jojoldu.tistory.com/407
@Request Body์์๋ Setter๊ฐ ํ์์๋ค?
ํ์ฌ์์ ๊ทผ๋ฌดํ๋์ค ์๋ก์ค์ ์ ์ ๊ฐ๋ฐ์๋ถ์ด ์ ์๊ฒ ํ๋์ ์ง๋ฌธ์ ํ์ต๋๋ค. POST ์์ฒญ์์ Setter ๊ฐ ํ์์๋๊ฒ ๊ฐ๋ค๊ณ . ์ฌํ ์ ๊ฐ ์๋๊ฒ๊ณผ๋ ๋ฌ๋ผ์ ์ด๋ป๊ฒ ๋ ์ผ์ธ์ง ๊ถ๊ธํ์ต๋๋ค. ์ ๋ง
jojoldu.tistory.com
์ด๋ฒ ๊ธ์ ์ฃผ์ ์ฃผ์ ์ธ record๋ @RequestBody์์ ๋ฐ์ธ๋ฉ ๋๋ ์ด์ ์ ๋ํด์ ์์๋ณด๊ฒ ์ต๋๋ค.
record ํด๋์ค์ ๋ชจ๋ ํ๋๋ final์ด๋ผ, ์ด๋ฅผ ์ฒ์ ์์ฑ์๋ฅผ ํตํด์ ๋ฐ๋์ ์ ํด์ฃผ์ด์ผ ํ๊ณ , ๋ฐ๋ผ์ ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ์์ต๋๋ค.
์ฆ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ์๊ธฐ์ record๋ @RequestBody์์ ๊ฐ์ ๋ฐ์ธ๋ฉํด์ค ์ ์๋ค๊ณ ์๊ฐํ์๋๋ฐ, ์ค์ ๋ก๋ ๊ฐ์ด ๋ฐ์ธ๋ฉ ๋์์ต๋๋ค.
์ด์ ๋ ๊ฐ๋จํ์ต๋๋ค.
record๋ ์กด์ฌํ๋ ์์ฑ์๊ฐ ์๋ค๋ฉด ์ปดํ์ผ๋ฌ๊ฐ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์์ฑํด์ค๋๋ค.
๊ทธ๋์ @RequestBody๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ํ ์ ์๋ ๊ฒ์ ๋๋ค.
๋ฐ๋ผ์ ๋ค์์ฒ๋ผ record๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๋๋ฅผ ์์ฑํ์ฌ๋ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
public record MemberUpdateDto(String name, String nickName,
Integer age) {
}
@PutMapping("/member")
@ResponseStatus(HttpStatus.OK)
public void updateBasicInfo(@RequestBody MemberUpdateDto memberUpdateDto) throws Exception {
memberService.update(memberUpdateDto);
}
Reference
https://docs.oracle.com/en/java/javase/14/language/records.html
Java Language Updates
JDK 14 introduces records, which are a new kind of type declaration. Like an enum, a record is a restricted form of a class. It’s ideal for "plain data carriers," classes that contain data not meant to be altered and only the most fundamental methods suc
docs.oracle.com
Programming tutorials | Newbedev
Checkout new tutorials and guides for programming languages Javascript, Python, Java, Golang, C++, PHP
newbedev.com
Define default constructor for record
I have a record and want to add default constructor to it. public record Record(int recordId) { public Record { } } But it created constructor with int param. public final class Record e...
stackoverflow.com
https://blogs.oracle.com/javamagazine/post/records-come-to-java#anchor_8