Entity ์ž์ฒด๋ฅผ ์‘๋‹ตํ•˜๊ฑฐ๋‚˜ Entity๋ฅผ ํฌํ•จํ•˜์ง€ ๋ง์ž.

JSON์˜ ๋ฌดํ•œ ์ˆœํ™˜ ๊ตฌ์กฐ๋Š” ์™œ ๋ฐœ์ƒํ• ๊นŒ?  

๋‚˜๋งŒ์˜ ์˜ท์žฅ ์„œ๋น„์Šค์˜ REST API ๊ตฌํ˜„ํ•˜๋ฉด์„œ ๊ฐ€์žฅ ๊ณ ์ƒํ•œ ๋ถ€๋ถ„์ด๋‹ค.  

๊ฐœ๋ฐœ ์ดˆ๋ฐ˜์—๋Š” ์•„๋ฌด ์ƒ๊ฐ ์—†์ด DTO๋ฅผ ํ™œ์šฉํ•˜์ง€ ์•Š๊ณ  Entity๋ฅผ ์ง์ ‘ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฐ˜ํ™˜ํ–ˆ๋Š”๋ฐ ๊ฐ์ฒด๊ฐ€ ๋˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ทธ ๊ฐ์ฒด๊ฐ€ ๋˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฌดํ•œ ํ˜ธ์ถœ ํ˜„์ƒ์ด ๋ฐœ์ƒํ–ˆ๋‹ค.  ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฌดํ•œ ํ˜ธ์ถœ ํ˜„์ƒ์„ ๋Œ€์ถฉ ํ‘œํ˜„ํ•˜์ž๋ฉด 

a {
 b {
  c {
  }
 }
}


์ด๋Ÿฐ ์‹์œผ๋กœ a์•ˆ์— b๊ฐ€ ํฌํ•จ๋˜์–ด b์˜ ํ•„๋“œ๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ , ๋˜ b์˜ ํ•„๋“œ์— c๊ฐ€ ํฌํ•จ๋˜์–ด c์˜ ํ•„๋“œ๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ  ๋งŒ์•ฝ์— c์˜ ํ•„๋“œ์— ๋˜๋‹ค์‹œ aํ•„๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ๋ฌดํ•œ ํ˜ธ์ถœ ํ˜„์ƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ๊ตญ์— ์„œ๋ฒ„๊ฐ€ ์ฃฝ์–ด๋ฒ„๋ฆฌ๋Š” ์ƒํ™ฉ์„ ๋งž์ดํ•˜๊ฒŒ ๋œ๋‹ค. 

 

์œ„์™€ ๊ฐ™์ด ๋ฌดํ•œ ์ˆœํ™˜ ๊ตฌ์กฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  ๊ฐ์ฒด๋ฅผ JSON์œผ๋กœ ์–ด๋–ค์‹์œผ๋กœ ๋ณ€ํ™˜์‹œํ‚ค๋Š”์ง€์— ๋Œ€ํ•œ ํ™•์ธ์ด ํ•„์š”ํ–ˆ๋‹ค. @RestController์˜ ๋‚ด๋ถ€๋ฅผ ์‚ดํŽด๋ณด๋ฉด @ResponseBody ์—๋…ธํ…Œ์ด์…˜์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. @ResponseBody ์—๋…ธํ…Œ์ด์…˜์€ ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ์˜ ๋ฆฌํ„ด ํƒ€์ž…์„ ํ™•์ธํ•œ ํ›„ ์•Œ๋งž์€ MessageConverter๋ฅผ ์ฐพ์•„์„œ ์‘๋‹ต ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜์‹œ์ผœ ์ค€๋‹ค. ์œ„ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•œ Spring Boot๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” MappingJackson2HttpMessageConverter๋ฅผ ์ด์šฉํ•˜์—ฌ Json์œผ๋กœ Convert ํ•œ๋‹ค.

 

๋”ฐ๋ผ์„œ! ๋ฌธ์ œ์˜ ์›์ธ์„ ์ฐพ๊ธฐ ์œ„ํ•ด MappingJackson2HttpMessageConverter์˜ ๋ณ€ํ™˜ ๋™์ž‘ ๋ฐฉ์‹์„ ์‚ดํŽด๋ณด์ž. 

 

MappingJackson2HttpMessageConverter์˜ ๋™์ž‘ ๋ฐฉ์‹

MappingJackson2HttpMessageConverter๋Š” JSON์˜ ํ•„๋“œ๋ฅผ ์ฐพ์„ ๋•Œ Getter(), Setter()๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฐพ์•„๋‚ธ๋‹ค. ๊ฒฐ๊ตญ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋Š” ์–ด๋–ค Entity์˜ Getter(), Setter() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ•„๋“œ๋ฅผ ์ฐพ์•„๋‚ด๊ณ , ๋งŒ์•ฝ์— ๊ทธ ํ•„๋“œ๊ฐ€ Getter(), Setter()๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฉด dfs ์ฒ˜๋Ÿผ ์•ž์˜ ๊ณผ์ •์„ ๋˜ ๋‹ค์‹œ ๋ฐ˜๋ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— MappingJackson2HttpMessageConverter์˜ ๋™์ž‘ ๋ฐฉ์‹์— ์˜ํ•ด์„œ ๋ฌดํ•œ ํ˜ธ์ถœ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

๋”ฐ๋ผ์„œ ๋ฌดํ•œ ํ˜ธ์ถœ ํ˜„์ƒ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” DTO ๋‚ด์— ์ˆœํ™˜ ๊ตฌ์กฐ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ํฌํ•จ์‹œ์ผœ์„œ๋Š” ์•ˆ๋œ๋‹ค. ๋ฌผ๋ก  ์•„์˜ˆ ๊ฐ์ฒด๋ฅผ ํฌํ•จ์‹œํ‚ค์ง€ ๋ง๋ผ๋Š” ๋ง์ด ์•„๋‹ˆ๋ผ ์ˆœํ™˜ ๊ตฌ์กฐ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ํฌํ•จ์‹œ์ผœ์„œ๋Š” ์•ˆ๋œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. 

 

์ˆœํ™˜ ๊ตฌ์กฐ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ํฌํ•จ์‹œํ‚ค์ง€ ๋ง์ž

์ˆœํ™˜ ๊ตฌ์กฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์™„๋ฒฝํ•œ ๋ฐฉ๋ฒ•์€ ์—†๋‹ค. ์• ์ดˆ์— API๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ ์ด ๋ถ€๋ถ„์„ ์‹ ๊ฒฝ์จ์„œ ์„ค๊ณ„ํ•ด์•ผ ํ•œ๋‹ค. ์ˆœํ™˜ ๊ตฌ์กฐ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋Š” ๋ฌด์กฐ๊ฑด ์ œ์™ธํ•˜๊ณ  Entity๋ฅผ ์ง์ ‘ ๋ฐ˜ํ™˜์‹œํ‚ค๋Š” ์ƒํ™ฉ์€ ํ”ผํ•ด์•ผ ํ•œ๋‹ค. (๋ฌผ๋ก  JSON์œผ๋กœ ๋ณ€ํ™˜์‹œ์— ํ•ด๋‹น ํ•„๋“œ๋ฅผ ๋ฌด์‹œํ•˜๋Š” ๋ฐฉ์‹์ด ์žˆ์ง€๋งŒ Entity์™€ API๋Š” ์˜์กด์ ์ด์ง€ ์•Š์€ ๊ฒƒ์ด ์ข‹๋‹ค) 

 

@Getter
public class ResponseForWardrobe extends Response {
    private Long id;
    private Integer likeCnt;
    private String memberName;
    private String email;
    private String name;
    private String isPublic;
    private String imageServerPath;

    @Builder
    public ResponseForWardrobe(Wardrobe wardrobe) {
        this.id = wardrobe.getId();
        this.memberName = wardrobe.getMember().getName();
        this.email = wardrobe.getMember().getEmail();
        this.name = wardrobe.getName();
        this.isPublic = wardrobe.getIsPublic();
        this.likeCnt = wardrobe.getLikeCnt();
        this.imageServerPath = wardrobe.getImage().getImageServerPath();
    }
}