一、下载jar包wx-tools-2.0.0.jar,并加入项目里
https://download.csdn.net/download/dmw412724/10457645
二、在项目本资源里放一个wx.properties,里面保存这个公众号的基本信息
wx.appId=wx1ed9
wx.appSecret=bcb647b7
wx.token=12345
wx.aesKey=
wx.mchId=
三、创建菜单
public class WxTest {
public static IService iService = new WxService();
public void createMenu() throws WxErrorException{
//创建一个菜单
WxMenuButton menuButton = new WxMenuButton();
menuButton.setName("百度");
menuButton.setType(WxConsts.BUTTON_VIEW);
menuButton.setUrl("http://www.baidu.com");
//菜单集合
List<WxMenuButton> buttons = new ArrayList<WxMenuButton> ();
buttons.add(menuButton);
WxMenu menu = new WxMenu();
menu.setButton(buttons);
iService.createMenu(menu , false);
}
public static void main(String[] args) throws Exception {
new WxTest().createMenu();
}
}
四、wx回调网址
@Resource
private IService iService;
@RequestMapping("/webMessageUrl")
public String webMessageUrl(HttpServletRequest request,String signature,String timestamp,String nonce,String echostr) throws IOException{
//校验发来的信息的合法性
if (iService.checkSignature(signature, timestamp, nonce, echostr)){
//把发来的xml的信息解码成对象。然后做进一步的操作
WxXmlMessage wxXmlMessage = XStreamTransformer.fromXml(WxXmlMessage.class, request.getInputStream());
log.info(wxXmlMessage);
return StringUtils.isBlank(echostr)? SUCCESS: echostr;
}
return null;
}
但是这一步操作xml需要的xstream的版本要高一些,至少要高于1.4.5