Development of Java wechat public platform (2) receiving of wechat server post message body

In the last article, we described in detail how to connect our application server and wechat Tencent server, and finally the connection was successful, I wonder if you found that in the previous [controller], I defined a get method and a post method, but we used the get method in the process of use. Here, let's talk about the use of our reserved post method!

When we have completed the server verification, after the user sends a message to the official account every time, or produces a custom menu click event, the server configuration URL that developers fill out will get the message and events that WeChat server has pushed. Then developers can respond to their own business logic, such as reply messages, etc. Through this sentence, we can know that the communication between all wechat servers and our application servers is completed through the post message body, so here we will talk about how to accept the wechat post message body!

(1) Message type and message format

As mentioned above, all our communication with wechat servers is basically completed through the post message body. First, we understand the following types of message bodies, which can be roughly divided into two types:

Common message types: text message, picture message, voice message, video message, small video message, geographic location message and link message

Event message type: follow / cancel follow events, scan QR code events with parameters, report geographic location events, customize menu events, push events when clicking menu to pull messages, and push events when clicking menu jump links. Message type: all message bodies pushed by wechat server are in XML format;

(2) Message retry mechanism

If the wechat server fails to receive a response within five seconds, it will disconnect and re initiate the request. It will retry three times in total. If the server cannot guarantee to process and reply within five seconds, it can directly reply to the empty string. The wechat server will not process this and will not initiate retry. However, the [customer service message interface] can be used to push the message again later.

(3) Message receiving and processing

We mentioned earlier that the message body of wechat is in XML format, so I wrote a messageutil here to process the message format. The approximate code is as follows:

Some dependencies need to be used in this method body, and the following parts need to be added to the POM file:

Then modify the post method in our wechatsecurity controller as follows to receive and process messages:

Because we have opened our developer mode before, then when we send our messages to the official account here, we can see that our message body has entered and parsed successfully in the background. Here I export the original ID of WeChat. The screenshots are as follows:

Here, I just received the message body and converted it into a map, but did not do it for the message. In the next article, we will talk about the classification and processing of messages! Thank you for reading. If you have any questions, you can leave a message for discussion!

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>