Publish an open source java WeChat public account to develop MVC framework (jwx)

Recently, a Java WeChat public account MVC development framework has been open-sourced on github. The address is: https://github.com/jweixin/jwx
jwx (java WeChat public account development MVC framework)

    jwx is an open source java public account development MVC framework, Based on the spring configuration file and WeChat message or event annotations, one or more WeChat official account service requests are processed through the WeChat context. There are two main purposes. It encapsulates the WeChat request xml message as a java entity object, and converts the returned object into an xml response message; the second is to encapsulate the WeChat interface as a java service. The WeChat official account uses web services as messages to interact with third-party platforms. The data formats are mainly xml and json. The common web request response mechanism uses the xml data format for interaction, and the WeChat interface service uses the json data format. jwx mainly encapsulates these two aspects, and draws on the request processing method of springmvc, uses the WeixinDispatcherServlet class as the message distribution controller, generates request messages or event entities through the message assembly factory, and processes them in the message strategy according to the message or event type. The factory finds the processing strategy and obtains the corresponding WeChat processing method. After the Servlet obtains the processing method, the request thread pool obtains the thread to call the WeChat method, and generates the requested xml response according to the return value of the WeChat method. This documentation will describe the features, quick start, configuration, extension and other aspects of the jwx framework in chapters.

1. Automatic processing of characteristic

    message rearrangement, providing message rearrangement cache interface
    plaintext/encryption mode imperceptible switching
    Commonly used WeChat interface service encapsulation
    Provide thread pool to execute WeChat method, method call thread pool size can be configured
    long task message push
    through WeChat context configuration Supports processing of multiple WeChat public accounts and
    provides a unified exception handling mechanism
    Provide access_token automatic update mechanism ,
    request message assembly
    and flexible response message types

2. Quick Start

This chapter provides the simplest example. The user sends a text request message of foo on the WeChat official account, and the official account responds to a text response message of bar.
1. maven configuration file Generate a webapp project

through maven, for example, the project name is weixin, add the jwx dependency in the maven configuration file pom.xml, the 1.0.0 jar package of jwx has been submitted to the maven central warehouse, search the jwx keyword through the central warehouse You can get the jar package dependency configuration.

<dependency>
    <groupId>com.github.jweixin</groupId>
    <artifactId>jwx</artifactId>
    <version>1.0.0</version>
</dependency>

2. web.xml file configuration

web.xml is a web application The configuration file, jwx obtains configuration information from the spring configuration file, so the spring context must be configured; in addition, the WeChat message processing and distribution servlet (WeixinDispatcherServlet) needs to be configured to process the request messages or events sent by WeChat. jwx has no dependencies on springmvc, and the web mvc framework can be configured according to actual needs.

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>weixin</servlet-name>
    <servlet-class>com.github.jweixin. jwx.servlet.WeixinDispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>weixin</servlet-name>
    <url -pattern>/wx/*</url-pattern>
</servlet-mapping>

    load-on-startup indicates that the servlet is loaded in the web application startup phase, and the number represents the startup sequence. If the project uses the springmvc framework, the number can be adjusted For 2, put it behind the springmvc framework to start loading, but in fact, the startup order of the Servlet does not matter much.
    The spring configuration is necessary for jwx. If the spring context is not configured, jwx will report an error during the startup phase.
    The url-pattern pattern matches the URL configuration configured by the WeChat official account platform server. If you need to process multiple WeChat official accounts, you can configure multiple servlet-mappings or use path wildcards to match multiple url links.

3. The spring configuration file

The spring configuration file applicationContext.xml needs to configure WeixinConfigurer, which is the only required configuration item for jwx. If it is not configured, an error will be reported during the startup phase.

<context:component-scan base-package="com.github.jweixin.jwx.weixin.service" />

<bean class="com.github.jweixin.jwx.config.WeixinConfigurer">
    <property name="packages" >
        <list>
            <value>com.telecomjs.yc.controller</value>
        </list>
    </property>
</bean>

    component-scan configures the WeChat interface service class, which contains the commonly used WeChat public account interface services, For example, service content such as menu management, message service, QR code service, user management, WeChat webpage authorization, and material management can be injected into the web application controller class and WeChat controller class through the @Autowired annotation. This configuration is not required.
    WeixinConfigurer is the only part that needs to be configured. The packages attribute must be configured, which contains the list of WeChat controller package paths. WeixinDispatcherServlet will scan the package path and the subpackage paths below it during the startup phase. If the class has the @Weixin annotation, the class will be Loaded into the WeChat context as a WeChat controller class.
    In addition to the packages attribute, which must be configured, other configurations have default values, including message cache, size of WeChat method thread pool, WeChat method call timeout threshold, etc. This part of the content is explained in the configuration section.

4. Write the WeChat controller class

When the above 3 parts are configured, all the configuration files are over. Is it very simple? Next, we only need to write the WeChat controller class to make our WeChat public account come alive. The WeChat controller class is an ordinary class annotated with @Weixin, which is very similar to the controller in sprngmvc, and the execution of the method is also very similar. We build a java class WeixinController under the com.telecomjs.yc.controller package, as follows:

package com.telecomjs.yc.controller;

import com.github.jweixin.jwx.context.Weixin;
import com.github.jweixin.jwx. message.annotation.TextMsg;

@Weixin(value="/wx/coreServlet",
   appID="xxx",
   appSecret="xxx",
   encodingAESKey="xxx",
   token="xxx")
public class WeixinController {

    @TextMsg("foo")
    public String foo(){
        return "bar";
    }
}

    @Weixin needs to configure the value, which is actually the last part of the URL in the WeChat server configuration, of course not including the domain name and web application The context, remember, can not include the web application context, the other four parts of the configuration content is also the public account configuration content, we only need to log in to the official account to see and fill in it. If encodingAESKey is not configured, encrypted messages cannot be processed. If there is a log4j configuration file, an alarm message will be given during the startup phase.

    The same official account can be configured with multiple @Weixin annotated controller classes, and only one of them needs to have the other 4 configuration items. If multiple controller classes are configured with the other 4 configuration items, if the corresponding configuration item values ​​are not The same, the startup phase will report an error.

    Different WeChat official accounts are distinguished by the value of @Weixin, which is also the search keyword in the WeChat context.

    The foo method is annotated with @TextMsg, which is a defined WeChat method, which is loaded into the WeChat context object through package scanning when the servlet starts. jwx has designed a set of WeChat annotations for WeChat message or event types, which basically cover the message and event types defined by WeChat Official Accounts.

    @TextMsg is a text message annotation, representing the request type is a text message, and the value value is the content of the text message sent. To handle text adaptation mode, @TextMsg also supports regular expression adaptation mode, which is described in the configuration section.

    In this example, the WeChat method does not set parameters, but can actually set parameters flexibly. For example, we can set parameters such as HttpServletRequest request, HttpServletResponse response, InMessage in, WeixinContext context, etc. in the method, which are also described in the configuration section.

    In this example, the return type of the method is String, and the message content representing the response is a text message. jwx provides a wealth of return value types, which will be described in detail in the configuration section.
    5. Start the web application

    The above is the entire content of this simplest example. Let's start the web application, enter our official account, enter the foo text to submit, and see if the content of bar is returned. If so, congratulations, You have already mastered the usage of jwx. More content is waiting for you below!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326554505&siteId=291194637