1 minute read

πŸ“˜ API

APIλŠ” Application Programming Interface의 μ•½μžλ‘œ μš΄μ˜μ²΄μ œλ‚˜ μ‘μš©ν”„λ‘œκ·Έλž¨ μ‚¬μ΄μ˜ 톡신에 μ‚¬μš©λ˜λŠ” μ–Έμ–΄λ‚˜ 메세지 ν˜•μ‹μ„ μ˜λ―Έν•œλ‹€.
일반적으둜 λ‹€μŒμ˜ 4가지 λ©”μ„œλ“œλ₯Ό 주둜 μ‚¬μš©ν•˜λ©°, 뒀에 κΈ°λŠ₯으둜 주둜 쓰인닀.

  • GET - read
  • POST - create
  • PUT - update
  • DELETE - delete


πŸ“˜ Get λ©”μ„œλ“œ

URL의 κ²½λ‘œλ‚˜ νŒŒλΌλ―Έν„°μ— λ³€μˆ˜λ₯Ό λ„£μ–΄μ„œ μš”μ²­μ„ λ³΄λ‚΄λŠ” 방식이닀.

    @GetMapping("/getmapping")
    public String getMapping() {
        return "getMapping";
    }


πŸ“ @pathVariable

    @GetMapping("/getmapping2/{variable}")
    public String getMapping2(@PathVariable String variable) {
        return variable;
    }


πŸ“ @RequestParam

    @GetMapping("/getmapping3")
    public String getMapping3(
            @RequestParam String name,
            @RequestParam String email,
            @RequestParam String organization) {
        return name + " " + email + " " + organization;
    }


πŸ“ DTO 객체


πŸ“Œ dto

dto packageλ₯Ό λ§Œλ“€μ–΄ μ€€ ν›„, dto 객체λ₯Ό λ§Œλ“€μ–΄μ€€λ‹€.
alt + insert 단좕킀λ₯Ό ν†΅ν•΄μ„œ μ‰½κ²Œ getter & setter 와 toString을 생성가λŠ₯ν•˜λ‹€.

dto 객체
package com.example.test.dto;

public class MemberDto {
    private String name;
    private String email;
    private String organization;

    public void setName(String name) {
        this.name = name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public String getEmail() {
        return email;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "MemberDto{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", organization='" + organization + '\'' +
                '}';
    }
}
    @GetMapping("/getmapping4")
    public String getmapping4(MemberDto dto){
        return dto.toString();
    }


πŸ“˜ POST λ©”μ„œλ“œ

μ €μž₯ν•˜κ³ μž ν•˜λŠ” λ¦¬μ†ŒμŠ€λ‚˜ 값을 HTTP λ°”λ””(body)에 λ‹΄μ•„μ„œ μ„œλ²„μ— μ „λ‹¬ν•œλ‹€.
Body μ˜μ—­μ— μž‘μ„±λ˜λŠ” 값은 μΌμ •ν•œ ν˜•νƒœλ₯Ό μ·¨ν•˜λŠ”λ°, 일반적으둜 JSON ν˜•μ‹μœΌλ‘œ μ „μ†‘λœλ‹€.


πŸ“ DTO 객체

@RestController
@RequestMapping("/post")
public class PostController {
    @PostMapping("/postmapping1")
    public String PostMapping(@RequestBody MemberDto dto){
        return dto.toString();
    }
}


πŸ“˜ PUT λ©”μ„œλ“œ

μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ„œλ²„λ₯Ό 톡해 λ°μ΄ν„°λ² μ΄μŠ€ 같은 μ €μž₯μ†Œμ— μ €μž₯ν•˜λŠ” λ¦¬μ†ŒμŠ€ 값을 μ—…λ°μ΄νŠΈ ν•˜λŠ”λ° μ‚¬μš©ν•œλ‹€.


πŸ“ DTO 객체

@RestController
@RequestMapping("/put")
public class PutController {

    @PutMapping("/putMapping1")
    public String putMapping(@RequestBody MemberDto dto){
        return dto.toString();
    }
}


πŸ“˜ DELETE λ©”μ„œλ“œ

μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ„œλ²„λ₯Ό 톡해 λ°μ΄ν„°λ² μ΄μŠ€ 같은 μ €μž₯μ†Œμ— μ €μž₯ν•˜λŠ” λ¦¬μ†ŒμŠ€ 값을 μ—…λ°μ΄νŠΈ ν•˜λŠ”λ° μ‚¬μš©ν•œλ‹€.


πŸ“ DTO 객체

@RestController
@RequestMapping("/put")
public class PutController {

    @PutMapping("/putMapping1")
    public String putMapping(@RequestBody MemberDto dto){
        return dto.toString();
    }
}



개인 곡뢀 기둝용 λΈ”λ‘œκ·Έμž…λ‹ˆλ‹€.
ν‹€λ¦¬κ±°λ‚˜ 였λ₯˜κ°€ μžˆμ„ 경우 μ œλ³΄ν•΄μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€.😁