我试图在globalexceptionhandler中处理httpmediatypenotsupportedexception,我想通过调用restcontroller的哪个方法知道发生了这个异常,或者我需要将pathvariables从requesturi中分离出来,在我将参数“consumes”添加到@patchmapping annotation之前,一切都正常REST控制器。如果没有这个参数,当我从 Postman 发送请求时,我也会得到httpmediatypenotsupportedexception和415代码,例如xml类型,但是我需要这个参数。所以,没有这个论点,我可以做以下两件事:
为了找出发生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);
}
}
要分离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),
new HttpHeaders(),
HttpStatus.UNSUPPORTED_MEDIA_TYPE);
}
}
但是,如果我在restcontroller中将参数“consumes”添加到@patchmapping中,那么如果尝试在@exceptionhandler中添加handlermethod参数,那么我就没有为参数[1]和spring句柄获得合适的解析程序,然后httpmediatypenotsupportedexception由他自己完成。
另外,如果我尝试在没有参数handlermethod的情况下获取pathvariables,那么会得到null:
@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),
new HttpHeaders(),
HttpStatus.UNSUPPORTED_MEDIA_TYPE);
}
}
问题:我应该如何处理这个异常,在restcontroller的@patchmapping注解中使用参数,并且我可以有发生这个异常的方法名,或者可以从requesturi中分离pathvariables?
我的控制器:
@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!
暂无答案!
目前还没有任何答案,快来回答吧!