Java WeChat development framework wechat4j Getting Started Tutorial

Source: Internet
Author: User
Wechatdevelopframeworkforjava (JAVA version of the development framework, the simplest and easiest development framework) wechat4jWhat is wechat4j?

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

What can wechat4j do?

Wechat4j is a jar package that helps you develop an application. it takes only a few seconds to develop a public account application. you do not need to pay attention to the details.

Wechat4j quick start

You can download the wechat4j sample project and modify it based on it. If you want to build your own development environment, you only need two steps to use wechat4j.

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

  2. Download the wechat4j. jar package and wechat4j.

  3. Create the wechat4j configuration file, create the wechat4j. properties file under the src Directory (java root directory), and configure your public account information. The content is as follows:

#you server urlwechat.url=#you wechat tokenwechat.token=token#message secret key,if don't set then message is cleartextwechat.encodingaeskey=#wechat appidwechat.appid=appid#wechat app secretwechat.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 serverwechat.ticket.jsapi.server.class=

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

Through the above steps, your project will be fully ready.

Wechat4j runtime environment

The minimum java Runtime Environment required by wechat4j is jdk1.6.

Wechat4j. jar dependent jar package

  • Commons-codec.jar more than 1.3

  • 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 are a web project, importing packages that support web projects will include, such as tomcat packages

Develop your own applications

After setting up the wechat4j development environment, you can start to develop your own applications. For example, if the token of a number is lejian, the following uses her as an example.

Create your own public account service class

To create your own public account service class, you must 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);}}

The onText () in the code above is the abstract method of WechatSupport. you need to implement it in your own class to process text messages. In this example, after receiving a user message, return the "test OK" text message to the user.

Create service address

Create a service address (your own server address configured on the public platform) servlet class. If springmvc is used, the corresponding controller is created. if struts is used, the corresponding action class is created. Servlet class example:

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());}

You can run the service in the above two steps.

For more articles about the java development framework wechat4j Getting Started Tutorial, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.