JSON 值无法转换为 System.String
The JSON value could not be converted to System.String
我遇到了 POST 请求的问题。这很简单,实际上,我只需要在应用程序正文中发送 json 中的一段日期。但是,我不断收到以下消息:
The JSON value could not be converted to System.String
完整的报错信息如下:
400 Bad Request: [{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"xxx","errors":{"$.reconciliationDate":["The JSON value could not be converted to System.String. Path: $.fooDate | LineNumber: 0 | BytePositionInLine: 23."]}}]
我尝试使用 LinkedMultiValueMap 而不是 JsonObject,但它也没有用。根据错误,我假设我发送的 json 有问题...数据是:{"fooDate":"2021-05-11"}
。完整代码为:
public void foo(String fooDate){
try{
Token token = this.GetAccessToken();
if(token == null){
throw new Exception("Access token was null");
}
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.set("Authorization", "Bearer " + token.getAccess_token());
JsonObject body = Json.createObjectBuilder()
.add("fooDate", fooDate).build();
HttpEntity<Object> entity = new HttpEntity<>(body, headers);
ResponseEntity<String> result = restTemplate.exchange(properties.getUri(),
HttpMethod.POST,
entity,
String.class);
if(result.getStatusCode() != HttpStatus.OK){
throw new Exception(String.format("Endpoint returned an unexpected status code : %d",
result.getStatusCode()));
}
}catch (Exception e){
e.printStackTrace();
}
}
Gson 应该适用于这种类型的转换。 Gson 是一个 Java 库,可用于将 Java 对象转换为其 JSON 表示形式。它还可用于将 JSON 字符串转换为等效的 Java 对象。
编辑
目前我还没有代码片段,很快就会分享,但现在你可以试试下面这样的东西,看看它是否有效。 Gson gson = new Gson(); String json = gson.toJson(obj);
在这种情况下,它将像:
JsonObject body = Json.createObjectBuilder()
.add("fooDate", gson.toJson(fooDate)).build();
抱歉,如果我误解了您的要求。
其实答案很简单。在尝试解决此问题时,我尝试向 headers 添加以下内容:
headers.set("Content-Type", "application/json");
这对行为没有任何改变。但是,通过添加以下内容修复了错误:
headers.setContentType(MediaType.APPLICATION_JSON);
希望这对遇到类似问题的人有所帮助。
我遇到了 POST 请求的问题。这很简单,实际上,我只需要在应用程序正文中发送 json 中的一段日期。但是,我不断收到以下消息:
The JSON value could not be converted to System.String
完整的报错信息如下:
400 Bad Request: [{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"xxx","errors":{"$.reconciliationDate":["The JSON value could not be converted to System.String. Path: $.fooDate | LineNumber: 0 | BytePositionInLine: 23."]}}]
我尝试使用 LinkedMultiValueMap 而不是 JsonObject,但它也没有用。根据错误,我假设我发送的 json 有问题...数据是:{"fooDate":"2021-05-11"}
。完整代码为:
public void foo(String fooDate){
try{
Token token = this.GetAccessToken();
if(token == null){
throw new Exception("Access token was null");
}
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.set("Authorization", "Bearer " + token.getAccess_token());
JsonObject body = Json.createObjectBuilder()
.add("fooDate", fooDate).build();
HttpEntity<Object> entity = new HttpEntity<>(body, headers);
ResponseEntity<String> result = restTemplate.exchange(properties.getUri(),
HttpMethod.POST,
entity,
String.class);
if(result.getStatusCode() != HttpStatus.OK){
throw new Exception(String.format("Endpoint returned an unexpected status code : %d",
result.getStatusCode()));
}
}catch (Exception e){
e.printStackTrace();
}
}
Gson 应该适用于这种类型的转换。 Gson 是一个 Java 库,可用于将 Java 对象转换为其 JSON 表示形式。它还可用于将 JSON 字符串转换为等效的 Java 对象。
编辑
目前我还没有代码片段,很快就会分享,但现在你可以试试下面这样的东西,看看它是否有效。 Gson gson = new Gson(); String json = gson.toJson(obj);
在这种情况下,它将像:
JsonObject body = Json.createObjectBuilder()
.add("fooDate", gson.toJson(fooDate)).build();
抱歉,如果我误解了您的要求。
其实答案很简单。在尝试解决此问题时,我尝试向 headers 添加以下内容:
headers.set("Content-Type", "application/json");
这对行为没有任何改变。但是,通过添加以下内容修复了错误:
headers.setContentType(MediaType.APPLICATION_JSON);
希望这对遇到类似问题的人有所帮助。