【发布时间】:2019-01-31 21:03:29
【问题描述】:
我有一个嵌套的 json pojo,其中 json 的嵌套部分用 @JsonRawValue 标记。我正在尝试将其与其余模板进行映射,但出现错误
JSON解析错误:Cannot deserialize instance of java.lang.String out of START_OBJECT token;
嵌套异常是com.fasterxml.jackson.databind.exc.MismatchedInputException。
这是我的响应对象的样子:
import com.fasterxml.jackson.annotation.JsonRawValue;
public class ResponseDTO {
private String Id;
private String text;
@JsonRawValue
private String explanation;
//getters and setters;
}
其中explanation 是映射到字符串的json。这适用于邮递员,招摇,我在响应中看到解释为 json。
但是当我使用 Rest Template 测试它时:
ResponseEntity<ResponseDTO> resonseEntity = restTemplate.exchange(URI, HttpMethod.POST, requestEntity, ResponseDTO.class);
我看到了这个异常:
org.springframework.web.client.RestClientException: Error while extracting
response for type [class com.**.ResponseDTO] and content type
[application/json;charset=utf-8]; nested exception is
org.springframework.http.converter.HttpMessageNotReadableException: JSON
parse error: Cannot deserialize instance of java.lang.String out of
START_OBJECT token; nested exception is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
deserialize instance of java.lang.String out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 604] (through
reference chain: com.****.ResponseDTO["explanation"])
【问题讨论】:
标签: java json spring rest jackson