Java Spring RestTemplate 使用自定义标头和请求对象发布请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21723183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:17:57  来源:igfitidea点击:

Spring RestTemplate to POST request with Custom Headers and a Request Object

javaspringrestspring-mvcresttemplate

提问by MCF

In Spring RestTemplate is there a way to send Custom Headers together with a POST Request Object. I have already tried out the exchangemethod which is available. It seems that we can send key value pairs together with a custom headers but not a request object itself attached to the HttpEntity.The following code illustrates the attempt and it seems to be 400 BadRequestfor the server.

在 Spring RestTemplate 中,有一种方法可以将自定义标头与 POST 请求对象一起发送。我已经尝试过exchange可用的方法。似乎我们可以将键值对与自定义​​标头一起发送,但不能将请求对象本身附加到HttpEntity.以下代码说明了该尝试,它似乎是400 BadRequest针对服务器的。

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<?> httpEntity = new HttpEntity<Object>(requestDTO, requestHeaders);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.exchange(URL, HttpMethod.POST, httpEntity, SomeObject.class);

Anyone aware about this situation ? Or is it something which is not possible that Im trying to do ?

有人知道这种情况吗?还是我试图做的事情是不可能的?

采纳答案by Andrey

Yes, It is possible, if use MultiValueMap headers instead of HttpHeaders

是的,这是可能的,如果使用 MultiValueMap 标头而不是 HttpHeaders

Example:

例子:

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Authorization", "Basic " + base64Creds);
headers.add("Content-Type", "application/json");

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingHymanson2HttpMessageConverter());

HttpEntity<ObjectToPass> request = new HttpEntity<ObjectToPass>(objectToPassInstance, headers);

restTemplate.postForObject(urlPost, request, Boolean.class);

Boolean.class just because my controller returns boolean at this endpoint (could be anything)

Boolean.class 只是因为我的控制器在这个端点返回布尔值(可以是任何东西)

Good luck with coding!

祝你编码好运!

回答by Vitaly Velikodny

  1. Try to enable full debug of Spring package. I'm sure you get more information about your "400 Bad Request":

    <logger name="org.springframework">
        <level value="DEBUG"/>
    </logger>
    
  2. Try to send same request with any rest tools (e.g. Rest Console Chrome plugin).

  3. See what happens on browser debug console ("Network" tab for Chrome, as example).

  1. 尝试启用 Spring 包的完整调试。我相信您会获得有关“400 错误请求”的更多信息:

    <logger name="org.springframework">
        <level value="DEBUG"/>
    </logger>
    
  2. 尝试使用任何休息工具(例如休息控制台 Chrome 插件)发送相同的请求。

  3. 查看浏览器调试控制台上发生的情况(例如,Chrome 的“网络”选项卡)。

That steps always help me.

这些步骤总是对我有帮助。

回答by Emerson Farrugia

If you're using HttpClient 3.x, turn on logging by following this. If you're using HttpClient 4.x, turn on logging by following this. That should tell you what's getting sent across the wire, and be a decent starting point for debugging.

如果您使用的是 HttpClient 3.x,请按照打开日志记录。如果您使用的是 HttpClient 4.x,请按照打开日志记录。这应该告诉您通过线路发送了什么,并且是调试的一个不错的起点。