Comprehensive Java SDK for WeChat Official Account development with complete API coverage for user management, messaging, materials, menus, and WeChat platform features.
npx @tessl/cli install tessl/maven-com-github-binarywang--weixin-java-mp@4.7.0Comprehensive Java SDK for WeChat Official Account development providing complete API coverage for user management, messaging, materials, menus, and all WeChat platform features.
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.7.0</version>
</dependency>import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.common.error.WxErrorException;import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
// Initialize configuration
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
config.setAppId("your-app-id");
config.setSecret("your-app-secret");
// Create service instance
WxMpService wxService = new WxMpServiceImpl();
wxService.setWxMpConfigStorage(config);
// Use specific services
String accessToken = wxService.getAccessToken();
WxMpUser user = wxService.getUserService().userInfo("openid", "zh_CN");The WeChat Official Account Java SDK follows a modular service-oriented architecture:
The SDK supports multiple HTTP client implementations:
Core service initialization and configuration management.
interface WxMpService extends WxService {
String getAccessToken() throws WxErrorException;
String getAccessToken(boolean forceRefresh) throws WxErrorException;
WxMpConfigStorage getWxMpConfigStorage();
void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider);
boolean switchover(String mpId);
WxMpService switchoverTo(String mpId);
}Comprehensive user information management, tagging, and blacklist operations.
interface WxMpUserService {
WxMpUser userInfo(String openid, String lang) throws WxErrorException;
List<WxMpUser> userInfoList(List<String> openids, String lang) throws WxErrorException;
void userUpdateRemark(String openid, String remark) throws WxErrorException;
WxMpUserList userList(String nextOpenid) throws WxErrorException;
}Complete message processing framework including routing, handling, and response building.
class WxMpMessageRouter {
WxMpMessageRouter rule();
WxMpMessageRouterRule async(boolean async);
WxMpMessageRouterRule handler(WxMpMessageHandler handler);
WxMpXmlOutMessage route(WxMpXmlMessage message);
}
interface WxMpMessageHandler {
WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
WxMpService wxMpService, WxSessionManager sessionManager)
throws WxErrorException;
}Upload, download, and manage permanent and temporary media materials.
interface WxMpMaterialService {
WxMediaUploadResult materialFileUpload(String mediaType, String fileName, InputStream inputStream) throws WxErrorException;
WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException;
InputStream materialImageOrVoiceDownload(String mediaId) throws WxErrorException;
WxMpMaterialVideoInfoResult materialVideoInfo(String mediaId) throws WxErrorException;
}Create, update, and delete custom menus for WeChat Official Accounts.
interface WxMpMenuService {
String menuCreate(WxMenu menu) throws WxErrorException;
void menuDelete() throws WxErrorException;
WxMenu menuGet() throws WxErrorException;
WxMpGetSelfMenuInfoResult getSelfMenuInfo() throws WxErrorException;
}Send template messages to users with dynamic content.
interface WxMpTemplateMsgService {
WxMpTemplateMsgResult sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException;
List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException;
String addTemplate(String shortTemplateId) throws WxErrorException;
boolean delPrivateTemplate(String templateId) throws WxErrorException;
}Comprehensive shopping guide functionality for managing shopping guides, buyers, materials, tags, and mass messaging campaigns.
interface WxMpGuideService {
WxMpGuideResult addGuide(WxMpGuideAccount account) throws WxErrorException;
WxMpGuideResult getGuide(String account, String guideOpenid) throws WxErrorException;
WxMpGuideResult updateGuide(WxMpGuideAccount account) throws WxErrorException;
WxMpGuideListResult getGuideList(Integer page, Integer num) throws WxErrorException;
}
interface WxMpGuideBuyerService {
WxMpGuideBuyerResult addGuideBuyer(WxMpGuideBuyer buyer) throws WxErrorException;
WxMpGuideBuyerListResult getGuideBuyerList(String guideAccount, Integer page, Integer num) throws WxErrorException;
WxMpGuideBuyerRelationResult getGuideBuyerRelation(String guideAccount, String openid) throws WxErrorException;
}
interface WxMpGuideTagService {
WxMpGuideTagResult newGuideTagOption(WxMpGuideTagOption tagOption) throws WxErrorException;
WxMpGuideTagResult addGuideBuyerTag(String guideAccount, String openid, List<String> tagValueList) throws WxErrorException;
}Generate temporary and permanent QR codes for user engagement.
interface WxMpQrcodeService {
WxMpQrCodeTicket qrCodeCreateTmp(int sceneId, Integer expireSeconds) throws WxErrorException;
WxMpQrCodeTicket qrCodeCreateTmp(String sceneStr, Integer expireSeconds) throws WxErrorException;
WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException;
WxMpQrCodeTicket qrCodeCreateLastTicket(String sceneStr) throws WxErrorException;
File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException;
String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException;
}Additional specialized services for WeChat Official Account functionality including QR code generation, customer service operations, data analytics, OAuth2 authentication, mass messaging, card management, and more.
Key services include:
interface WxMpQrcodeService {
WxMpQrCodeTicket qrCodeCreateTmp(int sceneId, Integer expireSeconds) throws WxErrorException;
WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException;
File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException;
}
interface WxMpKefuService {
boolean sendKfMessage(WxMpKefuMessage message) throws WxErrorException;
WxMpKfList kfList() throws WxErrorException;
boolean kfAccountAdd(WxMpKfAccount account) throws WxErrorException;
}
interface WxMpDataCubeService {
List<WxDataCubeUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException;
List<WxDataCubeArticleSummary> getArticleSummary(Date beginDate, Date endDate) throws WxErrorException;
}
interface WxOAuth2Service {
String buildAuthorizationUrl(String redirectUri, String scope, String state);
WxOAuth2AccessToken getAccessToken(String code) throws WxErrorException;
WxOAuth2UserInfo getUserInfo(WxOAuth2AccessToken token, String lang) throws WxErrorException;
}
interface WxMpMassMessageService {
WxMpMassSendResult massGroupMessageSend(WxMpMassGroupMessage message) throws WxErrorException;
WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException;
}Additional specialized services for extended WeChat functionality including analytics, OAuth2, mass messaging, and specialized integrations.
Key services include:
interface WxMpConfigStorage {
String getAccessToken();
boolean isAccessTokenExpired();
void expireAccessToken();
void updateAccessToken(WxAccessToken accessToken);
String getAppId();
String getSecret();
String getToken();
String getAesKey();
long getExpiresTime();
String getOAuth2redirectUri();
String getHttpProxyHost();
int getHttpProxyPort();
String getHttpProxyUsername();
String getHttpProxyPassword();
}
class WxMpHostConfig {
public static String buildUrl(WxMpHostConfig hostConfig, String prefix, String path);
// Host configuration for different deployment environments
}class WxErrorException extends Exception {
private WxError error;
public WxError getError();
public String getMessage();
}
class WxError implements Serializable {
private int errorCode;
private String errorMsg;
private String json;
// Error details and codes
}class WxMpXmlMessage implements Serializable {
private String toUser;
private String fromUser;
private Long createTime;
private String msgType;
private String content;
private Long msgId;
private String picUrl;
private String mediaId;
// Additional message fields based on type
}
abstract class WxMpXmlOutMessage {
protected String toUserName;
protected String fromUserName;
protected Long createTime;
protected String msgType;
// Base class for all outgoing messages
}class WxMpEventConstants {
// Card Events
public static final String CardPass = "card_pass_check";
public static final String CardNotPass = "card_not_pass_check";
public static final String UserGetCard = "user_get_card";
public static final String UserGiftingCard = "user_gifting_card";
public static final String UserDelCard = "user_del_card";
public static final String UserConsumeCard = "user_consume_card";
public static final String UserPayFromPayCell = "user_pay_from_pay_cell";
public static final String UserViewCard = "user_view_card";
public static final String UserEnterSessionFromCard = "user_enter_session_from_card";
public static final String UpdateMemberCard = "update_member_card";
public static final String CardSkuRemind = "card_sku_remind";
public static final String CardPayOrder = "card_pay_order";
public static final String SubmitMembercardUserInfo = "submit_membercard_user_info";
// Store Events
public static final String PoiCheckNotify = "poi_check_notify";
// Shake Events
public static final String ShakeAroundUserShake = "ShakeAround_user_shake";
public static final String ShakeAroundLotteryBind = "ShakeAround_lottery_bind";
}
enum WxMpApiUrl {
// Comprehensive API endpoint definitions organized by category
// Device, OAuth2, Menu, Material, User, Card, etc.
}