注册

rest 接口发文本消息 TextMessageBody 对象 填充msg参数,接口返回msg must be JSONObject

        String targets[] = { "test"};
        JSONObject j = new JSONObject();
        j.put("type", "txt");
        j.put("msg", "hello");
        BodyWrapper messagetxt = new TextMessageBody("users", targets, "rest", null, j.toJSONString());
        System.out.println(messagetxt.getBody());
        ResponseWrapper r = (ResponseWrapper) message.sendMessage(messagetxt);

        System.out.println("result:" + r.getResponseBody() + "__" + r.getMessages());

如上边代码,创建messbodyde 的时候msg参数已经是jsonobject,服务器返回信息还是400,
result:{"error":"illegal_argument","timestamp":1473389121249,"duration":0,"exception":"java.lang.IllegalArgumentException","error_description":"msg must be JSONObject"}__[]

这个TextMessageBody 这样写有问题吗?
已邀请:
解决了 ,问题不在传TextMessageBody 的msg参数的json格式,而是TextMessageBody 本身的getbody方法有问题。
    public ContainerNode<?> getBody() {
        if (!isInit()) {
            // this.getMsgBody().put("type", MsgType.TEXT);
            // this.getMsgBody().put("msg", msg);

            ObjectNode jsonNode = JsonNodeFactory.instance.objectNode();
            jsonNode.put("type", MsgType.TEXT);
            jsonNode.put("msg", msg);
            this.getMsgBody().put("msg", jsonNode);
            this.setInit(true);

        }

        return this.getMsgBody();
    }

更改了下getbody方法。这样就可以了。

要回复问题请先登录注册