gpt4 book ai didi

java - Spring-boot:未达到 RestControllerAdvise

转载 作者:行者123 更新时间:2023-12-01 17:49:32 25 4
gpt4 key购买 nike

我构建了一个RestControlerAdvise:

@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ExceptionControllerAdvice {

@ExceptionHandler({DocumentAlreadyExistsException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public cat.gencat.ctti.canigo.arch.web.rs.model.Error handleException(DocumentAlreadyExistsException e) {

cat.gencat.ctti.canigo.arch.web.rs.model.Error error = new cat.gencat.ctti.canigo.arch.web.rs.model.Error();
error.setCode(HttpStatus.BAD_REQUEST.value());
error.setMessage(e.getMessage());
return error;

}

}

尽管如此,即使我提出 DocumentAlreadyExistException,它也从未达到。

启动时检测到:

2018-08-20 17:08:25.791 INFO 4941 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in exceptionControllerAdvice

有什么想法吗?

最佳答案

我在我的应用程序中运行了相同的代码,并且工作正常,另请注意

@ExceptionHandler methods on the Controller are always selected before those on any @ControllerAdvice instance. It is undefined what order controller-advices are processed.

@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CustomExceptionHandler {

@ExceptionHandler(value={Exception.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public final ErrorMessage exceptionHandler(Exception e)
{
ErrorMessage msg=new ErrorMessage();
msg.setError("its an error");

return msg;
}

另请参阅以下链接以获取更多信息: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

关于java - Spring-boot:未达到 RestControllerAdvise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51933752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com