I'm not sure why but the @ControllerAdvice
is overriding the response code defined at Exception
level using the @ResponseStatus
annotation.
Exception:
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class GreetException extends RuntimeException {}
Controller:
@RestController
@RequestMapping("/")
public class GreetController {
@RequestMapping(method = RequestMethod.GET)
public String greet() {
throw new GreetException();
}
}
Controller Advice:
@ControllerAdvice
public class ExceptionConfiguration {
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(RuntimeException.class)
public void handleConflict() {}
}
When the greet method from the GreetController
gets called the response is 409 - CONFLICT. Because i specifically provided a response code on exception level I expected that will be the returned code (400 - BAD_REQUEST).
Of-course this is an overly simplified example but we defined an controller advice with RuntimeException
definition so we could assign an id to every exception that was not caught.
What is the correct way to achieve the expected behaviour?