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

时间:2018-08-08 19:34:58

标签: java spring spring-boot http-post

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

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

2 个答案:

答案 0 :(得分:2)

如果您想获得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);

答案 1 :(得分:1)

  

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

参考: 15. Web MVC framework

相关问题