如何在@RequestMapping 注释中使用“consumes”参数处理 HttpMediaTypeNotSupportedException?

问题描述

我试图在我的 GlobalExceptionHandler 中处理 HttpMediaTypeNotSupportedException,我想通过调用 RestController 的哪个方法来知道这个异常发生了,或者我需要将 pathVariables 与 requestUri 分开,一切正常,直到我添加参数'consumes' 到 RestController 中的 @PatchMapping 注释。如果没有这个参数,当我从 Postman 发送请求时,我也会得到 HttpMediaTypeNotSupportedException 和 415 代码,例如类型为 XML,但我需要这个参数。 所以,如果没有这个论点,我可以做以下两件事:

  1. 为了找出哪个方法发生HttpMediaTypeNotSupportedException,我在GlobalExceptionHandler的@ExceptionHandler方法添加了参数HandlerMethod,一切都很好:

    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        private static final Logger LOG = LogManager.getLogger();
    
        @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
        public ResponseEntity<ApiError> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,HandlerMethod handlerMethod) {
    
            System.out.println("methodName: " + handlerMethod.getmethod().getName()); //methodName: updateOrder
    
        return new ResponseEntity<>(constructError(ex.getMessage(),HttpStatus.UNSUPPORTED_MEDIA_TYPE),new HttpHeaders(),HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    
        }
    }
    
    
    
  2. 为了分离 pathVariables 或从 requestUri 中获取它们,我这样做:

    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        private static final Logger LOG = LogManager.getLogger();
    
        @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
        public ResponseEntity<ApiError> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,HttpServletRequest request
    ) {
    Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
    System.out.println("map : " + pathVariables); // map : {id=3} 
    
    String path = (String)request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    System.out.println("path : " + path); // path : /reservation/{id}
    
    
    return new ResponseEntity<>(constructError(ex.getMessage(),HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    
        }
    }
    
    

但是如果我将 RestController 中的参数“consumes”添加到 @PatchMapping 中,那么如果尝试在 @ExceptionHandler 中添加 HandlerMethod 参数,那么我会得到没有合适的参数解析器 [1] 并且 Spring 自己处理 HttpMediaTypeNotSupportedException .

另外,如果我试图获取没有参数 HandlerMethod 的路径变量,那么我会得到空值:

@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResponseEntity<ApiError> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,HttpServletRequest request
) {
Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
System.out.println("map : " + pathVariables); // null
String path = (String)request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
System.out.println("path : " + path); // null


return new ResponseEntity<>(constructError(ex.getMessage(),HttpStatus.UNSUPPORTED_MEDIA_TYPE);

    }
}

问题:我应该怎么做才能在 RestController 的 @PatchMapping 注释中使用参数消耗来处理此异常,并且我可以拥有发生此异常的方法名称,或者可以将路径变量与 requestUri 分开?

我的 RestController:

    @RestController
    @RequestMapping(value = "/reservation")
    public class ReservationController {
    
        @PatchMapping(
                value = "/{id}",// consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,// reason for my problems
                produces = MediaType.APPLICATION_JSON_UTF8_VALUE
        )
        public ResponseEntity<Void> updateOrder (@Valid @RequestBody Order order,@PathVariable Integer id) {
    
         // some logic
    
            return new ResponseEntity<>(new HttpHeaders(),HttpStatus.CREATED);
    }
    }

Thank you in advance!


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...