简体   繁体   English

使用 JSON 请求正文通过 REST 模板发送 GET 请求,绑定绑定元素失败必须是结构错误?

[英]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.

相关问题 thymeleaf 模板在 chrome 中发送请求时没有得到所有响应,我正在从数据库接收数据绑定是否有任何错误? - thymeleaf template not getting all response while sending request in chrome, I am receiving data from database is there any error in binding? 通过httpentity将请求正文作为json发送到GET请求 - Sending Request body as json to GET request through httpentity 获取java.lang.NoSuchMethodException:javax.validation.Valid.value()在RestController中绑定JSON请求时出错 - Getting java.lang.NoSuchMethodException: javax.validation.Valid.value() Error while binding JSON request in RestController Webclient 使用 get 请求发送请求正文 - Webclient sending request body with get request 在 java 的 get 请求中发送 Json - Sending a Json in get request in java 使用Rest模板时收到错误的请求 - Getting bad request when using rest template 在spring boot中将无效的json请求主体传递给rest api时没有出现异常 - Not getting exception while passing invalid json request body to rest api in spring boot 获取请求主体为字符串/ JSON以使用JSON模式进行验证-Spring Boot REST API - Get request body as string/json to validate with a json schema- Spring boot REST API 带有 map 的 RequestParam 不必要地绑定 GET 请求中的其他请求参数 - RequestParam with map is unnecessarily binding other request params in GET request Spring Rest 模板从数组中的请求正文中删除引号 - Spring Rest Template remove quotes from request body in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM