![](/img/trans.png)
[英]thymeleaf template not getting all response while sending request in chrome, I am receiving data from database is there any error in binding?
[英]sending GET request via REST template with JSON request body getting failed with binding binding element must be a struct error?
我正在嘗試使用帶有 JSON 請求正文的 REST 模板發送 GET 請求,但請求失敗並出現錯誤,
處理失敗org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: [code=400, message=binding element must be a struct]
我嘗試使用 insomnia 到達端點並且請求成功通過,我在那里放了 2 個標頭
1. Content-Type - application/json
2. Authorization - Bearer ******
和 JSON 正文。
我在 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);
httpentity 和 header 的 Sout 看起來像這樣
http實體
<{"requestType":"status"},[Authorization:"Bearer *******************", Content-Type:"application/json"]>
標題
[Authorization:"Bearer *************************", Content-Type:"application/json"]
此外,當我嘗試使用 rest 模板將沒有正文的請求發送到另一個端點時,它會成功執行,所以我認為我發送正文的方式與錯誤有關。
rest 模板不支持使用 body 獲取請求。 有關更多詳細信息,您可以參考這篇文章。
如果您使用的是 Java 11,我建議您使用java.net.HttpClient來滿足您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.