Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring引导中获取请求头_Java_Spring_Request - Fatal编程技术网

Java 在spring引导中获取请求头

Java 在spring引导中获取请求头,java,spring,request,Java,Spring,Request,如何从一个名为我的Springboot应用程序的应用程序中获取当前请求的头和体?我需要提取这些信息。不幸的是,这不起作用。我尝试使用以下代码示例()获取当前请求: 然后我试图找到尸体 ContentCachingRequestWrapper requestWrapper = (ContentCachingRequestWrapper) currentRequest; String requestBody = new String(requestWrapper.getContentAsBy

如何从一个名为我的Springboot应用程序的应用程序中获取当前请求的头和体?我需要提取这些信息。不幸的是,这不起作用。我尝试使用以下代码示例()获取当前请求:

然后我试图找到尸体

ContentCachingRequestWrapper requestWrapper = (ContentCachingRequestWrapper) currentRequest;
    String requestBody = new String(requestWrapper.getContentAsByteArray());
有人能告诉我我做错了什么吗? 提前感谢

@RestController
@RestController
public class SampleController {

    @PostMapping("/RestEndpoint")
    public ResponseEntity<?> sampleEndpoint(@RequestHeader Map<String, String> headers,@RequestBody Map<String,String> body) {
        //Do something with header / body
        return null;
    }
}
公共类采样控制器{ @后期映射(“/RestEndpoint”) 公共响应性sampleEndpoint(@RequestHeader映射头,@RequestBody映射体){ //对头部/身体做些什么 返回null; } }
如果应用程序通过rest端点进行通信,我相信这将是最简单的解决方案。在spring中,您可以将RequestHeader和RequestBody注释添加到方法参数中,以设置它们以供使用

当然,您可以直接将RequestBody映射到某个POJO,而不是使用映射,只是作为一个示例


让我知道这是否是你要找的

@TryHard,您使用的是spring boot,下面的方法更适合您

@RestController
public class SampleController {
    @RequestMapping("/get-header-data")
    public ResponseEntity<?> sampleEndpoint(HttpServletRequest request) {
        // request object comes with various in-built methods use as per your requirement.
        request.getHeader("<key>");
    }
}
@RestController
公共类采样控制器{
@请求映射(“/get header data”)
公共响应性sampleEndpoint(HttpServletRequest请求){
//请求对象随附各种内置方法,可根据您的需求使用。
request.getHeader(“”);
}
}
@RestController
public class SampleController {
    @RequestMapping("/get-header-data")
    public ResponseEntity<?> sampleEndpoint(HttpServletRequest request) {
        // request object comes with various in-built methods use as per your requirement.
        request.getHeader("<key>");
    }
}