{"id":27552451,"url":"https://github.com/lkqm/weixin-gateway","last_synced_at":"2025-04-19T10:07:52.274Z","repository":{"id":42556102,"uuid":"249110662","full_name":"lkqm/weixin-gateway","owner":"lkqm","description":"微信公众号消息网关, 基于注解分发消息@WxController、@WxEvent、@WxMessage, 提供脚手架项目。","archived":false,"fork":false,"pushed_at":"2023-05-09T18:46:40.000Z","size":57,"stargazers_count":1,"open_issues_count":3,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2023-07-26T21:59:57.121Z","etag":null,"topics":["microservice","weixin","weixin-gateway","wxjava"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lkqm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-22T04:08:57.000Z","updated_at":"2021-10-27T11:34:51.000Z","dependencies_parsed_at":"2022-08-26T20:59:18.535Z","dependency_job_id":null,"html_url":"https://github.com/lkqm/weixin-gateway","commit_stats":null,"previous_names":[],"tags_count":2,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lkqm%2Fweixin-gateway","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lkqm%2Fweixin-gateway/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lkqm%2Fweixin-gateway/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lkqm%2Fweixin-gateway/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lkqm","download_url":"https://codeload.github.com/lkqm/weixin-gateway/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249672724,"owners_count":21309064,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["microservice","weixin","weixin-gateway","wxjava"],"created_at":"2025-04-19T10:07:51.691Z","updated_at":"2025-04-19T10:07:52.269Z","avatar_url":"https://github.com/lkqm.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# weixin-gateway\n![Maven Central](https://img.shields.io/maven-central/v/com.github.lkqm/weixin-gateway)\n\n微信公众号消息网关, 基于注解分发消息, 提供脚手架项目。\n\n```java\n@WxController\npublic class InvoiceHandler {\n    @WxEvent(\"user_authorize_invoice\")\n    public void authorEvent(InvoiceAuthEventMessage message) {\n        System.out.println(\"电子发票授权事件\");\n    }\n}\n```\n\n支持: JDK1.7 +\n\n脚手架项目: [weixin-gateway-web](https://github.com/lkqm/weixin-gateway/tree/master/weixin-gateway-web)\n\n## 快速开始\n1. 引入依赖\n    ```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003ecom.github.lkqm\u003c/groupId\u003e\n        \u003cartifactId\u003eweixin-gateway-spring-boot-starter\u003c/artifactId\u003e\n        \u003cversion\u003e${version}\u003c/version\u003e\n    \u003c/dependency\u003e\n    ```\n2. 添加配置(application.properties)\n    ```properties\n    wx.gateway.enabled = ture                      # 是否开启, 关闭后不会自动注入WxPortalController\n    wx.gateway.dev = false                         # 开发者模式, 处理消息跳过签名校验\n    wx.gateway.prefix = /wx/gateway                # 设置回掉路径前缀, 访问地址: /wx/gateway, /wx/gateway/{key}\n    wx.gateway.config.default.appId = @appId       # 配置一个微信配置访问路径key=default, 当key=default访问地址{key}可省略\n    wx.gateway.config.default.token = @token\n    wx.gateway.config.default.aesKey = @aesKey\n    \n    wx.gateway.pool.namePrefix = wx-router-        # 线程名称前缀\n    wx.gateway.pool.coreSize = 10                  # 核心线程大小\n    wx.gateway.pool.maxSize = 100                  # 最大线程大小\n    wx.gateway.pool.keepAlive = 5000               # 空闲线程存活时间(ms)\n    wx.gateway.pool.queueCapacity=                 # 工作队列大小\n    ```\n3. 增加相关的业务处理器\n    ```java\n    @WxController\n    public class InvoiceHandler {\n        @WxEvent(\"user_authorize_invoice\")\n        public void authEvent(InvoiceAuthMessage authMessage, @WxBody String xml, \n                              @WxParam(\"FromUserName\") String fromUser, @WxParam(\"CreateTime\") Integer createTime) {\n        }\n    }\n    ```\n\n## 你需要知道的？\n### 消息分发\n所有的消息分发处理均是异步, 处理消息或事件类型不能重复声明。\n- `@WxController`: 标记类为消息处理类\n- `@WxEvent`: 注解用于事件匹配\n- `@WxMessage`: 注解用于消息匹配\n\n### 参数注入\n响应微信消息的处理方法, 支持注入4种参数, 解析顺序依次向下:\n\n- @WxParam: 方法参数指定注解`@WxParam`, 会自定注入指定xml标记的值(不支持多层嵌套)\n- @WxBody: 方法参数指定注解`@WxBody`, 并且类型String, 会自动注入值为微信请求的xml(非密文)\n- 自定义参数解析: a.实现接口HandlerMethodArgumentResolver, b.注入容器\n- 普通bean: 普通JavaBean, 字段命名需要按照驼峰命名(内部是xml转json, 然后json转对象)\n\n注意: 基本类型参数只会注入默认值\n\n### 多公众号\n可以实现多公众号, 控制层路径地址: /wx/gateway/{key}, 配置自定义参数解析可以实现: HandlerMethodArgumentResolver\n```java\n@Component\npublic class WxMpServiceArgumentResolver implements HandlerMethodArgumentResolver {\n    \n    private Map\u003cString, WxMpService\u003e appIdToWxMpServiceMap = new ConcurrentHashMap\u003c\u003e();\n\n    @Override\n    public boolean supportsParameter(Class\u003c?\u003e type) {\n        return WxMpService.class.isAssignableFrom(type);\n    }\n\n    @Override\n    public Object resolveArgument(Message message) {\n        WxConfig config = message.getWxConfig();\n        return appIdToWxMpServiceMap.get(config.getAppId());\n    }\n}\n```\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flkqm%2Fweixin-gateway","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flkqm%2Fweixin-gateway","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flkqm%2Fweixin-gateway/lists"}