package com.ryun.shop;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<String> handler1(){
return ResponseEntity.status(400).body("에러남");
}
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handler(){
return ResponseEntity.status(400).body("에러남");
}
}
다음과 같이 기존에 있는 MyExceptionHandler를 지우지 않고
@PostMapping("/modify")
String modifyItem(@RequestParam Long id, @RequestParam String title, @RequestParam Integer price) throws Exception{
itemService.modifyItem(id,title,price);
return "redirect:/list";
}
public void modifyItem(Long id, String title, Integer price) throws Exception{
Item item = new Item();
item.setId(id);
item.setTitle(title);
item.setPrice(price);
if (item.getTitle().length()>=100 || item.getPrice()<0){
throw new Exception("가격은 0이상, 제목은 100자 미만으로 만드세요");
} else {
itemRepository.save(item);
}
}
다음과 같은 modifyItem 메소드에서 예외 발생시 가격은 0이상, 제목은 100자 미만으로 만드세요라는 제가 지정한 에러메세지를 출력하고 싶은데
어떻게 하면 할 수 있을지 여쭤보고 싶습니다.