【发布时间】:2017-11-15 02:12:51
【问题描述】:
我有一个@ControllerAdvice 类,它捕获各种类型的自定义异常。问题是,这些自定义异常可以在 Controller 中抛出我的各种方法。
因此,在我的 @ControllerAdvice 类中,当需要 Splunk 异常时,我需要知道发生了什么具体操作,以便将正确的代码放入我的 Splunk 日志中。
例如:
@RestController
@RequestMapping(value = "/person/{id}")
public class PersonController {
public ResponseEntity<PersonDto> getPerson(@PathVariable final String id) throws PersonNotFoundException {
public ResponseEntity<PersonStuffDto> getPersonStuff(@PathVariable final String id) throws PersonNotFoundException {
...以及我的建议:
@ControllerAdvice
public class PersonControllerExceptionHandler {
@ExceptionHandler(PersonNotFoundException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED) @ResponseBody
public ErrorResponse personNotFoundException (final PersonNotFoundException exception, final HttpServletRequest request) {
... Splunk ...
}
在 personNotFoundException() 方法中,我需要 Splunk “RETRIEVING_PERSON”代码或“RETRIEVING_PERSON_STUFF”代码。
如何区分异常的来源?我是否必须检查请求...查看 url 或其他内容?如果是这样的话......将异常处理或至少这一部分留在控制器本身中不是更容易吗?
【问题讨论】:
-
为什么要在抛出相同异常并在通用实现中捕获它时区分调用方法?要么抛出不同的异常,要么在控制器中处理它们。
-
我明白你在说什么。只是,这两种方法都对Person进行了查找。一个返回 Person 本身,另一个返回那个 Person's Stuff。因此,虽然异常 - PersonNotFoundException - 是相同的,但我需要记录它发生的where。因此我的问题。
标签: spring model-view-controller exception-handling