[英]sending GET request via REST template with JSON request body getting failed with binding binding element must be a struct error?
I am trying to send a GET request using REST Template with a JSON request body, but the request is failing with error,我正在尝试使用带有 JSON 请求正文的 REST 模板发送 GET 请求,但请求失败并出现错误,
processing failedorg.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: [code=400, message=binding element must be a struct]处理失败org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: [code=400, message=binding element must be a struct]
I have tried hitting the endpoint using the insomnia and the request is going through successfully, There I have put 2 headers我尝试使用 insomnia 到达端点并且请求成功通过,我在那里放了 2 个标头
1. Content-Type - application/json
2. Authorization - Bearer ******
And the JSON body.和 JSON 正文。
My code in spring boot looks like this.我在 spring boot 中的代码看起来像这样。
ResponseEntity<String> responseObject = null;
String URL = "https://myurl/endpoint";
String requestBody = "{\"requestType\":\"status\"}";
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization","Bearer ***");
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity httpEntity = new HttpEntity<>(body,headers);
System.out.println(httpEntity+" httpEntity");
System.out.println(headers+" headers");
responseObject = restTemplate.exchange(URL, HttpMethod.GET, httpEntity, String.class);
The sout for httpentity and header looks like this httpentity 和 header 的 Sout 看起来像这样
httpEntity http实体
<{"requestType":"status"},[Authorization:"Bearer *******************", Content-Type:"application/json"]>
headers标题
[Authorization:"Bearer *************************", Content-Type:"application/json"]
Also when I am trying to send a request without the body to another endpoint using rest template, that is getting executed successfully, so I think something with the way I am sending the body has to do with the error.此外,当我尝试使用 rest 模板将没有正文的请求发送到另一个端点时,它会成功执行,所以我认为我发送正文的方式与错误有关。
rest template doesn't support get request with body . rest 模板不支持使用 body 获取请求。 for more details you can refer this article .有关更多详细信息,您可以参考这篇文章。
If you are on Java 11 I would suggest you to use java.net.HttpClient which will fulfill your need.如果您使用的是 Java 11,我建议您使用java.net.HttpClient来满足您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.