Java GlobalExceptionHandler始终返回500

Java GlobalExceptionHandler始终返回500,java,spring,spring-boot,exception,Java,Spring,Spring Boot,Exception,我有以下异常处理程序: @Log4j2 @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity handleException(Exception e) { logExceptionWithPath("Unhandled general exception", e); return new ResponseEn

我有以下异常处理程序:

@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);
}
在日志中,我总是使用logExceptionWithPath生成消息,因此ExceptionHandler似乎工作正常,但在某个地方存在IOException

每个异常(以及每个@ExceptionHandler)的响应都类似

下面是我的应用程序和spring的一些日志:

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
2019-06-13 17:26:00237调试*:102-资产术语不明确
2019-06-13 17:26:00237调试org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:403-使用@ExceptionHandler public org.springframework.http.ResponseEntity*.GlobalExceptionHandler.HandleAmbiguusterException(*.AmbiguusterException)
2019-06-13 17:26:00238错误*.GlobalExceptionHandler:92-路径的含糊不清异常:http://*/search
这里是来自AmbiguusterException的堆栈跟踪
2019-06-13 17:26:00239调试org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor:268-使用'application/json',给定[text/plain,application/json,application/cbor,application/*+json,*/*/*]并支持[application/json,application/*+json,application/json,application/cbor]
2019-06-13 17:26:00239调试org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor:90-编写[*。ErrorResponse@17bd7832]
2019-06-13 17:26:00240调试org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:143-已解决[*.AmbiguusterException:术语不明确]
2019-06-13 17:26:00240调试org.springframework.web.servlet.DispatcherServlet:1130-已完成400个错误请求
这些都是有趣的日志。也许这根本没有连接到Spring,而是它正在运行的服务器?
不管怎样,如果您有任何关于它不起作用的线索,请留下答案或评论。

您必须向方法添加响应状态,如
@ResponseStatus(HttpStatus.NOT_FOUND)

@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)//添加了此注释
@ExceptionHandler(歧义usterException.class)
公共响应处理模糊ustermexception(模糊ustermexception除外){
带有路径的LogException(“模糊UsterException”,例如);
返回新的ResponseEntity(新的ErrorResponse(例如getMessage()),HttpStatus.BAD_请求);
}
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