这是client端的请求
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8902/tttt234";
try {
ResponseEntity res = restTemplate.exchange(url, HttpMethod.POST, null, JSONObject.class);
JSONObject json = res.getBody();
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
}
这是Server端的方法
@PostMapping(value = "/tttt234")
private ResponseEntity xxxxx() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "123");
jsonObject.put("data", "{}");
// return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(jsonObject);
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(jsonObject.toString());
}
client端就会报错
org.springframework.web.client.RestClientException: Error while extracting response for type [class com.alibaba.fastjson.JSONObject] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `int` from String "{"data":"{}","name":"123"}": not a valid `int` value; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `int` from String "{"data":"{}","name":"123"}": not a valid `int` value
at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]
如果server端在最后的返回的地方不使用toString方法,client端就不会报错,这是什么原理?
