如何从 RestTemplate 读取响应标头?

发布于 2024-11-07 23:16:03 字数 228 浏览 0 评论 0原文

我正在使用 RestTemplate.postForObject 将信息发布到 Web 服务。除了结果字符串之外,我还需要响应标头中的信息。有什么办法可以得到这个吗?

RestTemplate template = new RestTemplate();
String result = template.postForObject(url, request, String.class);

I am posting information to a web service using RestTemplate.postForObject. Besides the result string I need the information in the response header. Is there any way to get this?

RestTemplate template = new RestTemplate();
String result = template.postForObject(url, request, String.class);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愚人国度 2024-11-14 23:16:03

好吧,我终于想通了。交换方法正是我所需要的。它返回一个包含完整标头的 HttpEntity。

RestTemplate template = new RestTemplate();
HttpEntity<String> response = template.exchange(url, HttpMethod.POST, request, String.class);

String resultString = response.getBody();
HttpHeaders headers = response.getHeaders();

Ok, I finally figured it out. The exchange method is exactly what i need. It returns an HttpEntity which contains the full headers.

RestTemplate template = new RestTemplate();
HttpEntity<String> response = template.exchange(url, HttpMethod.POST, request, String.class);

String resultString = response.getBody();
HttpHeaders headers = response.getHeaders();
小伙你站住 2024-11-14 23:16:03

最好的办法是使用 execute 方法并传入 ResponseExtractor 将有权访问标头。

private static class StringFromHeadersExtractor implements ResponseExtractor<String> {

    public String extractData(ClientHttpResponse response) throws   
    {
        return doSomthingWithHeader(response.getHeaders());
    }
}

另一个选项(不太干净)是扩展 RestTemplate 并覆盖对 doExecute 的调用,并在其中添加任何特殊的标头处理逻辑。

Best thing to do whould be to use the execute method and pass in a ResponseExtractor which will have access to the headers.

private static class StringFromHeadersExtractor implements ResponseExtractor<String> {

    public String extractData(ClientHttpResponse response) throws   
    {
        return doSomthingWithHeader(response.getHeaders());
    }
}

Another option (less clean) is to extend RestTemplate and override the call to doExecute and add any special header handling logic there.

相权↑美人 2024-11-14 23:16:03
  HttpEntity<?> entity = new HttpEntity<>( postObject, headers ); // for request
    HttpEntity<String> response = template.exchange(url, HttpMethod.POST, entity, String.class);
    String result= response.getBody();
    HttpHeaders headers = response.getHeaders();
  HttpEntity<?> entity = new HttpEntity<>( postObject, headers ); // for request
    HttpEntity<String> response = template.exchange(url, HttpMethod.POST, entity, String.class);
    String result= response.getBody();
    HttpHeaders headers = response.getHeaders();
小糖芽 2024-11-14 23:16:03

我不知道这是否是推荐的方法,但如果您将模板配置为使用自定义 HttpMessageConverter

I don't know if this is the recommended method, but it looks like you could extract information from the response headers if you configure the template to use a custom HttpMessageConverter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文