当前位置: 首页 > 知识库问答 >
问题:

GlobalExceptionHandler始终返回500

锺离烈
2023-03-14

我有以下异常处理程序:

@Log4j2
@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public ResponseEntity handleException(Exception e) {
    logExceptionWithPath("Unhandled general exception", e);
    return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}

@ResponseBody
@ExceptionHandler({ValidationException.class})
public ResponseEntity<ErrorResponse> handleBindException(ValidationException ex) {
    logExceptionWithPath("Validation exception", ex);
    return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}


@ResponseBody
@ExceptionHandler({BindException.class})
public ResponseEntity<ErrorResponse> handleBindException(BindException ex) {
    logExceptionWithPath("Bind exception", ex);
    return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}

@ResponseBody
@ExceptionHandler(AmbiguousTermException.class)
public ResponseEntity<ErrorResponse> handleAmbiguousTermException(AmbiguousTermException ex) {
    logExceptionWithPath("AmbiguousTermException", ex);
    return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}

此代码应该包装异常并返回具有正确状态代码的ResponseEntity。

{
"timestamp": 1560445348350,
"status": 500,
"error": "Internal Server Error",
"message": "I/O error on POST request for \"******": Error code:400 Message: ; nested exception is java.io.IOException: Error code:400 Message: ",
"path": "*****"
}
2019-06-13 17:26:00,237 DEBUG *:102 - assertTermsUnequivocal
2019-06-13 17:26:00,237 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:403 - Using @ExceptionHandler public org.springframework.http.ResponseEntity<*.ErrorResponse> *.GlobalExceptionHandler.handleAmbiguousTermException(*.AmbiguousTermException)
2019-06-13 17:26:00,238 ERROR *.GlobalExceptionHandler:92 - AmbiguousTermException for path: http://*/search
here stack trace from AmbiguousTermException
2019-06-13 17:26:00,239 DEBUG org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor:268 - Using 'application/json', given [text/plain, application/json, application/cbor, application/*+json, */*] and supported [application/json, application/*+json, application/json, application/*+json, application/cbor]
2019-06-13 17:26:00,239 DEBUG org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor:90 - Writing [*.ErrorResponse@17bd7832]
2019-06-13 17:26:00,240 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:143 - Resolved [*.AmbiguousTermException: terms are ambiguous]
2019-06-13 17:26:00,240 DEBUG org.springframework.web.servlet.DispatcherServlet:1130 - Completed 400 BAD_REQUEST

共有1个答案

卢德惠
2023-03-14

您必须向方法添加响应状态,如@responsestatus(httpstatus.not_find)

@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST) // Added this annotation
@ExceptionHandler(AmbiguousTermException.class)
public ResponseEntity<ErrorResponse> handleAmbiguousTermException(AmbiguousTermException ex) {
    logExceptionWithPath("AmbiguousTermException", ex);
    return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}
 类似资料:
  • 问题内容: 我以前使用过媒体播放器,但从未遇到过此问题。每当我尝试使用MediaPlayer.create()时,该方法都会使我为null,并且无法播放声音。有什么我想念的吗? 我的sound.mp3在我的原始文件夹中,通过将声音拖到eclipse中的文件夹中,我将其放置在其中。请帮忙,因为我以前玩过声音,所以这真的困扰我:( 问题答案: 如果create() API由于某种原因失败,则返回nul

  • 问题内容: 尽管是有效的类,但以下代码会打印。 文档说方法返回 由 aClassName 命名的类对象,或者如果当前没有加载该名称的类。如果 aClassName 为,则返回。 我也试图获得当前的viewcontroller已加载但仍然得到 可能是什么问题? 更新: 即使尝试这样做,我仍然可以 问题答案: 该函数 确实 适用于(纯和Objective-C派生的)swift类,但是仅当您使用全限定名

  • 问题内容: 我觉得有点愚蠢,但它不起作用: 我有如果给定的用户是unicode。如果字符串中包含或,我想打印成功,但是我总是得到的结果。 问题答案: 隐式锚定到字符串的开头。如果要在字符串中搜索可以在字符串中任何位置的子字符串,则需要使用: 输出: 另外,Python Regexes不需要在开头和结尾都有一个。 最后,我添加到该行的末尾,因为我认为这就是您想要的。否则,您会得到类似的信息,但并不太

  • 我的安全配置似乎不正确。无论我在使用hasRole时做什么,我的endpoint总是返回403。 此外,除非我在这两个和。很明显,我遗漏了一些东西。 基本上,我希望所有内容都需要身份验证,但只有当用户是某些组的成员时(现在只需要admin),少数endpoint才可以访问。 我的安全配置如下。旁边的一切都有效。 我的AuthenticationConfiguration如下 我的Authoriza

  • 我正在尝试使用NSKeyDarchiver在应用程序关闭和运行周期之间存储数据。我试图存储的根对象是一个NSMutableArray,但在这个对象中有基础对象和自定义对象的集合(所有这些都符合NSCoding)。 文件保存代码如下 该文件保存fine并且不会抛出异常。再次启动应用程序时,使用以下代码恢复该文件。 在这一点上,sessionData总是为零,而pData是几千字节长的,因此我知道问题

  • 问题内容: 请弄清楚这一点。该代码正常运行,没有任何异常。 Buttttttttt return语句始终返回false,并且文件未上传到服务器上。请有人帮忙。 供您参考,1)我在办公室网络中。-–>我们需要添加任何代理吗? 请帮忙 问题答案: 确切的失败消息可以通过调用FtpClient#getReplyCode()来找到。从该页面(我的重点): 连接后立即是唯一实时的操作,您需要检查回复代码(因