wechat-mp

wechat-mp development tool

License

License

GroupId

GroupId

me.gaigeshen.wechat
ArtifactId

ArtifactId

wechat-mp
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

wechat-mp
wechat-mp development tool
Project URL

Project URL

https://github.com/gaigeshen/wechat-mp
Source Code Management

Source Code Management

https://github.com/gaigeshen/wechat-mp

Download wechat-mp

How to add to project

<!-- https://jarcasting.com/artifacts/me.gaigeshen.wechat/wechat-mp/ -->
<dependency>
    <groupId>me.gaigeshen.wechat</groupId>
    <artifactId>wechat-mp</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/me.gaigeshen.wechat/wechat-mp/
implementation 'me.gaigeshen.wechat:wechat-mp:1.1.0'
// https://jarcasting.com/artifacts/me.gaigeshen.wechat/wechat-mp/
implementation ("me.gaigeshen.wechat:wechat-mp:1.1.0")
'me.gaigeshen.wechat:wechat-mp:jar:1.1.0'
<dependency org="me.gaigeshen.wechat" name="wechat-mp" rev="1.1.0">
  <artifact name="wechat-mp" type="jar" />
</dependency>
@Grapes(
@Grab(group='me.gaigeshen.wechat', module='wechat-mp', version='1.1.0')
)
libraryDependencies += "me.gaigeshen.wechat" % "wechat-mp" % "1.1.0"
[me.gaigeshen.wechat/wechat-mp "1.1.0"]

Dependencies

compile (14)

Group / Artifact Type Version
org.dom4j : dom4j jar 2.1.1
jaxen : jaxen jar 1.2.0
com.thoughtworks.xstream : xstream jar 1.4.9
com.alibaba : fastjson jar 1.2.39
org.apache.httpcomponents : fluent-hc jar 4.5.8
org.apache.httpcomponents : httpmime jar 4.5.8
com.google.guava : guava jar 23.0
org.bouncycastle : bcprov-jdk15on jar 1.61
commons-beanutils : commons-beanutils jar 1.9.3
commons-codec : commons-codec jar 1.11
org.apache.commons : commons-lang3 jar 3.8.1
org.slf4j : slf4j-api jar 1.7.26
ch.qos.logback : logback-classic Optional jar 1.2.3
org.projectlombok : lombok Optional jar 1.18.6

provided (1)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar 3.1.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

微信公众号开发工具

License Build Status Maven Central Sonatype Nexus (Snapshots) GitHub last commit

消息的处理,此处演示常用的配置案例

以下配置完毕之后,在微信公众平台启用服务器配置,并且将服务器地址配置为指向/messages

@Configuration
@ServletComponentScan(basePackageClasses = { ServletMessageFilter.class })
public class WechatMpConfiguration {
  // 消息处理器链,会获取所有的消息处理器
  // 请根据需要合理选择消息处理器的抽象实现,具体查看源代码
  @Bean
  public MessageProcessorChain messageProcessorChain(ApplicationContext ctx) {
    return new DefaultMessageProcessorChain(new ArrayList<>(ctx.getBeansOfType(MessageProcessor.class).values()));
  }
    
  // 读取微信的全局配置,配置的值要与微信公众平台上的服务器配置保持一致
  @Bean
  public Config wechatConfig() {
    String appid = ...; 
    String secret = ...;
    String token = ...;
    String encodingAesKey = ...; // 如果配置了该值的话,将会对消息进行加解密操作
    Assert.state(StringUtils.isNotBlank(appid), "appid is required");
    Assert.state(StringUtils.isNotBlank(secret), "secret is required");
    Assert.state(StringUtils.isNotBlank(token), "token is required");
    return Config.builder().appid(appid).secret(secret).token(token).encodingAesKey(encodingAesKey).build();
  }
}

请求接口使用方式

请注意,请求体数据和响应体数据都假定为json格式

// 卡券签名相关,如果需要开发卡券业务的话
@Bean
public CardSignatureCalculator cardSignatureCalculator(RequestExecutor executor) {
  return new CardSignatureCalculator(executor);
}

// 网页开发相关,如果需要开发网页授权之类的业务的话
@Bean
public JsApiSignatureCalculator jsApiSignatureCalculator(RequestExecutor executor) {
  return new JsApiSignatureCalculator(executor);
}

// 此对象将用于主动请求接口
@Bean(destroyMethod = "close")
public RequestExecutor wechatRequestExecutor(Config config) {
  return new RequestExecutor(new HttpClientExecutor(2000, 2000, 3000), config);
}
// 构造请求对象
XXXRequest req = ...

// 执行请求
XXXResponse resp = executor.execute(req);

// 打印请求结果是否成功,以及其他数据
System.out.println("error code: " + resp.getErrorCode());
System.out.println("error message: " + resp.getErrorMessage());

如何知道使用哪个请求对象

// 将微信文档里面的请求链接,在项目里面全局搜索即可
// 搜索的时候,建议只搜索不带参数的形式的链接,因为很可能参数有占位符名称不一致

假如微信的某个接口没有找到对应的请求类怎么办

// 自定义实现响应接口
public class YourResponse extends AbstractResponse {
  // 这里是定义响应的数据字段
}
// 自定义实现请求接口
public class YourRequest implements Request<YourResponse> {
  // 这里是定义请求的数据字段

  // 此处返回请求方式,此方法有默认实现
  @Override
  public HttpMethod httpMethod() {
    return HttpMethod.GET;
  }
  @Override
  public String requestUri() {
    // 此处写请求接口的地址
    return "https://api.weixin.qq.com/...";
  }
}

如何定义multipart/form-data形式的请求

public class YourUploadRequest implements Request<YourUploadResponse> {
  // 普通字段
  private String type;
  // 此处的字段类型必须上传项目,字段名称对应接口文档的说明即可
  private UploadItem media;
  @Override
  public String requestUri() {
    // 此处写请求接口的地址
    return "https://api.weixin.qq.com/...";
  }
}

假如我需要定义请求体是单个文件的请求

// 请求类直接实现上传项目,此种类型的请求对象内的所有字段将被忽略
public class YourUploadRequest implements Request<YourUploadResponse>, UploadItem {
  private final UploadItem item;
  private YourUploadRequest(UploadItem item) {
    this.item = item;
  }
  public static YourUploadRequest create(UploadItem item) {
    Validate.notNull(item, "Upload item is required");
    return new YYYUploadRequest(item);
  }
  @Override
  public String requestUri() {
    // 此处写请求接口的地址
    return "https://api.weixin.qq.com/...";
  }
  @Override
  public String getFilename() {
    return item.getFilename();
  }
  @Override
  public byte[] getContent() {
    return item.getContent();
  }
}

我在自定义请求类的时候,我不想定义对应的响应类

// 这样的话,响应对象里只有响应码和响应消息数据
public class YourRequest implements Request<EmptyDataResponse> {
  @Override
  public String requestUri() {
    // 此处写请求接口的地址
    return "https://api.weixin.qq.com/...";
  }
}

我在自定义请求类的时候,想直接打印请求原始的返回结果

// 这样的话,不会尝试自动转换为响应对象,而是直接将接口的原始响应内容打印出来
executor.execute(req, new StringResponseBodyPrintHandler());

对于下载文件类的请求,如何定义响应类

// 实现请求结果处理器
public class YourResponseBodyHandler implements ResponseBodyHandler {
  @Override
  public void handle(String contentType, byte[] content) {
    // 第一个参数告诉你,响应的数据类型
    // 第二个参数是传递给你原始数据
    // 接下来你可以处理数据了
  }
}

// 执行请求的时候,将刚才定义的请求结果处理器传入即可
executor.execute(req, new YourResponseBodyHandler());

// 系统现在已经定义了常用的下载文件类的请求结果处理器,请查看具体的源代码了解

Versions

Version
1.1.0
1.0.0