第一步:配置接口配置信息
测试账号在http://mp./debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index网页中配置 接口配置信息
配置参考微信测试账号管理-配置接口信息
如果是开发账号请在https://mp./advanced/advanced?action=interface&t=advanced/interface&token=637985175&lang=zh_CN配置页面如下
第二步:修改接收微信响应接口如下
package com.wl.wechat.contoller;import com.wl.wechat.model.WxMessageTemplate;import mons.codec.digest.DigestUtils;import mons.lang3.StringUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.xml.bind.JAXBContext;import java.io.InputStream;import java.util.Arrays;/*** Created by wl on /4/10.*/@RestController@RequestMapping("/wx")public class WxController {private static final String TOKEN = "wl123";@RequestMapping("/receiveWx")public void receiveWxToken(HttpServletRequest request,HttpServletResponse response) throws Exception{String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");System.out.println("signature: " + signature);System.out.println("timestamp: " + timestamp);System.out.println("nonce: " + nonce);System.out.println("echostr: " + echostr);String[] params = new String[]{nonce,timestamp,TOKEN};Arrays.sort(params);String signatureResult = DigestUtils.sha1Hex(params[0]+params[1] + params[2]);//校验签名if(!signatureResult.equals(signature)) {throw new RuntimeException("signature is not the same wechat signature is " + signature + " signatureResult is " + signatureResult);}if(StringUtils.isNotBlank(echostr)) {response.getWriter().write(echostr);}InputStream is = request.getInputStream();if(is != null){JAXBContext context = JAXBContext.newInstance(WxMessageTemplate.class);//解析接收到的微信xml格式消息WxMessageTemplate template = (WxMessageTemplate)context.createUnmarshaller().unmarshal(is);String fromUserName = template.getFromUserName();template.setFromUserName(template.getToUserName());template.setToUserName(fromUserName);template.setContent("你好 hello");template.setMsgType("text");response.setCharacterEncoding("utf-8");//返回微信xml消息context.createMarshaller().marshal(template,response.getWriter());}}@RequestMapping("/login")public String login(HttpServletRequest request){return "ok";}}
WxMessageTemplate 如下
package com.wl.wechat.model;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import java.io.Serializable;/*** Created by wl on /5/30.*/@XmlRootElement(name = "xml")public class WxMessageTemplate implements Serializable {private String toUserName;private String fromUserName;private String createTime;private String msgType;private String content;@XmlElement(name = "ToUserName")public String getToUserName() {return toUserName;}public void setToUserName(String toUserName) {this.toUserName = toUserName;}@XmlElement(name = "FromUserName")public String getFromUserName() {return fromUserName;}public void setFromUserName(String fromUserName) {this.fromUserName = fromUserName;}@XmlElement(name = "CreateTime")public String getCreateTime() {return createTime;}public void setCreateTime(String createTime) {this.createTime = createTime;}@XmlElement(name = "MsgType")public String getMsgType() {return msgType;}public void setMsgType(String msgType) {this.msgType = msgType;}@XmlElement(name = "Content")public String getContent() {return content;}public void setContent(String content) {this.content = content;}}
注意响应给微信的 FromUserName 和 ToUserName要互换,响应给微信的xml文本中不能有空格且服务不能发生异常,否则微信会通知用户:该公众号提供的服务出现故障,请稍后重试 的提醒
上面的代码就是当用户向公众号发送文本信息时,公众号都会向用户发送 "你好 hello"的文本消息
测试截图如下
使用jackson处理xml格式的请求 返回
加入依赖
<dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId><version>2.8.10</version></dependency>
controller代码如下
@RequestMapping(value = "/receiveWx",produces = MediaType.APPLICATION_XML_VALUE)@ResponseBodypublic Object receiveWxToken(@RequestBody(required = false) WxMessageTemplate template,HttpServletRequest request, HttpServletResponse response) throws Exception{String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");System.out.println("signature: " + signature);System.out.println("timestamp: " + timestamp);System.out.println("nonce: " + nonce);System.out.println("echostr: " + echostr);String[] params = new String[]{nonce,timestamp,TOKEN};Arrays.sort(params);String signatureResult = DigestUtils.sha1Hex(params[0]+params[1] + params[2]);//校验签名if(!signatureResult.equals(signature)) {throw new RuntimeException("signature is not the same wechat signature is " + signature + " signatureResult is " + signatureResult);}if(StringUtils.isNotBlank(echostr)) {response.getWriter().write(echostr);return null;}String fromUserName = template.getFromUserName();template.setFromUserName(template.getToUserName());template.setToUserName(fromUserName);template.setContent("你好 hello");template.setMsgType("text");return template;}
实体类如下(需要加上jackson xml 的注解)
package com.wl.wechat.model;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import java.io.Serializable;/*** Created by wl on /5/30.*/@XmlRootElement(name = "xml")@JacksonXmlRootElement(localName = "xml")public class WxMessageTemplate implements Serializable {private String toUserName;private String fromUserName;private String createTime;private String msgType;private String content;@XmlElement(name = "ToUserName")@JacksonXmlProperty(localName = "ToUserName")public String getToUserName() {return toUserName;}public void setToUserName(String toUserName) {this.toUserName = toUserName;}@XmlElement(name = "FromUserName")@JacksonXmlProperty(localName = "FromUserName")public String getFromUserName() {return fromUserName;}public void setFromUserName(String fromUserName) {this.fromUserName = fromUserName;}@XmlElement(name = "CreateTime")@JacksonXmlProperty(localName = "CreateTime")public String getCreateTime() {return createTime;}public void setCreateTime(String createTime) {this.createTime = createTime;}@XmlElement(name = "MsgType")@JacksonXmlProperty(localName = "MsgType")public String getMsgType() {return msgType;}public void setMsgType(String msgType) {this.msgType = msgType;}@XmlElement(name = "Content")@JacksonXmlProperty(localName = "Content")public String getContent() {return content;}public void setContent(String content) {this.content = content;}}