我想查看使用Spring Boot构建的端点中所有请求的相对详细信息(例如标头,正文)。如何获得?
@RestController
public class SomeRestController {
...
@PostMapping("path/")
public String getResponse(@RequestBody SomeObject object) {
// There I want to look at Request details... but how?
...
}
...
}
答案 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等)定义所需的任何控制器方法签名。