[英]Trying to get Spring Boot app to send an error response message when request does not have "Content-type" HTTP request header
I am still really new with Spring Boot, but I have a web application, that is setup to handle either XML requests (when the requests have a "Content-type: application/xml" header, or JSON requests (when the requests have a "Content-type: application/xml" header), and I noticed that if the "Content-type" header is either missing, or contains something other than "Content-type: application/xml" or "Content-type: application/json", I get a "415" response error code. The logging also shows a message like: I am still really new with Spring Boot, but I have a web application, that is setup to handle either XML requests (when the requests have a "Content-type: application/xml" header, or JSON requests (when the requests have a “Content-type: application/xml” 标头),我注意到如果“Content-type” header 丢失,或者包含“Content-type: application/xml”或“Content-type: application/”以外的其他内容json”,我收到“415”响应错误代码。日志还显示如下消息:
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported]
I found this older SO thread:我发现了这个较旧的 SO 线程:
Springboot exception handler doesn't catch exception Springboot异常处理程序不捕获异常
and have been trying to implement the code that was posted by "Sannu" on Dec 17 '16 at 13:55.并一直在尝试实现由“Sannu”于 2016 年 12 月 17 日 13:55 发布的代码。 This is the code that I built and am testing (as a test, I was trying to change the error response code to "500" and also have a error response message):这是我构建并正在测试的代码(作为测试,我试图将错误响应代码更改为“500”并且还有错误响应消息):
import org.apache.logging.log4j.LogManager;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import mil.nga.geoaxis.pdp.models.ErrorResponse;
import org.springframework.http.HttpStatus;
@ControllerAdvice
public class SpringExceptionHandler extends ExceptionHandlerExceptionResolver
{
@ExceptionHandler(org.springframework.web.HttpMediaTypeNotSupportedException.class)
public ResponseEntity<Object> handleControllerException(HttpMediaTypeNotSupportedException ex, WebRequest req)
{
ErrorResponse errorResponse = null;
//ex.printStackTrace();
errorResponse = new ErrorResponse();
errorResponse.setCode(HttpCodes.HTTP_CODE_INTERNAL_ERROR);
System.out.println("In SpringExceptionHandler.handleControllerException: HttpStatus.INTERNAL_SERVER_ERROR.value()=[" + HttpStatus.INTERNAL_SERVER_ERROR.value() + "]");
System.out.println("In SpringExceptionHandler.handleControllerException: ex.getMessage()=[" + ex.getMessage() + "]");
int myCode = errorResponse.getCode();
System.out.println("In SpringExceptionHandler.handleControllerException: errorResponse.getCode()/String.valueOf(myCode)=[" + String.valueOf(myCode) + "]");
errorResponse.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
errorResponse.setMessage(ex.getMessage());
myCode = errorResponse.getCode();
System.out.println("In SpringExceptionHandler.handleControllerException: errorResponse.getCode()/String.valueOf(myCode)=[" + String.valueOf(myCode) + "]");
return new ResponseEntity<Object>(errorResponse, HttpStatus.valueOf(errorResponse.getCode()));
}
}
However, when I test, and send a request with no "Content-type:" header (using curl), I am still getting what appears to be a response with "100" response code, then a response with a "415" response code, and no response message.但是,当我测试并发送没有“Content-type:”header(使用 curl)的请求时,我仍然收到似乎是带有“100”响应代码的响应,然后是带有“415”响应的响应代码,并且没有响应消息。
FYI, here is part of the logging that is appearing:仅供参考,这是正在出现的日志记录的一部分:
In SpringExceptionHandler.handleControllerException: HttpStatus.INTERNAL_SERVER_ERROR.value()=[500]
In SpringExceptionHandler.handleControllerException: ex.getMessage()=[Content type 'application/x-www-form-urlencoded' not supported]
In SpringExceptionHandler.handleControllerException: errorResponse.getCode()/String.valueOf(myCode)=[0]
In SpringExceptionHandler.handleControllerException: errorResponse.getCode()/String.valueOf(myCode)=[500]
WARN 15428 --- [io-18443-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Failure in @ExceptionHandler ....errorhandling.SpringExceptionHandler#handleControllerException(HttpMediaTypeNotSupportedException, WebRequest)
Can anyone suggest why this does not seem to be able to send the error code and response message?谁能建议为什么这似乎无法发送错误代码和响应消息?
[FYI, I am doing this under Spring Boot 2.5.6.] [仅供参考,我在 Spring Boot 2.5.6 下执行此操作。]
Thanks in advance!提前致谢!
Jim吉姆
EDIT 1: I modified the class per huy's suggestion to this:编辑1:我根据huy的建议修改了class:
import org.apache.logging.log4j.LogManager;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
@ControllerAdvice
public class SpringExceptionHandler
{
@ExceptionHandler(org.springframework.web.HttpMediaTypeNotSupportedException.class)
public ResponseEntity handleControllerException(HttpMediaTypeNotSupportedException ex, WebRequest req)
{
ErrorResponse errorResponse = null;
//ex.printStackTrace();
errorResponse = new ErrorResponse();
//errorResponse.setCode(HttpCodes.HTTP_CODE_INTERNAL_ERROR);
System.out.println("In SpringExceptionHandler.handleControllerException: HttpStatus.INTERNAL_SERVER_ERROR.value()=[" + HttpStatus.INTERNAL_SERVER_ERROR.value() + "]");
System.out.println("In SpringExceptionHandler.handleControllerException: ex.getMessage()=[" + ex.getMessage() + "]");
int myCode = errorResponse.getCode();
System.out.println("In SpringExceptionHandler.handleControllerException: errorResponse.getCode()/String.valueOf(myCode)=[" + String.valueOf(myCode) + "]");
errorResponse.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
errorResponse.setMessage(ex.getMessage());
myCode = errorResponse.getCode();
System.out.println("In SpringExceptionHandler.handleControllerException: errorResponse.getCode()/String.valueOf(myCode)=[" + String.valueOf(myCode) + "]");
return ResponseEntity
.status(HttpStatus.valueOf(415))
.body(ex.getMessage());
}
}
The code above causes a response with error code 415, and the text error message from the HttpMediaTypeNotSupportedException exception.上面的代码会导致错误代码为 415 的响应,以及来自 HttpMediaTypeNotSupportedException 异常的文本错误消息。
You don't need to extend ExceptionHandlerException..., it's used by default to resolve error.您不需要扩展 ExceptionHandlerException...,它默认用于解决错误。
You only need only ControllerAdvice and ExceptionHandler, ExceptionHandlerExceptionResolver will auto scan annotation ExceptionHandler and handle it.你只需要ControllerAdvice和ExceptionHandler,ExceptionHandlerExceptionResolver会自动扫描注解ExceptionHandler并进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.