package weixin.Guan Jia.core.controller;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.List;
import javax.servlet.servlet exception;
import javax.servlet.http.http servlet;
import javax.servlet.http.http servlet request;
import javax.servlet.http.http无servlet轮询;
importorg.spring framework.beans.factory.annotation.auto wired;
import weixin.Guan Jia.account.entity.weixinaccountentity;
import weixin.Guan Jia.account.service.weixinaccountservicei;
import weixin.Guan Jia.account.service.impl.weixinaccountserviceimpl;
import weixin.Guan Jia.core.service.impl.wechatservice;
import weixin.Guan Jia.core.util.sign util;
//*
核心请求处理类
*
*
@author liufeng
* @date 2013-05-18
*/
publicclassweixinservletextendshttpservlet {
privatestaticfinallongserialversionuid=4440739483644821986 l;
@Autowired
专用xinaccountserviceiweixinaccountservice;
@Override
公共语音初始化(throwsservletexception )。
weixinaccountservice=newweixinaccountserviceimpl (;
}
//*
*确认请求来自微信服务器
*/
公共void doget (httpservletrequestrequest,HttpServletResponse response ) )。
throwsServletException,IOException {
//微信加密签名
string signature=request.getparameter (signature );
//时间戳
string timestamp=request.getparameter (' timestamp );
//随机数
string nonce=request.getparameter (' nonce );
//随机字符串
stringechostr=request.getparameter (echostr );
printwriter out=response.get writer (;
listweixinaccountentities=weixinaccountservice
. getlist (weixinaccountentity.class;
验证signature以验证请求,如果验证成功,则直接返回echostr,表示访问成功,否则表示访问失败
weixinaccountentityaccount : weixinaccountentities ({
sign util.check signature (account.getaccounttoken )、signature、
timestamp,nonce ) }{
out.print(Echostr;
}
}
out.close (;
out=null;
}
//*
*处理来自微信服务器的消息
*/
公共void dopost (httpservletrequestrequest,HttpServletResponse response ) )。
throwsServletException,IOException {
//将请求、响应的编码全部设为UTF-8 (防止中文乱码) )。
request.setcharacterencoding (utf-8 );
response.setcharacterencoding (utf-8 );
微信服务微信服务=网络服务(;
//调用核心业务类接收和处理消息
stringrespmessage=wechatservice.coreservice (request;
//响应消息
printwriter out=response.get writer (;
out.print(respmessage;
out.close (;
}
}