WxJava - 微信开发 Java SDK,支持微信支付、开放平台、公众号、企业号/企业微信、小程序等的后端开发
Maven 引用方式
<!--微信公众号SDK --><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>4.0.0</version></dependency><!--微信小程序SDK --><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-miniapp</artifactId><version>4.0.0</version></dependency><!--微信支付SDK --><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-pay</artifactId><version>4.0.0</version></dependency>
微信小程序:
weixin-java-miniapp微信支付:
weixin-java-pay微信开放平台:
weixin-java-open公众号(包括订阅号和服务号):
weixin-java-mp企业号/企业微信:
weixin-java-cp
项目地址
开源地址:https://github.com/Wechat-Group/WxJava
配置Bean
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType.EVENT;import java.util.HashMap;import java.util.Map;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.gdhz.config.wx.WxmpConfig;import com.gdhz.wx.card.config.WechatPayConfig;import com.gdhz.wx.handler.KFSessionHandler;import com.gdhz.wx.handler.LocationHandler;import com.gdhz.wx.handler.MaMsgHandler;import com.gdhz.wx.handler.MenuHandler;import com.gdhz.wx.handler.MsgHandler;import com.gdhz.wx.utils.WechatPayHttpClientUtil;import com.github.binarywang.wxpay.config.WxPayConfig;import com.github.binarywang.wxpay.constant.WxPayConstants;import com.github.binarywang.wxpay.service.WxPayService;import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;import cn.binarywang.wx.miniapp.api.WxMaService;import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;import cn.binarywang.wx.miniapp.config.WxMaConfig;import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;import lombok.AllArgsConstructor;import me.chanjar.weixin.common.api.WxConsts.EventType;import me.chanjar.weixin.common.api.WxConsts.XmlMsgType;import me.chanjar.weixin.mp.api.WxMpMessageRouter;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;@AllArgsConstructor@Configurationpublic class WXConfiguration {private final LocationHandler locationHandler;private final MsgHandler msgHandler;private final KFSessionHandler kfHandler;private final MenuHandler menuHandler;private final WxmpConfig properties;private final MaMsgHandler maMsgHandler;// 微信支付@Beanpublic WxPayService wxPayService() {WxPayService wxPayService = new WxPayServiceImpl();WxPayConfig payConfig = new WxPayConfig();payConfig.setAppId(WechatPayConfig.appid);payConfig.setApiV3HttpClient(WechatPayHttpClientUtil.getCloseableHttpClient());payConfig.setApiV3Key(WechatPayConfig.privateKey);payConfig.setMchId(WechatPayConfig.mchId);payConfig.setMchKey(WechatPayConfig.privateKey);payConfig.setSignType(WxPayConstants.SignType.HMAC_SHA256);payConfig.setCertSerialNo(WechatPayConfig.mchSerialNo);// payConfig.setKeyPath("classpath:apiclient_cert.p12");// payConfig.setPrivateCertPath("classpath:apiclient_cert.pem");payConfig.setPrivateKeyPath("classpath:apiclient_key.pem");wxPayService.setConfig(payConfig);return wxPayService;}// 微信公众号@Beanpublic WxMpService wxMpService() {final WxmpConfig configs = this.properties;if (configs == null) {throw new RuntimeException("配置文件失败");}WxMpService service = new WxMpServiceImpl();Map<String, WxMpConfigStorage> configStorages = new HashMap<String, WxMpConfigStorage>();WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();configStorage.setAppId(configs.getOffiAppid());configStorage.setSecret(configs.getOffiSecret());configStorages.put(configs.getOffiAppid(), configStorage);service.setMultiConfigStorages(configStorages);return service;}// 微信小程序@Beanpublic WxMaService wxMaService() {final WxmpConfig configs = this.properties;if (configs == null) {throw new RuntimeException("配置文件失败");}WxMaService service = new WxMaServiceImpl();Map<String, WxMaConfig> configStorages = new HashMap<String, WxMaConfig>();WxMaDefaultConfigImpl configStorage = new WxMaDefaultConfigImpl();configStorage.setAppid(configs.getMpAppid());configStorage.setSecret(configs.getMpSecret());configStorages.put(configs.getMpAppid(), configStorage);service.setWxMaConfig(configStorage);return service;}@Beanpublic WxMpMessageRouter messageRouter(WxMpService wxMpService) {final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);// 上报地理位置事件newRouter.rule().async(false).msgType(EVENT).event(EventType.LOCATION).handler(this.locationHandler).end();// 接收地理位置消息newRouter.rule().async(false).msgType(XmlMsgType.LOCATION).handler(this.locationHandler).end();// 点击菜单连接事件newRouter.rule().async(false).msgType(EVENT).event(EventType.CLICK).handler(this.menuHandler).end();// 接入客服newRouter.rule().async(false).msgType(XmlMsgType.TRANSFER_CUSTOMER_SERVICE).handler(this.kfHandler).end();// 默认newRouter.rule().async(false).handler(this.msgHandler).end();return newRouter;}/*** 小程序客服** @param wxMaService* @return*/@Beanpublic WxMaMessageRouter maMessageRouter(WxMaService wxMaService) {final WxMaMessageRouter newRouter = new WxMaMessageRouter(wxMaService);// 接入客服 默认newRouter.rule().async(false).handler(this.maMsgHandler).end();return newRouter;}}
使用案例-小程序二维码:
import java.awt.image.RenderedImage;import java.io.File;import java.io.IOException;import java.util.Base64;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletResponse;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import cn.binarywang.wx.miniapp.api.WxMaService;import lombok.extern.log4j.Log4j2;import me.chanjar.weixin.common.error.WxErrorException;//小程序服务@Log4j2@RestController@RequestMapping("/wx-ma")public class WXMaController {// 小程序@Autowiredprivate WxMaService wxMaService;// 小程序二维码@GetMapping("/getCode/{code}")public void getCode(@PathVariable("code") String code, boolean isBase64, HttpServletResponse response) {response.setDateHeader("Expires", 0);response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");response.addHeader("Cache-Control", "post-check=0, pre-check=0");response.setHeader("Pragma", "no-cache");try {if (!StringUtils.isEmpty(code) && code.length() <= 32) {File maQrcodeFile = wxMaService.getQrcodeService().createWxaCodeUnlimit(code, null);if (maQrcodeFile != null) {RenderedImage bgImage = ImageIO.read(maQrcodeFile);if (isBase64) {ImageIO.write(bgImage, "jpg", Base64.getEncoder().wrap(response.getOutputStream()));} else {ImageIO.write(bgImage, "jpg", response.getOutputStream());}}}} catch (WxErrorException e) {log.error("小程序创建二维码失败:{}", e.getMessage());} catch (IOException e) {log.error("文件操作失败: {}", e.getMessage());}}}
文章转载自聊聊IT技术,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




