Home >WeChat Applet >WeChat Development >Java WeChat development framework wechat4j introductory tutorial

Java WeChat development framework wechat4j introductory tutorial

高洛峰
高洛峰Original
2017-02-15 10:43:482862browse

wechat4j

What is wechat4j?

wechat develop framework for java (Wechat development framework JAVA version, the simplest and easiest to use Wechat development framework)

wechat4j can be used What to do?

wechat4j is a jar package that helps you develop WeChat applications. Using it, you can develop WeChat public account applications in just a few seconds without paying attention to too much detail.

wechat4j Quick Start

You can download the wechat4j sample project and then modify it based on it. If you want to build it yourself, you can use wechat4j to build a WeChat development environment in just two steps.

  1. Create a web project and import jdk and related web project jar packages.

  2. Download the wechat4j.jar package, download address wechat4j download.

  3. Create the wechat4j configuration file, create the wechat4j.properties file in the src directory (java root directory), and configure the relevant information of your WeChat official account. The content is as follows:

#you server url
wechat.url=
#you wechat token
wechat.token=token
#message secret key,if don't set then message is cleartext
wechat.encodingaeskey=

#wechat appid
wechat.appid=appid
#wechat app secret
wechat.appsecret=secret

#wechat access token server ,when you save in db,must implement you server class
#this class must extend org.sword.wechat4j.token.DbAccessTokenServer
#if no this property,then token server is default memery accesstoken server()
wechat.accessToken.server.class=

#jsapi_ticket customer server class name,
#this class must extend org.sword.wechat4j.token.server.CustomerServer
#if no this property,then ticket server is default memery ticket server
wechat.ticket.jsapi.server.class=

You can also find the wechat4j.properties.sample file in the META-INF directory of the jar package, copy it to the src directory and modify the name. For the meaning of the configuration items in the wechat4j.properties configuration file, see Interpretation of the wechat4j configuration file

After the above steps, your WeChat project is completely set up.

wechat4j operating environment

The minimum java operating environment required by wechat4j is jdk1.6

wechat4j.jar’s dependent jar package

  • commons-codec.jar 1.3 or above

  • commons-lang3.jar

  • log4j.jar 1.2 or above

  • fastjson-1.2.0.jar

  • fluent-hc-4.3.6.jar (httpclient dependency)

  • httpclient-4.3.6.jar

  • httpcore-4.3.3.jar (httpclient dependency)

  • servlet-api.jar If you It is a web project. Importing packages that support web projects will include, for example, the tomcat package

Develop your own WeChat application

After the wechat4j development environment is set up, You can start developing your own WeChat application. For example, I have a WeChat account whose token is lejian. Let’s use her as an example to illustrate.

Create your own public account service class

To create your own WeChat public account service class, you need to inherit the WechatSupport class of wechat4j, and then implement its abstract method. The following uses text message processing as an example

public class Lejian extends WechatSupport{public Lejian(HttpServletRequest request, String token) {super(request, token);
}@Overrideprotected void onText() {this.wechatRequest.getFromUserName();
String content = "test ok";responseText(content);
}
}

onText() in the above code is an abstract method of WechatSupport, which requires your own class to implement, indicating the processing of text messages. In the example, it is receiving After receiving the user's message, return the "test ok" text message to the user.

Create WeChat service address

Create WeChat service address (your own server address configured in WeChat public platform) servlet class. If it is springmvc, create the corresponding controller, if it is struts, create the corresponding action class. An example of the servlet class is as follows:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Lejian lejian = new Lejian(request, TOKEN);
String result = lejian.execute();
response.getOutputStream().write(result.getBytes());
}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Lejian lejian = new Lejian(request, TOKEN);
String result = lejian.execute();
response.getOutputStream().write(result.getBytes());

}

After the above two steps, your WeChat service can be run

More introduction to the java WeChat development framework wechat4j For tutorials and related articles, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn