WxJava icon indicating copy to clipboard operation
WxJava copied to clipboard

Add JSON format support for WeChat Mini Program message push

Open Copilot opened this issue 2 months ago • 1 comments

Implements JSON format support for WeChat Mini Program outbound messages by adding the WxMaJsonOutMessage class.

Problem

Currently, the WeChat Mini Program SDK only supports XML format for message push responses through WxMaXmlOutMessage. According to WeChat's official documentation, JSON format should also be supported for message push scenarios.

Solution

Added WxMaJsonOutMessage class that provides JSON format support while maintaining full compatibility with existing XML functionality:

// Create JSON format message
WxMaJsonOutMessage jsonMessage = WxMaJsonOutMessage.builder()
    .fromUserName("your_mini_program")
    .toUserName("user_openid")
    .msgType(WxConsts.XmlMsgType.TEXT)
    .createTime(System.currentTimeMillis() / 1000)
    .build();

// Convert to JSON
String jsonResponse = jsonMessage.toJson();
// Output: {"toUserName":"user_openid","fromUserName":"your_mini_program","createTime":1758727378,"msgType":"text"}

// Encrypted JSON support
String encryptedJson = jsonMessage.toEncryptedJson(config);

Key Features

  • JSON Serialization: Uses existing WxMaGsonBuilder for consistent JSON formatting
  • Encryption Support: Includes toEncryptedJson(WxMaConfig) method for encrypted responses
  • Builder Pattern: Follows the same builder pattern as WxMaXmlOutMessage
  • Zero Breaking Changes: Existing XML functionality remains unchanged
  • Consistent API: Mirrors the interface of WxMaXmlOutMessage for familiar usage

Implementation Details

The new class follows established patterns from other WxJava modules (like weixin-java-cp's WxCpMessage.toJson()) and maintains the project's coding standards:

  • Uses Lombok annotations for consistency
  • Includes comprehensive unit tests
  • Passes all checkstyle validations
  • Follows the existing serialization patterns

This minimal implementation provides developers the flexibility to choose between XML and JSON formats based on their specific needs while keeping the codebase clean and maintainable.

Fixes #小程序消息推送没有对消息体是json数据格式支持

Fixes binarywang/WxJava#3376


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Sep 24 '25 15:09 Copilot

@copilot 你只是定义了WxMaJsonOutMessage类,并没有在项目代码中使用到,就像xml格式的那样

binarywang avatar Nov 17 '25 02:11 binarywang

@copilot 你只是定义了WxMaJsonOutMessage类,并没有在项目代码中使用到,就像xml格式的那样

已完成集成。我创建了 WxMaOutMessage 接口,并更新了整个消息处理基础设施:

  1. WxMaMessageHandler 现在返回 WxMaOutMessage 接口
  2. WxMaMessageRouterWxMaMessageRouterRule 都使用 WxMaOutMessage
  3. WxMaPortalServlet 示例代码已更新,根据配置自动处理 JSON 或 XML 响应格式
  4. 两种消息类型 (WxMaXmlOutMessageWxMaJsonOutMessage) 都实现了统一接口

现在项目中的消息处理就像 XML 格式那样完整集成了 JSON 支持。详细用法请参见更新的 PR 描述。

Commit: 27d5961

Copilot avatar Nov 17 '25 02:11 Copilot