Notice
Recent Posts
Recent Comments
Link
«   2026/09   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

helloworld

DELETE 기능 본문

restful

DELETE 기능

hellojava 2023. 8. 13. 14:54
public User deleteById(int id) {
//열거형데이터 배열 등을 순차적으로 접근하여 사용하기 위한
Iterator<User> iterator = users.iterator();
while (iterator.hasNext()) {
User user = iterator.next();

if (user.getId() == id) {
iterator.remove();
return user;
}
}
return null;
}

예제 코드를 만들때 간단히 하기 위해 List에 User를 담아 데이터를 만들어 놨었다.

 

delete 기능은 id값을 순회하여 들어온 id값과 같은id값이 있으면 그 아이디를 삭제하는 기능이다.

 

컨트롤러에서  deleteById를 호출하여 아이디를 삭제해보자.

 

@DeleteMapping("/users/{id}")
public void deleteUser(@PathVariable int id) {
User user = service.deleteById(id);
if (user == null) {
throw new UserNotFoundException(String.format("ID[$s] not found", id));
}
}

@DeleteMapping 을 사용하여 delete요청을 한다

 

만약 user가 없다면 404에러를 처리한다.

'restful' 카테고리의 다른 글

유효성 검사 (Validatation)  (0) 2023.08.13
PUT  (0) 2023.08.13
Exception 처리 심화과정 ..  (0) 2023.08.13
Exception 처리  (0) 2023.08.13
rest PostMapping 2  (0) 2023.08.13