120. Global Exception Handling with Controller Advice - dkkahm/study-springfamework5 GitHub Wiki
Advice
@ControllerAdvice
public class ControllerExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(NumberFormatException.class)
public ModelAndView handleNumberFormatException(Exception exception) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("400error");
return modelAndView;
}
}
Test
@BeforeEach
public void setUp() throws Exception {
....
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setControllerAdvice(new ControllerExceptionHandler())
.build();
}