我正在嘗試將 JSON 物件發送到 Spring 控制器,控制器將請求正文作為 aMap<String, Object>接收,JSON 本身是正確的,并且通過郵遞員發送相同的 JSON 也可以正常作業,只是當我使用 HttpClient 發送它時。
這是我要發送的 JSON:
{"fruit": "Apple", "color": "Red"}
我的 HTTP 請求:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
String jsonString = mapper.writeValueAsString(body);
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.header("content-type", "application/json")
.POST(BodyPublishers.ofString(jsonString))
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
我的控制器:
@PostMapping(value = "/config/addDocument", consumes = {"application/json"})
public void addDocument(@RequestBody Map<String, Object> document)
我收到此錯誤:
HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"fruit": "Apple", "color": "Red"}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"fruit": "Apple", "color": "Red"}')<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]]
我嘗試使用郵遞員發送相同的請求并且它作業正常,創建一個變數字串并發送它也可以正常作業,我在請求方面做錯了什么?
編輯
客戶端來自java.net.http.HttpClient包。
我使用創建了客戶端HttpClient.newHttpClient()
我正在使用 Spring Boot Starter Web 2.7.5。
編輯 2
根據答案,我嘗試將輸入 JSON 字串切換為地圖,并將地圖轉換回 JSON 字串并發送,這樣就可以了。
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(body, Map.class);
String jsonString = mapper.writeValueAsString(map);
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.header("content-type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonString))
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
uj5u.com熱心網友回復:
我使用 Spring Boot 2.7.5、Java 17 嘗試了以下代碼。發送請求的驅動程式代碼。
Map<String, String> body = new LinkedHashMap<>(); //new HashMap() is also fine here
body.put("fruit", "Apple");
body.put("color", "Red");
//Map.of("fruit", "Apple", "color", "Red");
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
String jsonString = mapper.writeValueAsString(body);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://localhost:8080/test"))
.header("content-type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonString))
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("Status Code : " response.statusCode());
System.out.println("Headers : " response.headers());
其余控制器與您在問題中提到的相同。如果我仍然使用它來實體化地圖,Map.of()它作業正常。最后一個 sysout 陳述句輸出到控制臺,如下所示。

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/532295.html
