使用Spring MVC从ExceptionHandler获取请求值 [英] Get request values from ExceptionHandler using Spring MVC

查看:1071
本文介绍了使用Spring MVC从ExceptionHandler获取请求值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring MVC控制器和一个异常处理程序.发生异常时,我希望异常处理程序记录请求中发送的所有GET/POST数据.如何实现?

I have a Spring MVC Controller and an Exception Handler. When an exception occurs, I want the Exception Handler to log all the GET/POST data that was sent in the request. How can this be achieved?

控制器:

@Controller
@RequestMapping("/foo")
public class FooController {
    private final FooService fooService;

    @PostMapping("/bar")
    @ResponseBody
    public BarObject doSomething(@RequestBody final FooContext context) 
    {
        return fooService.doSomething(context);
    }
}

异常处理程序:

@ControllerAdvice
public class ExceptionController {

    private final Logger log = LoggerFactory.getLogger(ExceptionController.class);

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ErrorMessage handleException(final HttpServletRequest request, final Exception exception) {
        //Retrieve request data 
        //request.getQueryString() 
        // How to get POST data if we cannot access @RequestBody?)
        log.error(request.getRequestURI(), exception);
        return new ErrorMessage(request.getRequestURI(), exception.getLocalizedMessage());
}

推荐答案

请求主体位于HttpServletRequest中.

Well the request body is in the HttpServletRequest.

您可以执行以下操作来访问RAW请求正文:

You could access the RAW request body by doing something like:

String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));

来自异常处理程序方法. (使用Java 8).

from the exception handler method. (using java 8).

然后,您可以将主体字符串解析为POJO.

Then you can parse the body string as a POJO.

编辑

已注意到avobe答案无效.发生这种情况是因为在解析正文(使用@RequestBody)时,http连接流已关闭,因此无法再次访问请求正文.如何通过控制器方法将属性直接注入httpRequest,然后在Exception处理程序中访问值:

It's been noted that the avobe answer does not work. This happens because when the body is parsed (with @RequestBody) the http connection stream is closed, hence the request body cannot be accessed again. Howver you could inject a property directly to the httpRequest from your controller method and then access the value in your Exception handler:

@RestController
@RequestMapping(ApiVersion.V1.prefix)
public class BatchController {
    @PostMapping("/batch")
    public @ResponseBody BatchResponse runBatch(@RequestBody BatchRequest batchRequest, HttpServletRequest request) throws IOException {
        System.out.println(batchRequest.getName());
        request.setAttribute("batchRequest" , batchRequest);
        throw new IllegalArgumentException("Some error");
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public @ResponseBody BatchResponse handle(HttpServletRequest request) {
        BatchRequest batchRequest = (BatchRequest) request.getAttribute("batchRequest");
        System.out.println("handling exception");
        return new BatchResponse(batchRequest.getName());
    }
}

希望这会有所帮助

这篇关于使用Spring MVC从ExceptionHandler获取请求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆