【问题标题】:HttpMessageNotReadableException: JSON parse errorHttpMessageNotReadableException:JSON 解析错误
【发布时间】:2020-12-14 07:00:32
【问题描述】:

这是我正在尝试使用的课程:

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"])

是什么原因以及如何解决问题?

【问题讨论】:

    标签: json spring spring-boot kotlin concordion


    【解决方案1】:

    替换:

    "\"catalogUrl\": " + archiveUrl + ", " +
    

    用这个:

    "\"catalogUrl\": \"" + archiveUrl + "\", " +
    

    【讨论】:

    • archiveUrl 变量已经是 String,有关系吗?
    • 我相信是的。因为在你的连接字符串中你会得到:"catalogUrl": http://something, - 这是无效的 JSON
    【解决方案2】:

    例如,您的 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"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      相关资源
      最近更新 更多