开发者社区> 问答> 正文

dingtalk.corp.message.corpconversation.asyncsend企业会话消息异步发送,Missing method

一、背景
在做钉钉企业会话异步发送的接口时,按照官方文档,
(1)我先封装好了请求参数类: AsyncMessage. java
(2)封装好http请求工具类(用的钉钉官方给的示例代码):
(3)然后写了一个企业会话异步发送业务类:AsyncSendMessageService.java
(4)最后是测试类:AsyncSendMessageServiceTest.java


源码在后面


二、异常
请求参数中明明有method,可以发送请求后,却报了一个Missing method的错误;


错误信息如下:
com.alibaba.fastjson.JSONException: syntax error, pos 1, json : <?xml version="1.0" encoding="utf-8" ?><error_response><code>21</code><msg>Missing method</msg><sub_msg>http传入的参数加入method字段</sub_msg><request_id>12jrtcf50tcfa</request_id></error_response><!--top010178205069.n.et2-->
    at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1394)
    at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1287)
    at com.alibaba.fastjson.JSON.parse(JSON.java:139)
    at com.alibaba.fastjson.JSON.parse(JSON.java:130)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:206)
    at com.ray.dingtalk.util.HttpHelper.httpPost(HttpHelper.java:115)
    at com.ray.dingtalk.service.message.asyncsend.AsyncSendMessageService.sendMessage(AsyncSendMessageService.java:58)
    at com.ray.dingtalk.test.AsyncSendMessageServiceTest.testSendNotice(AsyncSendMessageServiceTest.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


展开
收起
rayner 2017-10-13 16:08:20 6274 0
1 条回答
写回答
取消 提交回答
  • Redingtalk.corp.message.corpconversation.asyncsend企业会话消息异步发送,Missing meth
    三。源码
    AsyncMessage
    package com.ray.dingtalk.model.message.asyncsend;

    /**@desc  :
    *
    * 参考文档地址:https://open-doc.dingtalk.com/docs/
    * doc.htm?spm=a219a.7629140.0.0.hZSBgV&treeId=385&
    * articleId=28915&docType=2
    *
    * @author: shirayner
    * @date  : 2017年10月13日 下午1:53:40
    */
    public class AsyncMessage {
        //1.公共参数
        //必须 ,API接口名称
        private String method;
        //必须 ,钉钉提供的授权Token
        private String session;
        //必须,时间戳,格式为yyyy-MM-dd HH:mm:ss,时区为GMT+8,例如:2015-01-01 12:00:00。
        //淘宝API服务端允许客户端请求最大时间误差为10分钟。
        private String timestamp;
        //必须,API协议版本,可选值:2.0。
        private String v;
        
        //2.请求参数
        
        private String msgtype;
        //必须,微应用的id
        private Number agent_id;
        //必须,与msgtype对应的消息体,参见文档
        private String msgcontent;
        
        //否
        private String []     userid_list;
        //否
        private Boolean to_all_user;
        
        
        
        
        
        
        
        
        
        public String getMethod() {
            return method;
        }
        public void setMethod(String method) {
            this.method = method;
        }
        public String getSession() {
            return session;
        }
        public void setSession(String session) {
            this.session = session;
        }
        public String getTimestamp() {
            return timestamp;
        }
        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }
        public String getV() {
            return v;
        }
        public void setV(String v) {
            this.v = v;
        }
        public String getMsgtype() {
            return msgtype;
        }
        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }
        public Number getAgent_id() {
            return agent_id;
        }
        public void setAgent_id(Number agent_id) {
            this.agent_id = agent_id;
        }
        public String getMsgcontent() {
            return msgcontent;
        }
        public String[] getUserid_list() {
            return userid_list;
        }
        public void setUserid_list(String[] userid_list) {
            this.userid_list = userid_list;
        }
        public Boolean getTo_all_user() {
            return to_all_user;
        }
        public void setTo_all_user(Boolean to_all_user) {
            this.to_all_user = to_all_user;
        }
        public void setMsgcontent(String msgcontent) {
            this.msgcontent = msgcontent;
        }


        
    }


    2.AsyncSendMessageService
    package com.ray.dingtalk.service.message.asyncsend;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.ray.dingtalk.model.message.asyncsend.AsyncMessage;
    import com.ray.dingtalk.util.HttpHelper;

    /**@desc  : 主动发送异步消息
    *
    * @author: shirayner
    * @date  : 2017年10月13日 下午1:39:46
    */
    public class AsyncSendMessageService {
        private static final String SEND_ASYNCSENDMESSAGE_URL="https://eco.taobao.com/router/rest";

        
        
        public void sendMessage(String accessToken) throws Exception{
            //1.封装请求参数
            String method="dingtalk.corp.message.corpconversation.asyncsend";
            String session =accessToken;
            String timestamp= String.valueOf( System.currentTimeMillis() / 1000 );
            String v="2.0";

            String msgtype ="text";
            Number agent_id=128838526;
            String msgcontent="发送异步消息测试";
            Boolean to_all_user=true;
            
            AsyncMessage cca=new AsyncMessage();
            cca.setMethod(method);
            cca.setSession(session);
            cca.setTimestamp(timestamp);
            cca.setV(v);
            
            cca.setMsgtype(msgtype);
            cca.setAgent_id(agent_id);
            cca.setMsgcontent(msgcontent);
            cca.setTo_all_user(to_all_user);
            
            //1.准备POST请求参数
            Object data=JSON.toJSON(cca);      //将javaBean 转为 json
            System.out.println(data);

            //2.获取请求url
            String url=SEND_ASYNCSENDMESSAGE_URL;

            //3.发起POST请求,获取返回结果
            JSONObject jsonObject=HttpHelper.httpPost(url, data);
            System.out.println("jsonObject:"+jsonObject.toString());

            //4.解析结果,获取
            if (null != jsonObject) {  

                //5.错误消息处理
                if (0 != jsonObject.getInteger("errcode")) {  
                    int errCode = jsonObject.getInteger("errcode");
                    String errMsg = jsonObject.getString("errmsg");
                    throw new Exception("error code:"+errCode+", error message:"+errMsg);
                }  
            }          
            
        }
        
        
        
    }



    3.AsyncSendMessageServiceTest


    /**@desc  :
    *
    * @author: shirayner
    * @date  : 2017年10月13日 下午2:36:27
    */
    public class AsyncSendMessageServiceTest {

        @Test
        public void testSendNotice() throws Exception {
            String accessToken=AuthHelper.getAccessToken(Env.CORP_ID, Env.CORP_SECRET);
            
            AsyncSendMessageService asm=new AsyncSendMessageService();
            asm.sendMessage(accessToken);
        }
        
        
        
    }


    2017-10-13 16:11:16
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Customer Success Dashboard GoDaddy Business Score 立即下载
Customer Success Dashboard GoD 立即下载
RocketMQ 新特性揭秘- Request-Reply 立即下载