Building a WeChat Mini Program with a Spring Boot Backend: Step‑by‑Step Guide
This tutorial walks through creating a WeChat mini‑program, setting up a Spring Boot backend with Maven dependencies, configuring SSL, writing REST controllers, invoking the APIs from the mini‑program using wx.request, and deploying the service on a Linux server.
WeChat mini‑programs are increasingly popular, and this article records the author’s experience developing a simple mini‑program together with a Spring Boot backend.
Project setup : In the mini‑program IDE, disable domain verification, and refer to the official component and API documentation.
Backend construction : A Maven project is created with the following
<!-- 统一版本控制 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!-- freemarker渲染页面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- spring boot 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot整合jsp -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>in pom.xml. The application.properties file configures JSP view prefixes, SSL certificate paths, and server port (443).
The main Spring Boot entry class is:
@ComponentScan(basePackages="com.bin")
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}A simple REST controller provides two endpoints: /getUser returns a list of names, and /getWord returns a message based on the supplied word parameter. The controller code is:
@RestController
@SpringBootApplication
public class ControllerText {
@RequestMapping("getUser")
public Map<String, Object> getUser(){
System.out.println("微信小程序正在调用。。。");
Map<String, Object> map = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("zhangsan");
list.add("lisi");
list.add("wanger");
list.add("mazi");
map.put("list",list);
System.out.println("微信小程序调用完成。。。");
return map;
}
@RequestMapping("getWord")
public Map<String, Object> getText(String word){
Map<String, Object> map = new HashMap<>();
String message = "我能力有限,不要为难我";
if ("后来".equals(word)) {
message = "正在热映的后来的我们是刘若英的处女作。";
} else if ("微信小程序".equals(word)) {
message = "想获取更多微信小程序相关知识,请更多的阅读微信官方文档,还有其他更多微信开发相关的内容,学无止境。";
} else if ("西安工业大学".equals(word)) {
message = "西安工业大学(Xi'an Technological University)简称…"; // truncated for brevity
}
map.put("message", message);
return map;
}
@RequestMapping("")
public String getText(){
return "hello world";
}
}Mini‑program side : The .wxml defines a button and a view to display the returned list. The .js uses wx.request to call http://localhost:443/getUser and populates the page data. A second request sends a word parameter to /getWord and shows the returned message.
// Example request in mini‑program
wx.request({
url: 'http://localhost:443/getUser',
method: 'GET',
header: {'content-type': 'application/json'},
success: function(res){
var list = res.data.list;
if (list == null) {
wx.showToast({title: '数据获取失败', icon: '', duration: 2000});
} else {
that.setData({list: list});
}
}
});After implementing both sides, the mini‑program can successfully retrieve data from the Spring Boot API.
Deployment : The author recommends building a runnable JAR and deploying it on an Alibaba Cloud lightweight server with JDK installed. The command used is nohup java -jar helloworld.jar &, which keeps the service running in the background.
Finally, a Baidu Cloud link is provided for the source code, and the author invites readers to like and share the post.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
