为了账号安全,请及时绑定邮箱和手机立即绑定

Java实现微信的自动回复总是报错2

Java实现微信的自动回复总是报错2

IT小公举 2017-04-10 18:14:54
是按照慕课网的教程抄的代码,可是不知道为什么总是报错Servlet.service() for servlet [weixinServlet] in context with path [/One] threw exception [Servlet execution threw an exception] with root causejava.lang.NoClassDefFoundError: javax/jms/TextMessage错误出现在这一行我把weixinservlet类和checkutil还有messageutil类发在下面了,求大牛指点错在哪里了↓↓下面是WeixinServlet类的dopost方法protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");PrintWriter out  = resp.getWriter();try {Map<String, String> map = MessageUtil.xmlToMap(req);String fromUserName = map.get("FromUserName");String toUserName = map.get("ToUserName");String msgType = map.get("MsgType");String content = map.get("Content");String message = null;if("text".equals(msgType)){TextMassage text  = new TextMassage();text.setFromUserName(toUserName);text.setToUserName(fromUserName);text.setMsgType("text");text.setContent("您发送的消息是"+content);message = MessageUtil.textMessageToXml(text);}out.print(message);} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{out.close();}}↓↓这是checkutil类package com.imooc.util;import java.security.MessageDigest;import java.util.Arrays;public class CheckUtil {public static final String token = "imooc";public static boolean checkSignature(String signature,String timestamp,String nonce){String[] arr=new String[]{token,timestamp,nonce};Arrays.sort(arr);StringBuffer content = new StringBuffer();for(int i = 0;i<arr.length;i++){content.append(arr[i]);}String temp = getSha1(content.toString());return temp.equals(signature);}public static String getSha1(String str){       if(str==null||str.length()==0){           return null;       }       char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',               'a','b','c','d','e','f'};       try {           MessageDigest mdTemp = MessageDigest.getInstance("SHA1");           mdTemp.update(str.getBytes("UTF-8"));           byte[] md = mdTemp.digest();           int j = md.length;           char buf[] = new char[j*2];           int k = 0;           for (int i = 0; i < j; i++) {               byte byte0 = md[i];               buf[k++] = hexDigits[byte0 >>> 4 & 0xf];               buf[k++] = hexDigits[byte0 & 0xf];                 }           return new String(buf);       } catch (Exception e) {           // TODO: handle exception           return null;       }}} ↓↓这是messageutil类package com.imooc.util;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.jms.TextMessage;import javax.servlet.http.HttpServletRequest;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import com.immoc.po.TextMassage;import com.thoughtworks.xstream.XStream;public class MessageUtil {public static Map<String,String>xmlToMap(HttpServletRequest request)throws IOException,DocumentException{Map<String,String>map=new HashMap<String, String>();SAXReader reader = new SAXReader();InputStream ins = request.getInputStream();Document doc = reader.read(ins);Element root = doc.getRootElement();List<Element> list = root.elements();for(Element e:list){map.put(e.getName(),e.getText());}ins.close();return map;}public static String textMessageToXml(TextMassage text){XStream xstream = new XStream();xstream.alias("xml", text.getClass());return  xstream.toXML(text);}}
查看完整描述

2 回答

?
qq_流年之殇_0

TA贡献2条经验 获得超1个赞

我也遇到这个问题了,你解决了吗?

查看完整回答
1 反对 回复 2018-05-01
?
胜利女神在微笑

TA贡献67条经验 获得超59个赞

TextMessage这个类你创建了吗,导包导错了

查看完整回答
反对 回复 2017-08-03
  • 2 回答
  • 0 关注
  • 1519 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信