Rest接口,使用Jackson作为json转换器,后端接收请求的时候报错了,异常信息:
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: 138] (through reference chain: com.xxx.TradeDto["body"]->com.xxx.dto.FxsFsDto["txDt"])] [http-nio-9008-exec-10]
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: 138] (through reference chain: com.xxx.TradeDto["body"]->com.xxx.dto.FxsFsDto["txDt"])
请求报文:
{"body":{"txType":"01","nearDeliveryType":"01","txDt":{}}}
异常信息是很详细、明确的,
Cannot deserialize instance of `java.lang.String` out of START_OBJECT
是说无法将字段,从START_OBJECT(Object类型,即对象)转换为String(字符串类型)
(through reference chain: com.xxx.TradeDto["body"]->com.xxx.dto.FxsFsDto["txDt"])
是说,转换出错的字段,是body字段里的txDt字段。
还包含了类型信息:
com.xxx.TradeDto是整个请求体的类型
body字段是com.xxx.dto.FxsFsDto类型
综上,出错原因是body的txDt字段,请求数据是Object类型,接收方要求的是String类型,不匹配。
问题解决:
将txDt改成字符串
更改后的请求报文:
{"body":{"txType":"01","nearDeliveryType":"01","txDt":""}}
附:
关于Jackson转换,报错信息是很明确的,
比如:Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
意思是无法将对象转换为列表