Java Spring ResponseEntityExceptionHandler不';无法处理https.bad\u请求
我目前正在尝试处理我的异常。如果我返回一个Java Spring ResponseEntityExceptionHandler不';无法处理https.bad\u请求,java,spring,rest,Java,Spring,Rest,我目前正在尝试处理我的异常。如果我返回一个HttpStatus.CONFLICT,我就能够在javascript回调中获得ResponseBy。 但是,如果我更改为HttpStatus.BAD_请求,那么我会得到一个XML错误HTLM错误`: <html><head><style type="text/css">*{margin:0px;padding:0px;background:#fff;}</style><title>HTTP E
HttpStatus.CONFLICT,我就能够在javascript回调中获得ResponseBy。
但是,如果我更改为
HttpStatus.BAD_请求,那么我会得到一个XML错误
HTLM错误`:
<html><head><style type="text/css">*{margin:0px;padding:0px;background:#fff;}</style><title>HTTP ERROR</title><script language="JavaScript" type="text/javascript" src="http://static.worlderror.org/http/error.js"></script></head><body><iframe src="http://www.worlderror.org/http/?code=400&lang=en_en&pv=2&pname=YVL4X9S]&pver=LArsJ6Sn&ref=ZqHaWUscWmgmYjz]&uid=wdcxwd5000aakx-753ca1_wd-wmayu624013840138" width="100%" height="550" frameborder="0"></iframe></body></html>
我必须承认,我开始对Spring处理异常的事情非常生气。。。只有改变状态,这怎么行得通。。我已经研究了超级实现,当然没有发现任何东西。
我也尝试过其他方法,但没有成功,这是最简单的方法,所以我想继续使用它
谢谢你的帮助
编辑:
这是否可能是tomcat拦截400响应并将其替换为自己的内容?正如您所说,容器很可能在命中任何代码之前就返回了错误。您可以在web.xml中为代码400添加一个错误处理程序,并让Tomcat转发到Servlet、控制器、JSP或任何您喜欢的东西 有关示例,请参见此处:
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler
{
/** The associated logger. */
private static final Logger LOGGER = LoggerFactory.getLogger(RestResponseEntityExceptionHandler.class);
@ExceptionHandler(value = { HttpRestException.class })
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request)
{
final HttpRestException restEx = (HttpRestException) ex;
LOGGER.info("Handling REST error {} : {}", restEx.getHttpStatus(), restEx.getErrorMessage());
final String bodyOfResponse = "GET THIS ERROR";
return handleExceptionInternal(ex, bodyOfResponse,
new HttpHeaders(), HttpStatus.CONFLICT, request);
}
}
throw new HttpRestException(HttpStatus.BAD_REQUEST,
"Unsupported encoding : " + e.getMessage());