HttpMessageTreadableException:JSON解析错误
这是我尝试使用的类:HttpMessageTreadableException:JSON解析错误,json,spring,spring-boot,kotlin,concordion,Json,Spring,Spring Boot,Kotlin,Concordion,这是我尝试使用的类: data class GetAppResponse( val externalId: Long?, val lastUpdate: LocalDateTime?, var connectionInfo: ConnectionInfoDto? = null ) : BaseResponse() data class ConnectionInfoDto( val catalogUrl: String?,
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 + "\", " +
替换:
"\"catalogUrl\": " + archiveUrl + ", " +
为此:
"\"catalogUrl\": \"" + archiveUrl + "\", " +
例如,您的archiveUrl值是asdsad,因此您的整个字符串体将是
{"externalId": 1, "lastUpdate": "2020-01-01T10:00:00", "connectionInfo": {"catalogUrl": asdsad, "catalogPass": "user", "catalogLogin": "user"}
您可以看到catalogrl值不是一个字符串,因此给出了错误
因此,如果您想成功地将字符串解析为对象,只需将catalogrl值更改为用双引号表示即可
你的弦体
"{\"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"}
例如,您的archiveUrl值是asdsad,因此您的整个字符串体将是
{"externalId": 1, "lastUpdate": "2020-01-01T10:00:00", "connectionInfo": {"catalogUrl": asdsad, "catalogPass": "user", "catalogLogin": "user"}
您可以看到catalogrl值不是一个字符串,因此给出了错误
因此,如果您想成功地将字符串解析为对象,只需将catalogrl值更改为用双引号表示即可
你的弦体
"{\"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"}
archiveUrl变量已经是字符串了,这有关系吗?我相信是的。因为在连接的字符串中,您将得到:
“catalogrl”:http://something,
-无效的JSONarchiveUrl变量已经是字符串,这有关系吗?我相信是的。因为在连接的字符串中,您将得到:“catalogrl”:http://something,
-这是无效的JSON