In the past two days, I wanted to learn WeChat public account development, so I searched online and started practicing. During the process, due to various problems, (the description was incomplete, some articles popped up a new constant without knowing how, It didn’t say where it was defined, and the problem with the jar package version cost me a day), so I will record it here.
1. First, you must register a WeChat public account. Go to Du Niang to search the WeChat public platform and enter this page to register (skip this step if you already have an account):
2. Then use Eclipse to create a new project. Here I am building a web project, jdk is 1.8, and tomcat is 8.5.
3 .In order to establish a connection with WeChat, after building the project, first create a new class and name it: CheckUtil. Its function is to serve as a verification tool for connecting to WeChat. The code is as follows:
import java.security.MessageDigest; import java.util.Arrays; public class CheckUtil { public static final String tooken = "自行定义"; //开发者自行定义Tooken public static boolean checkSignature(String signature,String timestamp,String nonce){ //1.定义数组存放tooken,timestamp,nonce String[] arr = {tooken,timestamp,nonce}; //2.对数组进行排序 Arrays.sort(arr); //3.生成字符串 StringBuffer sb = new StringBuffer(); for(String s : arr){ sb.append(s); } //4.sha1加密,网上均有现成代码 String temp = getSha1(sb.toString()); //5.将加密后的字符串,与微信传来的加密签名比较,返回结果 return temp.equals(signature); } public static String getSha1(String str){ if(str==null||str.length()==0){ return null; } char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f'}; try { MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); mdTemp.update(str.getBytes("UTF-8")); byte[] md = mdTemp.digest(); int j = md.length; char buf[] = new char[j*2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; buf[k++] = hexDigits[byte0 & 0xf]; } return new String(buf); } catch (Exception e) { // TODO: handle exception return null; } } }
4. Then create a new servlet , rewrite the doGet method to receive the GET request from WeChat. Part of the code is as follows:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); PrintWriter out = response.getWriter(); if(CheckUtil.checkSignature(signature, timestamp, nonce)){ //如果校验成功,将得到的随机字符串原路返回 out.print(echostr); } }
5. After the code is written, we need a tool to map our intranet link to the public network so that WeChat can access it. Go to our backend. Here I use ngrok, a free mapping tool. Just go to Du Niang to search and download it. After downloading, unzip it and put it in a designated location. Press the Win key and R key at the same time, enter cmd, and press Enter to enter. Dos environment, switch to the drive letter where ngrock is located, enter ngrock http 8080 and press Enter (start tomcat before doing this):
6. Press Enter and wait for a while, that is You can get the public network link. The link given in the shaded area shown in the figure below can directly access the link content under the local machine 127.0.0.1:8080, which are the addresses corresponding to the http protocol and https protocol:
7. Log in to the WeChat public account platform, slide to the bottom, and click Development in the lower left corner - Basic configuration:
Click the submit button , the prompt submission is successful, which means that WeChat can access our own backend.
Related recommendations:
Java implements graphic and text code examples for WeChat public platform development
WeChat public account payment development (java) examples Detailed explanation
The above is the detailed content of Detailed explanation of Java+WeChat public account development process steps. For more information, please follow other related articles on the PHP Chinese website!

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

This article explains Java's NIO API for non-blocking I/O, using Selectors and Channels to handle multiple connections efficiently with a single thread. It details the process, benefits (scalability, performance), and potential pitfalls (complexity,

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function