๐ก
1. @DeleteMapping ์ดํด
2. @PathVariable ์ฌ์ฉ
3. @RequestParam ์ฌ์ฉ
DeleteApiController
@RestController // Ioc ๋์ (์ฑ๊ธํค ํจํด์ผ๋ก ๊ด๋ฆฌ)
public class DeleteApiController {
// http://localhost:8080/delete/100?account=์ฐ๋ฆฌ์ํ
//@GetMapping("/delete/{userId}")
/**
* DELETE ์์ฒญ์ ์ฒ๋ฆฌ *
* ์ฃผ์ ์ค๊ณ (๋ธ๋ผ์ฐ์ ์์ ์ง์ ์์ฒญํ ์ ์์, ๋ณดํต Postman ๋ฑ์ผ๋ก ํ
์คํธ)
* http://localhost:8080/delete/100?account=์ฐ๋ฆฌ์ํ
*
* DELETE ๋ฐฉ์์ ๋ณธ๋ฌธ(HTTP ์์ฒญ ๋ฉ์์ง Body)๊ฐ ์๋ค.
* @PathVariable๊ณผ @RequestParam์ ํจ๊ป ์ฌ์ฉํ์ฌ ๊ฒฝ๋ก ๋ณ์์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ฅผ ๋์์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
*
* @param userId ์ญ์ ํ๋ ค๋ ์ฌ์ฉ์์ ID (๊ฒฝ๋ก ๋ณ์)
* @param account ์ญ์ ์์
๊ณผ ๊ด๋ จ๋ ๊ณ์ข ์ ๋ณด (์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ)
* @return ์ญ์ ๊ฒฐ๊ณผ๋ฅผ ํฌํจํ๋ ResponseEntity
* ์ถ๊ฐ ๊ณ ๋ ค ์ฌํญ
* ์ธ์ฆ ๋ฐ ๊ถํ ๊ฒ์ฌ
* ์๋ต ์ฒ๋ฆฌ: ์ญ์ ๊ฐ ์ฑ๊ณต์ฌ๋ถ์ ๋ฐ๋ผ ๋ค๋ฅธ ์ํ ๋ด๋ ค์ผ ํจ
* (์: 404 Not Found, 500 Internal Server Error ๋ฑ)
*/
@DeleteMapping("/delete/{userId}")
public ResponseEntity<?> delete(@PathVariable(name = "userId") String userId,
@RequestParam(name = "account") String account ) {
System.out.println("userId : " + userId);
System.out.println("account : " + account);
return ResponseEntity.status(200).body("์ ์ ์ญ์ ๋์์ต๋๋ค");
}
}
PutApiController
@RestController // Ioc (์ฑ๊ธํค ํจํด์ผ๋ก ๊ด๋ฆฌ ๋จ)
public class PutApiController {
/**
* ์ฃผ์์ค๊ณ
* http://localhost:8080/put/demo1/100
*
* @return JSON
*/
@PutMapping("/put/demo1/{id}")
public UserDTO putMethodName(@PathVariable(name = "id") String id,
@RequestBody UserDTO userDTO) {
System.out.println("id" + id);
System.out.println("userDTO" + userDTO.toString());
// ๋ฉ์ธ์ง ์ปจ๋ฒํฐ (UserDTO) ->> JSON ๋ฐํ์ ํด์ ---> ํด๋ผ์ด์ธํธ์ ์๋ต์ฒ๋ฆฌ ํ๋ค.
return userDTO;
}
// @PutMapping("/put/demo1/{id}")
// public UserDTO putMethodName(@PathVariable(name = "id") String id) {
// System.out.println("id" + id);
// // System.out.println("userDTO" + userDTO.toString());
// // ๋ฉ์ธ์ง ์ปจ๋ฒํฐ (UserDTO) ->> JSON ๋ฐํ์ ํด์ ---> ํด๋ผ์ด์ธํธ์ ์๋ต์ฒ๋ฆฌ ํ๋ค.
// return null;
// }
}
UserDTO
@Data
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public class UserDTO {
private String name;
private Integer age;
// @JsonProperty("car_list")
private List<CarDTO> carList;
// ๋น์ ์ ๋ด๋ถ ํด๋์ค (x) - static ์์๋
// ์คํ๋ง ๋ถํธ DTO์ค๊ณ์ ๋ด๋ถ ํด๋์ค๋ static ๋ด๋ถ ํด๋์ค๋ก ๋ง๋ค์
@Getter
@ToString
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
static class CarDTO{
private String name;
// @JsonProperty("car_number")
private String carNumber;
}
}
์คํ ๊ฒฐ๊ณผ____
'Spring boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring boot] ๋จ๋ฐฉํฅ, ์๋ฐฉํฅ ๋งคํ์ ๋ํ ์ดํด (2) (1) | 2024.10.01 |
---|---|
[Spring boot] ๋จ๋ฐฉํฅ, ์๋ฐฉํฅ ๋งคํ์ ๋ํ ์ดํด (1) (1) | 2024.10.01 |
[Spring boot] ์์์ฑ ์ปจํ ์คํธ๋? (0) | 2024.10.01 |
[Spring boot] ์ํฐํฐ ๋งค๋์ ์ ๋ํด ์์๋ณด์. (0) | 2024.10.01 |
[Spring boot] JPA ์ ํ์ด๋ฒ๋ค์ดํธ๋? (0) | 2024.10.01 |