JPA2 - API ๊ฐœ๋ฐœ ๊ธฐ๋ณธ

youngยท2023๋…„ 5์›” 21์ผ
0

Spring Boot

๋ชฉ๋ก ๋ณด๊ธฐ
14/19
post-thumbnail

์ธํ”„๋Ÿฐ ๊ฐ•์˜
๐Ÿ”— ์‹ค์ „! ์Šคํ”„๋ง ๋ถ€ํŠธ์™€ JPA ํ™œ์šฉ2 - API ๊ฐœ๋ฐœ๊ณผ ์„ฑ๋Šฅ ์ตœ์ ํ™”

๐Ÿ–‡๏ธAPI ๊ฐœ๋ฐœ

@RestController ์‚ฌ์šฉ

  • @Controller ์–ด๋…ธํ…Œ์ด์…˜๊ณผ @ResponseBody ์–ด๋…ธํ…Œ์ด์…˜ ํฌํ•จ

๋“ฑ๋ก V1 - ์—”ํ‹ฐํ‹ฐ๋ฅผ RequestBody์— ์ง์ ‘ ๋งคํ•‘

@PostMapping("/api/v1/members")
public CreateMemberResponse saveMemberV1(@RequestBody @Valid Member member){
        Long id = memberService.join(member);
        return new CreateMemberResponse(id);
}
@Data
static class CreateMemberResponse{
    private Long id;
    public CreateMemberResponse(Long id){
        this.id = id;
    }
}

๐Ÿšจ๋ฌธ์ œ์ (ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋ฐ›์„ ๊ฒฝ์šฐ)
1. ์—”ํ‹ฐํ‹ฐ์— ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๊ณ„์ธต์„ ์œ„ํ•œ ๋กœ์ง๊ณผ ๊ฒ€์ฆ(@NotEmpty)๋“ฑ ๋กœ์ง ์ถ”๊ฐ€๋จ
2. ํ•œ ์—”ํ‹ฐํ‹ฐ์— ๋‹ค์–‘ํ•œ ์š”๊ตฌ์‚ฌํ•ญ ํฌํ•จ์— ์–ด๋ ค๊ณผ ๋™์‹œ์— ์—”ํ‹ฐํ‹ฐ ๋ณ€๊ฒฝ ์‹œ, ์š”์ฒญ๋œ API ์ŠคํŽ™์ด ๋ณ€๊ฒฝ๋จ
=> API ์š”์ฒญ ์ŠคํŽ™์— ๋งž์ถฐ์„œ ๋ณ„๋„์˜ dto๋ฅผ ๋ฐ›๋Š” ๊ฒŒ ์ข‹๋‹ค.

๋“ฑ๋ก V2 - ์—”ํ‹ฐํ‹ฐ ๋Œ€์‹  DTO๋ฅผ RequestBody์— ์ง์ ‘ ๋งคํ•‘

@PostMapping("/api/v2/members")
public CreateMemberResponse saveMemberV2(@RequestBody @Valid
CreateMemberRequest request) {

	Member member = new Member();
 	member.setName(request.getName());
    
 	Long id = memberService.join(member);
 	return new CreateMemberResponse(id);
}
@Data
static class CreateMemberRequest {
 	private String name;
}
  1. ์—”ํ‹ฐํ‹ฐ์™€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๊ณ„์ธต์„ ์œ„ํ•œ ๋กœ์ง ๋ถ„๋ฆฌ๊ฐ€๋Šฅ
  2. ์—”ํ‹ฐํ‹ฐ์™€ API์ŠคํŽ™ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ถ„๋ฆฌ ๊ฐ€๋Šฅ ๋ฐ ์—”ํ‹ฐํ‹ฐ ๋ณ€ํ˜•์—๋„ API ์ŠคํŽ™์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€

๐Ÿ’ก ์‹ค๋ฌด์—์„œ๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ API ์ŠคํŽ™์— ๋…ธ์ถœํ•˜๋ฉด ์•ˆ๋œ๋‹ค !

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ • ๋ณ€๊ฒฝ ํ™•์ธ

  jpa:
    hibernate:
      ddl-auto: none

์ˆ˜์ • V2

@PutMapping("/api/v2/members/{id}")
public UpdateMemberResponse updateMemberV2(@PathVariable("id") Long id,
                                               @RequestBody@Valid updateMemberRequest request){
        memberService.update(id, request.getName());
        Member findMember = memberService.findOne(id);
        return new UpdateMemberResponse(findMember.getId(), findMember.getName());
}
@Data
@AllArgsConstructor
static class UpdateMemberResponse{
    private Long id;
    private String name;
}

์กฐํšŒ V1 - ์‘๋‹ต ๊ฐ’์œผ๋กœ ์—”ํ‹ฐํ‹ฐ ์ง์ ‘ ์™ธ๋ถ€์— ๋…ธ์ถœ

@GetMapping("/api/v1/members")
    public List<Member> membersV1(){
        return memberService.findMembers();
}

๐Ÿšจ๋ฌธ์ œ์ 
1.๊ธฐ๋ณธ์ ์œผ๋กœ ์—”ํ‹ฐํ‹ฐ์˜ ๋ชจ๋“  ๊ฐ’ ๋…ธ์ถœ
2. ์‘๋‹ต ์ŠคํŽ™์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๋กœ์ง ์ถ”๊ฐ€ (@JsonIgnore ๋“ฑ)
3. ์ถ”๊ฐ€๋กœ ์ปฌ๋ ‰์…˜์„ ์ง์ ‘ ๋ฐ˜ํ™˜ํ•˜๋ฉด ํ•ญํ›„ API ์ŠคํŽ™์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์–ด๋ ค์›€(๋ณ„๋„์˜ Result ํด๋ž˜์Šค ์ƒ์„ฑ์œผ๋กœ ํ•ด๊ฒฐ)

์กฐํšŒ V2 - ์—”ํ‹ฐํ‹ฐ ์•„๋‹Œ ๋ณ„๋„์˜ DTO ์‚ฌ์šฉ

@GetMapping("/api/v2/members")
    public Result membersV2() {
        List<Member> findMembers = memberService.findMembers();
        //์—”ํ‹ฐํ‹ฐ -> DTO ๋ณ€ํ™˜
        List<MemberDto> collect = findMembers.stream()
                .map(m -> new MemberDto(m.getName()))
                .collect(Collectors.toList());
        return new Result(collect);
    }
    
@Data
@AllArgsConstructor
static class Result<T> {
    private T data;
}
@Data
@AllArgsConstructor
static class MemberDto {
    private String name;
}

=> ์—”ํ‹ฐํ‹ฐ๊ฐ€ ๋ณ€ํ•ด๋„ API ์ŠคํŽ™ ๋ณ€๊ฒฝ X

์—”ํ‹ฐํ‹ฐ๋ฅผ ์™ธ๋ถ€์— ๋…ธ์ถœํ•˜์ง€ ์•Š๊ธฐ

  • ์‹ค๋ฌด์—์„œ๋Š” member ์—”ํ‹ฐํ‹ฐ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ API๊ฐ€ ๊ณ„์† ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค. ์–ด๋–ค API๋Š” name ํ•„๋“œ๊ฐ€ ํ•„์š”ํ•˜์ง€๋งŒ, ์–ด๋–ค API๋Š” name ํ•„๋“œ๊ฐ€ ํ•„์š”์—†์„ ์ˆ˜ ์žˆ๋‹ค.
    => ๊ฒฐ๋ก ์ ์œผ๋กœ ์—”ํ‹ฐํ‹ฐ ๋Œ€์‹ ์— API ์ŠคํŽ™์— ๋งž๋Š” ๋ณ„๋„์˜ DTO ๋…ธ์ถœ!
profile
เธ…ส•โ€ขอกฬซโ€ขส”เธ…

0๊ฐœ์˜ ๋Œ“๊ธ€