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

是按照慕课网的教程抄的代码,可是不知道为什么总是报错

Servlet.service() for servlet [weixinServlet] in context with path [/One] threw exception [Servlet execution threw an exception] with root cause

java.lang.NoClassDefFoundError: javax/jms/TextMessage

错误出现在这一行

http://img.mukewang.com/58eb5ac9000128a205000359.jpg

我把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 block

e.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);




}



}


IT小公举
浏览 1527回答 2
2回答

qq_流年之殇_0

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

胜利女神在微笑

TextMessage这个类你创建了吗,导包导错了
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java