简体   繁体   English

尝试获取 Spring 启动应用程序以在请求没有“内容类型”时发送错误响应消息 HTTP 请求 header

[英]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.

相关问题 Angular Spring Boot:重定向错误:在飞行前响应中Access-Control-Allow-Headers不允许请求标头字段Content-Type - Angular Spring Boot: Redirection error: Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response 如何发送请求标题是“内容类型”:“应用程序/ json”在排球时获取 - How to send request Header is “Content-Type”:“application/json” when GET on Volley 如何在 Spring Boot 中点击 Controller 组件之前操作 Content-Type 请求标头? - How to manipulate the Content-Type request header before hitting the Controller component in Spring Boot? Spring-更改内容类型HTTP标头 - Spring - changing Content-type HTTP header 我想要接受 https 请求并将此请求发送到 http 上的其他应用程序并将此 http 响应发送回 https req 的 Spring Boot 应用程序 - I want spring boot application which accepts https request and send this request to other app on http and send this http response back to https req 对于 Spring Boot 中的 GET 请求,如何在单个 JSON 响应中发送 Header 中的一些特定信息和 Body 中的一些信息? - How to send some specific info in Header and some info in Body in a single JSON response, for a GET request in Spring boot? 如何在 Spring boot(v2.4.2) 中从 Content-Type 响应头中删除 charset=UTF-8 - How to remove charset=UTF-8 from Content-Type response header in Spring boot(v2.4.2) 如何使用 Spring Boot RestTemplate 将 Microsoft Office Mime 类型作为 Content-Type header 发送 - How do I send Microsoft Office Mime Types as Content-Type header with with Spring Boot RestTemplate 将 map 作为请求参数发送到 Spring 启动应用程序中的 GET 请求 - Send a map as request param to a GET request in Spring Boot app 在 Spring Boot 中获取请求标头 - Get request header in spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM