首页 > 解决方案 > Issue with class cast during post request

问题描述

    try {

        def contactId= "9999"
        def phoneNumber = 213213214
        def postObj = [PhoneNumber: phoneNumber,
                       ContactID  : contactId]

        def queryString = ["processAgainstExisting": true]

        def params = ["restVerb"     : "POST",
                      "postObj"      : postObj,

                      "path"         : "/contact/$contactId/PhoneNumber",
                      "query"        : queryString]

        def response = new PostRequest("Some url")
                .setParams(params)
                .execute()

        // check HttpResponse status code
        if (response.status != 200) {
            throw new RuntimeException(response.statusLine)
        }

} catch (Throwable e) {
    throw e
}

tiedd doing the post and faced this issue. Caught: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.String

Any other way to handle this issue?

标签: javarestgroovy

解决方案


好像这条线

throw new RuntimeException(response.statusLine)

导致异常,不是吗?

尝试

throw new RuntimeException(response.statusLine.toString())

statusLine不是 a String,而是RuntimeException需要构造函数的消息String

https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/StatusLine.html


推荐阅读