1
回答
使用消息路由为什么无法发送消息?我写了几次 还是没成功过,求助~~~
终于搞明白,存储TCO原来是这样算的>>>   

weixin-java-tools的消息路由 一直无法使用 

这是我重新建的工程 jar包都是maven下载下来的

就在servlet里面测试消息路由。。
结果还是无法使用
求指点迷津
package com.nykjwx.demo;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;


public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected WxMpInMemoryConfigStorage wxMpConfigStorage;
	protected WxMpService wxMpService;
	protected WxMpMessageRouter wxMpMessageRouter;
	
	
	@Override public void init() throws ServletException {
	    super.init();

	    wxMpConfigStorage = new WxMpInMemoryConfigStorage();
	    wxMpConfigStorage.setAppId("wx1e107463ff65337e"); // 设置微信公众号的appid
	    wxMpConfigStorage.setSecret("2cb63f17f691b0b6d422f5ac14cbcf60"); // 设置微信公众号的app corpSecret
	    wxMpConfigStorage.setToken("nykjweixin"); // 设置微信公众号的token
	    wxMpConfigStorage.setAesKey("qv9covJzWGNk5MVuOjyRQMXJQHVmJWry62N86WKOMRT"); // 设置微信公众号的EncodingAESKey

	    wxMpService = new WxMpServiceImpl();
	    wxMpService.setWxMpConfigStorage(wxMpConfigStorage);

	    WxMpMessageHandler handler = new WxMpMessageHandler() {
	       @Override
	       public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> arg1, WxMpService arg2, WxSessionManager arg3) throws WxErrorException {
	    	   System.out.println(wxMessage.toString());
	    	    WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content("测试加密消息").fromUser(wxMessage.getToUserName())
	            .toUser(wxMessage.getFromUserName()).build();
	    	    return m;
	       }
	    };

	    wxMpMessageRouter = new WxMpMessageRouter(wxMpService);
	    wxMpMessageRouter
	        .rule()
	        .async(false)
	        .content("哈哈") // 拦截内容为“哈哈”的消息
	        .handler(handler)
	        .end();
	  }

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
	    response.setStatus(HttpServletResponse.SC_OK);

	    String signature = request.getParameter("signature");
	    String nonce = request.getParameter("nonce");
	    String timestamp = request.getParameter("timestamp");

	    if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
	      // 消息签名不正确,说明不是公众平台发过来的消息
	      response.getWriter().println("非法请求");
	      return;
	    }

	    String echostr = request.getParameter("echostr");
	    if (StringUtils.isNotBlank(echostr)) {
	      // 说明是一个仅仅用来验证的请求,回显echostr
	      response.getWriter().println(echostr);
	      return;
	    }

	  }
		

}


希望好心人能够指出我的错误~

感激不尽~

举报
chen8643766
发帖于2年前 1回/1K+阅
顶部