gpt4 book ai didi

spring - Spring REST Controller 的异常处理程序

转载 作者:行者123 更新时间:2023-12-05 01:48:41 24 4
gpt4 key购买 nike

我想处理异常,以便 URL 信息自动显示给客户端。有没有简单的方法可以做到这一点?

<bean id="outboundExceptionAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<!-- what property to set here? -->
</bean>

最佳答案

你有两个选择:

Spring Reference 15.9.1 HandlerExceptionResolver

Spring HandlerExceptionResolvers ease the pain of unexpected exceptions that occur while your request is handled by a controller that matched the request. HandlerExceptionResolvers somewhat resemble the exception mappings you can define in the web application descriptor web.xml. However, they provide a more flexible way to handle exceptions. They provide information about which handler was executing when the exception was thrown. Furthermore, a programmatic way of handling exceptions gives you more options for responding appropriately before the request is forwarded to another URL (the same end result as when you use the servlet specific exception mappings).

HandlerExceptionResolver有一个方法,包含你需要的一切:

HandlerExceptionResolver.resolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler, Exception ex)

或者如果您需要针对不同的 Controller 使用不同的处理程序:Spring Reference Chapter 15.9.2 @ExceptionHandler

@ExceptionHandler(IOException.class)
public String handleIOException(IOException ex, HttpServletRequest request) {
return "every thing you asked for: " + request;
}

简答题

关于spring - Spring REST Controller 的异常处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8178319/

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