重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1.首先我们新建一个Java开发包WeiXinSDK
公司主营业务:网站建设、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出常德免费做网站回馈大家。
2.包路径:com.ansitech.weixin.sdk
测试的前提条件:
假如我的公众账号微信号为:vzhanqun
我的服务器地址为:
下面我们需要新建一个URL的请求地址
我们新建一个Servlet来验证URL的真实性,具体接口参考
接入指南
3.新建com.ansitech.weixin.sdk.WeixinUrlFilter.java
这里我们主要是获取微信服务器法师的验证信息,具体验证代码如下
[java] view plain copy print?
package com.ansitech.weixin.sdk;
import com.ansitech.weixin.sdk.util.SHA1;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WeixinUrlFilter implements Filter {
//这个Token是给微信开发者接入时填的
//可以是任意英文字母或数字,长度为3-32字符
private static String Token = "vzhanqun1234567890";
@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("WeixinUrlFilter启动成功!");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//微信服务器将发送GET请求到填写的URL上,这里需要判定是否为GET请求
boolean isGet = request.getMethod().toLowerCase().equals("get");
System.out.println("获得微信请求:" + request.getMethod() + " 方式");
if (isGet) {
//验证URL真实性
String signature = request.getParameter("signature");// 微信加密签名
String timestamp = request.getParameter("timestamp");// 时间戳
String nonce = request.getParameter("nonce");// 随机数
String echostr = request.getParameter("echostr");//随机字符串
ListString params = new ArrayListString();
params.add(Token);
params.add(timestamp);
params.add(nonce);
//1. 将token、timestamp、nonce三个参数进行字典序排序
Collections.sort(params, new ComparatorString() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
//2. 将三个参数字符串拼接成一个字符串进行sha1加密
String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));
if (temp.equals(signature)) {
response.getWriter().write(echostr);
}
} else {
//处理接收消息
}
}
@Override
public void destroy() {
}
}
好了,不过这里有个SHA1算法,我这里也把SHA1算法的源码给贴出来吧!
4.新建com.ansitech.weixin.sdk.util.SHA1.java
[java] view plain copy print?
/*
* 微信公众平台(JAVA) SDK
*
* Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ansitech.weixin.sdk.util;
import java.security.MessageDigest;
/**
* pTitle: SHA1算法/p
*
* @author qsyangyangqisheng274@163.com
*/
public final class SHA1 {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* Takes the raw bytes from the digest and formats them correct.
*
* @param bytes the raw bytes from the digest.
* @return the formatted bytes.
*/
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密文转换成十六进制的字符串形式
for (int j = 0; j len; j++) {
buf.append(HEX_DIGITS[(bytes[j] 4) 0x0f]);
buf.append(HEX_DIGITS[bytes[j] 0x0f]);
}
return buf.toString();
}
public static String encode(String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
messageDigest.update(str.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
5.把这个Servlet配置到web.xml中
[html] view plain copy print?
filter
description微信消息接入接口/description
filter-nameWeixinUrlFilter/filter-name
filter-classcom.ansitech.weixin.sdk.WeixinUrlFilter/filter-class
/filter
filter-mapping
filter-nameWeixinUrlFilter/filter-name
url-pattern/api/vzhanqun/url-pattern
/filter-mapping
好了,接入的开发代码已经完成。
6.下面就把地址URL和密钥Token填入到微信申请成为开发者模式中吧。
说明:
本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。
在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。另外,在做内容回复时用到了图灵机器人的api接口,这是一个自然语言解析的开放平台,可以帮我们解决整个微信开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。
1.1 在登录微信官方平台之后,开启开发者模式,此时需要我们填写url和token,所谓url就是我们自己服务器的接口,用WechatServlet.java来实现,相关解释已经在注释中说明,代码如下:
[java] view plain copy
package demo.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import demo.process.WechatProcess;
/**
* 微信服务端收发消息接口
*
* @author pamchen-1
*
*/
public class WechatServlet extends HttpServlet {
/**
* The doGet method of the servlet. br
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
/** 读取接收到的xml消息 */
StringBuffer sb = new StringBuffer();
InputStream is = request.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String s = "";
while ((s = br.readLine()) != null) {
sb.append(s);
}
String xml = sb.toString(); //次即为接收到微信端发送过来的xml数据
String result = "";
/** 判断是否是微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回 */
String echostr = request.getParameter("echostr");
if (echostr != null echostr.length() 1) {
result = echostr;
} else {
//正常的微信处理流程
result = new WechatProcess().processWechatMag(xml);
}
try {
OutputStream os = response.getOutputStream();
os.write(result.getBytes("UTF-8"));
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. br
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
1.2 相应的web.xml配置信息如下,在生成WechatServlet.java的同时,可自动生成web.xml中的配置。前面所提到的url处可以填写例如:http;//服务器地址/项目名/wechat.do
[html] view plain copy
?xml version="1.0" encoding="UTF-8"?
web-app version="2.5"
xmlns=""
xmlns:xsi=""
xsi:schemaLocation="
"
servlet
descriptionThis is the description of my J2EE component/description
display-nameThis is the display name of my J2EE component/display-name
servlet-nameWechatServlet/servlet-name
servlet-classdemo.servlet.WechatServlet/servlet-class
/servlet
servlet-mapping
servlet-nameWechatServlet/servlet-name
url-pattern/wechat.do/url-pattern
/servlet-mapping
welcome-file-list
welcome-fileindex.jsp/welcome-file
/welcome-file-list
/web-app
1.3 通过以上代码,我们已经实现了微信公众平台开发的框架,即开通开发者模式并成功接入、接收消息和发送消息这三个步骤。
下面就讲解其核心部分——解析接收到的xml数据,并以文本类消息为例,通过图灵机器人api接口实现智能回复。
2.1 首先看一下整体流程处理代码,包括:xml数据处理、调用图灵api、封装返回的xml数据。
[java] view plain copy
package demo.process;
import java.util.Date;
import demo.entity.ReceiveXmlEntity;
/**
* 微信xml消息处理流程逻辑类
* @author pamchen-1
*
*/
public class WechatProcess {
/**
* 解析处理xml、获取智能回复结果(通过图灵机器人api接口)
* @param xml 接收到的微信数据
* @return 最终的解析结果(xml格式数据)
*/
public String processWechatMag(String xml){
/** 解析xml数据 */
ReceiveXmlEntity xmlEntity = new ReceiveXmlProcess().getMsgEntity(xml);
/** 以文本消息为例,调用图灵机器人api接口,获取回复内容 */
String result = "";
if("text".endsWith(xmlEntity.getMsgType())){
result = new TulingApiProcess().getTulingResult(xmlEntity.getContent());
}
/** 此时,如果用户输入的是“你好”,在经过上面的过程之后,result为“你也好”类似的内容
* 因为最终回复给微信的也是xml格式的数据,所有需要将其封装为文本类型返回消息
* */
result = new FormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(), xmlEntity.getToUserName(), result);
return result;
}
}
2.2 解析接收到的xml数据,此处有两个类,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通过反射的机制动态调用实体类中的set方法,可以避免很多重复的判断,提高代码效率,代码如下:
[java] view plain copy
package demo.entity;
/**
* 接收到的微信xml实体类
* @author pamchen-1
*
*/
public class ReceiveXmlEntity {
private String ToUserName="";
private String FromUserName="";
private String CreateTime="";
private String MsgType="";
private String MsgId="";
private String Event="";
private String EventKey="";
private String Ticket="";
private String Latitude="";
private String Longitude="";
private String Precision="";
private String PicUrl="";
private String MediaId="";
private String Title="";
private String Description="";
private String Url="";
private String Location_X="";
private String Location_Y="";
private String Scale="";
private String Label="";
private String Content="";
private String Format="";
private String Recognition="";
public String getRecognition() {
return Recognition;
}
public void setRecognition(String recognition) {
Recognition = recognition;
}
public String getFormat() {
return Format;
}
public void setFormat(String format) {
Format = format;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getLocation_X() {
return Location_X;
}
public void setLocation_X(String locationX) {
Location_X = locationX;
}
public String getLocation_Y() {
return Location_Y;
}
public void setLocation_Y(String locationY) {
Location_Y = locationY;
}
public String getScale() {
return Scale;
}
public void setScale(String scale) {
Scale = scale;
}
public String getLabel() {
return Label;
}
public void setLabel(String label) {
Label = label;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public String getPicUrl() {
return PicUrl;
}
public void setPicUrl(String picUrl) {
PicUrl = picUrl;
}
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
public String getEventKey() {
return EventKey;
}
public void setEventKey(String eventKey) {
EventKey = eventKey;
}
public String getTicket() {
return Ticket;
}
public void setTicket(String ticket) {
Ticket = ticket;
}
public String getLatitude() {
return Latitude;
}
public void setLatitude(String latitude) {
Latitude = latitude;
}
public String getLongitude() {
return Longitude;
}
public void setLongitude(String longitude) {
Longitude = longitude;
}
public String getPrecision() {
return Precision;
}
public void setPrecision(String precision) {
Precision = precision;
}
public String getEvent() {
return Event;
}
public void setEvent(String event) {
Event = event;
}
public String getMsgId() {
return MsgId;
}
public void setMsgId(String msgId) {
MsgId = msgId;
}
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
java调用微信支付接口方法:\x0d\x0aRequestHandlerrequestHandler=newRequestHandler(super.getRequest(),super.getResponse());\x0d\x0a\x0d\x0a//获取token//两小时内有效,两小时后重新获取\x0d\x0a\x0d\x0aToken=requestHandler.GetToken();\x0d\x0a\x0d\x0a//更新token到应用中\x0d\x0a\x0d\x0arequestHandler.getTokenReal();\x0d\x0a\x0d\x0aSystem.out.println("微信支付获取token=======================:"+Token);\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//requestHandler初始化\x0d\x0a\x0d\x0arequestHandler.init();\x0d\x0a\x0d\x0arequestHandler.init(appid,appsecret,appkey,partnerkey,key);\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//--------------------------------本地系统生成订单-------------------------------------\x0d\x0a\x0d\x0a//设置package订单参数\x0d\x0a\x0d\x0aSortedMappackageParams=newTreeMap();\x0d\x0a\x0d\x0apackageParams.put("bank_type","WX");//支付类型\x0d\x0a\x0d\x0apackageParams.put("body","xxxx");//商品描述\x0d\x0a\x0d\x0apackageParams.put("fee_type","1");//银行币种\x0d\x0a\x0d\x0apackageParams.put("input_charset","UTF-8");//字符集\x0d\x0a\x0d\x0apackageParams.put("notify_url","");//通知地址这里的通知地址使用外网地址测试,注意80端口是否打开。\x0d\x0a\x0d\x0apackageParams.put("out_trade_no",no);//商户订单号\x0d\x0a\x0d\x0apackageParams.put("partner",partenerid);//设置商户号\x0d\x0a\x0d\x0apackageParams.put("spbill_create_ip",super.getRequest().getRemoteHost());//订单生成的机器IP,指用户浏览器端IP\x0d\x0a\x0d\x0apackageParams.put("total_fee",String.valueOf(rstotal));//商品总金额,以分为单位\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//设置支付参数\x0d\x0a\x0d\x0aSortedMapsignParams=newTreeMap();\x0d\x0a\x0d\x0asignParams.put("appid",appid);\x0d\x0a\x0d\x0asignParams.put("noncestr",noncestr);\x0d\x0a\x0d\x0asignParams.put("traceid",PropertiesUtils.getOrderNO());\x0d\x0a\x0d\x0asignParams.put("timestamp",timestamp);\x0d\x0a\x0d\x0asignParams.put("package",packageValue);\x0d\x0a\x0d\x0asignParams.put("appkey",this.appkey);\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//生成支付签名,要采用URLENCODER的原始值进行SHA1算法!\x0d\x0a\x0d\x0aStringsign="";\x0d\x0a\x0d\x0atry{\x0d\x0a\x0d\x0asign=Sha1Util.createSHA1Sign(signParams);\x0d\x0a\x0d\x0a}catch(Exceptione){\x0d\x0a\x0d\x0ae.printStackTrace();\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//增加非参与签名的额外参数\x0d\x0a\x0d\x0asignParams.put("sign_method","sha1");\x0d\x0a\x0d\x0asignParams.put("app_signature",sign);\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//api支付拼包结束------------------------------------\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//获取prepayid\x0d\x0a\x0d\x0aStringprepayid=requestHandler.sendPrepay(signParams);\x0d\x0a\x0d\x0aSystem.out.println("prepayid:"+prepayid);\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//--------------------------------生成完成---------------------------------------------\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//生成预付快订单完成,返回给android,ios掉起微信所需要的参数。\x0d\x0a\x0d\x0aSortedMappayParams=newTreeMap();\x0d\x0a\x0d\x0apayParams.put("appid",appid);\x0d\x0a\x0d\x0apayParams.put("noncestr",noncestr);\x0d\x0a\x0d\x0apayParams.put("package","Sign=WXPay");\x0d\x0a\x0d\x0apayParams.put("partnerid",partenerid);\x0d\x0a\x0d\x0apayParams.put("prepayid",prepayid);\x0d\x0a\x0d\x0apayParams.put("appkey",this.appkey);\x0d\x0a\x0d\x0a//这里除1000是因为参数长度限制。\x0d\x0a\x0d\x0ainttime=(int)(System.currentTimeMillis()/1000);\x0d\x0a\x0d\x0apayParams.put("timestamp",String.valueOf(time));\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0aSystem.out.println("timestamp:"+time);\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//签名\x0d\x0a\x0d\x0aStringpaysign="";\x0d\x0a\x0d\x0atry{\x0d\x0a\x0d\x0apaysign=Sha1Util.createSHA1Sign(payParams);\x0d\x0a\x0d\x0a}catch(Exceptione){\x0d\x0a\x0d\x0ae.printStackTrace();\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0apayParams.put("sign",paysign);\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//拼json数据返回给客户端\x0d\x0a\x0d\x0aBasicDBObjectbackObject=newBasicDBObject();\x0d\x0a\x0d\x0abackObject.put("appid",appid);\x0d\x0a\x0d\x0abackObject.put("noncestr",payParams.get("noncestr"));\x0d\x0a\x0d\x0abackObject.put("package","Sign=WXPay");\x0d\x0a\x0d\x0abackObject.put("partnerid",payParams.get("partnerid"));\x0d\x0a\x0d\x0abackObject.put("prepayid",payParams.get("prepayid"));\x0d\x0a\x0d\x0abackObject.put("appkey",this.appkey);\x0d\x0a\x0d\x0abackObject.put("timestamp",payParams.get("timestamp"));\x0d\x0a\x0d\x0abackObject.put("sign",payParams.get("sign"));\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0aStringbackstr=dataObject.toString();\x0d\x0a\x0d\x0aSystem.out.println("backstr:"+backstr);\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0areturnbackstr;\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a====================到此为止,预付款订单已生成,并且已返回客户端====================\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//坐等微信服务器通知,通知的地址就是生成预付款订单的notify_url\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0aResponseHandlerresHandler=newResponseHandler(request,response);\x0d\x0a\x0d\x0aresHandler.setKey(partnerkey);\x0d\x0a\x0d\x0a//创建请求对象\x0d\x0a\x0d\x0a//RequestHandlerqueryReq=newRequestHandler(request,response);\x0d\x0a\x0d\x0a//queryReq.init();\x0d\x0a\x0d\x0aif(resHandler.isTenpaySign()==true){\x0d\x0a\x0d\x0a//商户订单号\x0d\x0a\x0d\x0aStringout_trade_no=resHandler.getParameter("out_trade_no");\x0d\x0a\x0d\x0aSystem.out.println("out_trade_no:"+out_trade_no);\x0d\x0a\x0d\x0a//财付通订单号\x0d\x0a\x0d\x0aStringtransaction_id=resHandler.getParameter("transaction_id");\x0d\x0a\x0d\x0aSystem.out.println("transaction_id:"+transaction_id);\x0d\x0a\x0d\x0a//金额,以分为单位\x0d\x0a\x0d\x0aStringtotal_fee=resHandler.getParameter("total_fee");\x0d\x0a\x0d\x0a//如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee\x0d\x0a\x0d\x0aStringdiscount=resHandler.getParameter("discount");\x0d\x0a\x0d\x0a//支付结果\x0d\x0a\x0d\x0aStringtrade_state=resHandler.getParameter("trade_state");\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//判断签名及结果\x0d\x0a\x0d\x0aif("0".equals(trade_state)){\x0d\x0a\x0d\x0a//------------------------------\x0d\x0a\x0d\x0a//即时到账处理业务开始\x0d\x0a\x0d\x0a//------------------------------\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0aSystem.out.println("----------------业务逻辑执行-----------------");\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——\x0d\x0a\x0d\x0aSystem.out.println("----------------业务逻辑执行完毕-----------------");\x0d\x0a\x0d\x0aSystem.out.println("success");//请不要修改或删除\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0aSystem.out.println("即时到账支付成功");\x0d\x0a\x0d\x0a//给财付通系统发送成功信息,财付通系统收到此结果后不再进行后续通知\x0d\x0a\x0d\x0aresHandler.sendToCFT("success");\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a//给微信服务器返回success否则30分钟通知8次\x0d\x0a\x0d\x0areturn"success";\x0d\x0a\x0d\x0a}else{\x0d\x0a\x0d\x0aSystem.out.println("通知签名验证失败");\x0d\x0a\x0d\x0aresHandler.sendToCFT("fail");\x0d\x0a\x0d\x0aresponse.setCharacterEncoding("utf-8");\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a}else{\x0d\x0a\x0d\x0aSystem.out.println("fail-Md5failed");