提问人:Supriyo Biswas 提问时间:7/15/2021 最后编辑:Brian Tompsett - 汤莱恩Supriyo Biswas 更新时间:7/19/2021 访问量:203
如何使用springboot获取GlobalExceptionHandler类中的RequestBody
How can I get the RequestBody in GlobalExceptionHandler class using springboot
问:
===============控制器类==================
@PostMapping(path = "/products")
public ResponseEntity<ProductResponseDTO> saveProduct(@RequestBody Product product, @RequestHeader(name = "uuid", required = false) String uuid, HttpServletRequest request) throws Exception {
request.setattribute("product",product);
productRepo.save(product);
......
.....
}
=======================异常处理程序类==========================
@ControllerAdvice
public class GlobalErrorHandler{
@ExceptionHandler({RuntimeException.class})
public ResponseEntity<ErrorDetails> generateRuntimeException(RuntimeException re, WebRequest request, HttpServletRequest servletRequest) throws IOException {
ErrorDetails errorDetails = new ErrorDetails();
Product product=(Product)servletRequest.getAttribute("product");
errorDetails.setTimestamp(LocalDateTime.now());
errorDetails.setMessage("500");
errorDetails.setDetails(re.getMessage());
log.error("Exception occured3: " + re);
}
return new ResponseEntity<ErrorDetails>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
}
]
我可以看到在执行 时,它显示 null 值。一旦发生异常,它就不会进入控制器主体内部,而是直接将控件传递给异常类。因此,未设置请求的值。request.getAttribute("product")
resuest.setAttribute("product")
我用谷歌搜索并尝试了几种方法,但仍然得到空值。 任何人都请帮助我解决它。
答: 暂无答案
评论