JAVA微信二次开发

大家首先自己有条件的,自己注册一个微信公众账号
1.一个微信公众账号(供自己测试使用)
2.一个百度开发账号
3.myeclipse开发工具 
4.需要熟悉jeecg开发
5.微信开发代码如下

标签: JEECG  MiniDao

代码片段(1)[全屏查看所有代码]

1. [代码][Java]代码     

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

packageweixin.guanjia.core.controller;

importjava.io.IOException;

importjava.io.PrintWriter;

importjava.util.List;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.springframework.beans.factory.annotation.Autowired;

importweixin.guanjia.account.entity.WeixinAccountEntity;

importweixin.guanjia.account.service.WeixinAccountServiceI;

importweixin.guanjia.account.service.impl.WeixinAccountServiceImpl;

importweixin.guanjia.core.service.impl.WechatService;

importweixin.guanjia.core.util.SignUtil;

/**

 * 核心请求处理类

 *

 * @author liufeng

 * @date 2013-05-18

 */

publicclassWeixinServletextendsHttpServlet {

    privatestaticfinallongserialVersionUID = 4440739483644821986L;

    @Autowired

    privateWeixinAccountServiceI weixinAccountService;

    @Override

    publicvoidinit()throwsServletException {

         weixinAccountService =newWeixinAccountServiceImpl();

    }

    /**

     * 确认请求来自微信服务器

     */

    publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)

            throwsServletException, IOException {

        // 微信加密签名

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

        // 时间戳

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

        // 随机数

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

        // 随机字符串

        String echostr = request.getParameter("echostr");

        PrintWriter out = response.getWriter();

        List weixinAccountEntities = weixinAccountService

                .getList(WeixinAccountEntity.class);

        // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败

        for(WeixinAccountEntity account : weixinAccountEntities) {

            if(SignUtil.checkSignature(account.getAccounttoken(), signature,

                    timestamp, nonce)) {

                out.print(echostr);

            }

        }

        out.close();

        out =null;

    }

    /**

     * 处理微信服务器发来的消息

     */

    publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)

            throwsServletException, IOException {

        // 将请求、响应的编码均设置为UTF-8(防止中文乱码)

        request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        WechatService wechatService =newWechatService();

        // 调用核心业务类接收消息、处理消息

        String respMessage = wechatService.coreService(request);

        // 响应消息

        PrintWriter out = response.getWriter();

        out.print(respMessage);

        out.close();

    }

}

评论

暂无评论