HttpMessageNotReadableException: JSON 解析错误
HttpMessageNotReadableException: JSON parse error
这是我正在尝试使用的 class:
data class GetAppResponse(
val externalId: Long?,
val lastUpdate: LocalDateTime?,
var connectionInfo: ConnectionInfoDto? = null
) : BaseResponse()
data class ConnectionInfoDto(
val catalogUrl: String?,
val catalogPass: String?,
val catalogLogin: String?
)
当我尝试将其序列化为 json:
fun partner() {
partner.stubFor(get(anyUrl())
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\"externalId\": 1, " +
"\"lastUpdate\": \"2020-01-01T10:00:00\", " +
"\"connectionInfo\": {" +
"\"catalogUrl\": " + archiveUrl + ", " +
"\"catalogPass\": \"user\", " +
"\"catalogLogin\": \"user\"}")
))
}
我得到
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'http': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unrecognized token 'http': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (PushbackInputStream); line: 1, column: 94] (through reference chain: GetAppResponse["connectionInfo"])
什么原因以及如何解决问题?
替换:
"\"catalogUrl\": " + archiveUrl + ", " +
有了这个:
"\"catalogUrl\": \"" + archiveUrl + "\", " +
例如,您的 archiveUrl 值为 asdsad,因此您的完整字符串正文将为
{"externalId": 1, "lastUpdate": "2020-01-01T10:00:00", "connectionInfo": {"catalogUrl": asdsad, "catalogPass": "user", "catalogLogin": "user"}
您可以看到 catalogUrl 值不是字符串,因此出现错误。
因此,如果您想成功地将字符串解析为您的对象,只需更改 catalogUrl 值以用双引号表示。
你的字符串主体
"{\"externalId\": 1, " +
"\"lastUpdate\": \"2020-01-01T10:00:00\", " +
"\"connectionInfo\": {" +
"\"catalogUrl\": \"" + archiveUrl + "\", " +
"\"catalogPass\": \"user\", " +
"\"catalogLogin\": \"user\"}"
和json响应:-
{"externalId": 1, "lastUpdate": "2020-01-01T10:00:00", "connectionInfo": {"catalogUrl": "asdsad", "catalogPass": "user", "catalogLogin": "user"}
这是我正在尝试使用的 class:
data class GetAppResponse(
val externalId: Long?,
val lastUpdate: LocalDateTime?,
var connectionInfo: ConnectionInfoDto? = null
) : BaseResponse()
data class ConnectionInfoDto(
val catalogUrl: String?,
val catalogPass: String?,
val catalogLogin: String?
)
当我尝试将其序列化为 json:
fun partner() {
partner.stubFor(get(anyUrl())
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\"externalId\": 1, " +
"\"lastUpdate\": \"2020-01-01T10:00:00\", " +
"\"connectionInfo\": {" +
"\"catalogUrl\": " + archiveUrl + ", " +
"\"catalogPass\": \"user\", " +
"\"catalogLogin\": \"user\"}")
))
}
我得到
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'http': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unrecognized token 'http': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (PushbackInputStream); line: 1, column: 94] (through reference chain: GetAppResponse["connectionInfo"])
什么原因以及如何解决问题?
替换:
"\"catalogUrl\": " + archiveUrl + ", " +
有了这个:
"\"catalogUrl\": \"" + archiveUrl + "\", " +
例如,您的 archiveUrl 值为 asdsad,因此您的完整字符串正文将为
{"externalId": 1, "lastUpdate": "2020-01-01T10:00:00", "connectionInfo": {"catalogUrl": asdsad, "catalogPass": "user", "catalogLogin": "user"}
您可以看到 catalogUrl 值不是字符串,因此出现错误。
因此,如果您想成功地将字符串解析为您的对象,只需更改 catalogUrl 值以用双引号表示。
你的字符串主体
"{\"externalId\": 1, " +
"\"lastUpdate\": \"2020-01-01T10:00:00\", " +
"\"connectionInfo\": {" +
"\"catalogUrl\": \"" + archiveUrl + "\", " +
"\"catalogPass\": \"user\", " +
"\"catalogLogin\": \"user\"}"
和json响应:-
{"externalId": 1, "lastUpdate": "2020-01-01T10:00:00", "connectionInfo": {"catalogUrl": "asdsad", "catalogPass": "user", "catalogLogin": "user"}