PUT API / DELETE API
12 Oct 2021 -
1 minute read
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);
}
}