由于之前柳峰老师在博客上写的都是用servlet 可能会有朋友会问用其他框架可以不可以开发,其实 都是一样的,微信开发不限制语言和框架
需要看其他基础知识的请去看柳峰老师的博客,没有SPINGMVC 基础的朋友也请自己补习下。
以下是验证的代码:
@Controller
public class WeixinController {
@Autowired
private WeixinService weixinService;
// 处理GET请求
@RequestMapping(value = "weixin", method = RequestMethod.GET)
public @ResponseBody String doGet(HttpServletRequest request) {
// 微信加密签名
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
// 请求校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if (SignUtil.checkSignature(signature, timestamp, nonce)) {
return echostr;
}
return "";
}
// 处理POST请求
@RequestMapping(value = "weixin", method = RequestMethod.POST)
public @ResponseBody String doPost(HttpServletRequest request) {
// 微信加密签名
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 请求校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if (SignUtil.checkSignature(signature, timestamp, nonce)) {
return weixinService.processRequest(request);
}
return "";
}
}