繁体   English   中英

如何在Spring REST服务中获取所有传入的请求详细信息?

[英]How to get all incoming request details in Spring REST service?

我想查看使用Spring Boot构建的端点中所有请求的相对详细信息(例如标头,正文)。 如何获得?

@RestController
public class SomeRestController {
    ...
    @PostMapping("path/")
    public String getResponse(@RequestBody SomeObject object) {
        // There I want to look at Request details... but how?
        ...
    }
    ...
}

如果要获取RequestHeader ,可以在方法中使用@RequestHeader批注

public String getResponse(@RequestBody SomeObject object,
 @RequestHeader("Content-type") String contentType) {

您可以执行的另一种方法是,在春季之前将对HttpServletRequest注入进行处理。

 public String getResponse(HttpServletRequest request, 
  @RequestBody SomeObject object) {
String userAgent = request.getHeader("content-Type");
}

要么

  Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String key = (String) headerNames.nextElement();
        String value = request.getHeader(key);

定义给定方案所需的任何控制器方法签名,可能使用参数注释之一(例如@ RequestParam,@ RequestHeader,@ PathVariable等)。

参考: 15. Web MVC框架

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM