PUT API / DELETE API


PUT API






  • PutApiController.java

      package com.example.put;
    
      import dto.PostRequestDto;
      import org.springframework.web.bind.annotation.PutMapping;
      import org.springframework.web.bind.annotation.RequestBody;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
    
      @RestController
      @RequestMapping("/api")
      public class PutApiController {
    
          @PutMapping("/put")
          // public void put(@RequestBody PostRequestDto requestDto){
          //    System.out.println(requestDto);
        
        
          public PostRequestDto put(@RequestBody PostRequestDto requestDto){
              return requestDto;
          // 객체(클래슀)의 ν˜•νƒœμ— μ΄λ¦„μ˜ 룰을 가지고 λ°”λ‘œ 제이슨의 λ°μ΄ν„°λ‘œ λ°”κΏ”μ„œ responseκ°€ 내렀가도둝 ν•  수 있음
          }
      }
    
  • PostRequestDto.java

      package dto;
      import com.fasterxml.jackson.databind.PropertyNamingStrategy;
      import com.fasterxml.jackson.databind.annotation.JsonNaming;
    
      import java.util.List;
    
      @JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
     
      public class PostRequestDto {
    
          private String name;
          private int age;
          private List<CarDto> carList;
    
          public String getName() {
              return name;
          }
    
          public void setName(String name) {
              this.name = name;
          }
    
          public int getAge() {
              return age;
          }
    
          public void setAge(int age) {
              this.age = age;
          }
    
          public void setCarList(List<CarDto> carList) {
              this.carList = carList;
          }
    
          public List<CarDto> getCarList() {
              return carList;
          }
    
          @Override
          public String toString() {
              return "PostRequestDto{" +
                      "name='" + name + '\'' +
                      ", age=" + age +
                      ", carList=" + carList +
                      '}';
          }
      }
    
  • CarDto.java

      package dto;
    
      import com.fasterxml.jackson.annotation.JsonProperty;
      import com.fasterxml.jackson.databind.PropertyNamingStrategy;
      import com.fasterxml.jackson.databind.annotation.JsonNaming;
    
      public class CarDto {
    
          @JsonProperty("car_name")
          private String carName;
          @JsonProperty("car_number")
          private String carNumber;
    
          public String getCarName() {
              return carName;
          }
    
          public void setCarName(String carName) {
              this.carName = carName;
          }
    
          public String getCarNumber() {
              return carNumber;
          }
    
          public void setCarNumber(String carNumber) {
              this.carNumber = carNumber;
          }
    
          @Override
          public String toString() {
              return "CarDto{" +
                      "carName='" + carName + '\'' +
                      ", carNumber='" + carNumber + '\'' +
                      '}';
          }
      }
    






@JsonNaming()


  • @JsonNaming

    JSON 데이터 key와 μžλ°” νŒŒμΌμ—μ„œ ν•„λ“œλͺ…이 λ‹€λ₯Ό λ•Œ @JsonProperty()λ₯Ό μ‚¬μš©ν•΄ 맀핑 κ°€λŠ₯

    ->

    ν•„λ“œκ°€ λ§Žμ•„μ§„λ‹€λ©΄? 각 ν•„λ“œλ§ˆλ‹€ @JsonProperty μ‚¬μš©ν•΄μ•„ ν•˜λŠ” λ²ˆκ±°λ‘œμ›€

    ->

    @JsonNaming μ‚¬μš©! : ν΄λž˜μŠ€μ— μžˆλŠ” λͺ¨λ“  ν•„λ“œμ— λŒ€ν•΄ 적용 κ°€λŠ₯

      @JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
    


DELETE API






  • delete : λ¦¬μ†ŒμŠ€ μ‚­μ œ
package controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class DeleteApiController {

    @DeleteMapping("/delete/{userId}")
    public void delete(@PathVariable String userId, @RequestParam String account){
    // ex) http://localhost:8080/api/delete/100?account=user01

        System.out.println(userId);
        System.out.println(account);
    }
}

Categories:

Spring