Generate QR Codes in Spring Boot Using Hutool and ZXing
This guide shows how to generate standard and logo‑enhanced QR codes in a Spring Boot backend by adding Hutool and ZXing dependencies, configuring Maven, using QrCodeUtil, and optionally creating time‑limited QR links with MD5 signatures.
Introduction
QR codes are widely used for quick sharing and login. They can be generated on the frontend or backend, and this article focuses on backend generation with Spring Boot.
History of QR Codes
Barcodes were invented in the 1950s to speed up checkout, but their limited capacity led to the development of QR codes, which store more information.
Environment Setup
Add the following Maven dependencies to your pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.35</version>
</dependency>
</dependencies>Basic QR Code Generation
Use QrCodeUtil.generate with content, width, height, format, and output stream.
QrCodeUtil.generate(content, width, height, format, target);
Content: text, URL, etc.
Width & Height: image dimensions.
Format: image format such as png.
Target: output destination (file or stream).
Example controller:
@GetMapping("/hutool/qrcode")
public void generateQRCodeByHutool(@RequestParam String content,
HttpServletResponse response) throws Exception {
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
QrCodeUtil.generate(content, 300, 300, "png", os);
}QR Code with Logo
For richer QR codes, configure a QrConfig object to set logo, error correction, margin, and other properties.
QrCodeUtil.generate(content, config, "png", target);
Example:
@GetMapping("/hutool/qrcode/custom")
public void generateCustomQRCode(@RequestParam String content,
HttpServletResponse response) throws Exception {
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
QrConfig config = new QrConfig(500, 500);
config.setErrorCorrection(ErrorCorrectionLevel.L);
config.setImg("/home/seaua/Desktop/logo.png");
config.setRound(0.2);
config.setMargin(2);
QrCodeUtil.generate(content, config, "png", os);
}Creating Time‑Limited QR Links
To make QR codes expire, embed a timestamp and an MD5 signature in the URL, then validate them on the server.
public String generateQrCodeUrl(Long comboId, Long expireTime) {
String sign = getMd5Sign(comboId, expireTime);
return redirect + "?comboId=" + comboId + "&expire=" + expireTime + "&sign=" + sign;
}
public String getMd5Sign(Long comboId, Long expireTime) {
return MD5.encode(comboId + ":" + expireTime + ":" + secret);
}
public Long getExpire() {
long timeStamp = System.currentTimeMillis();
return timeStamp + expire * 1000;
}Conclusion
By adding Hutool and ZXing to a Spring Boot project, you can quickly generate plain or logo‑enhanced QR codes on the backend, and you can also produce expiring QR links by signing URLs with a timestamp.
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.
Dunmao Tech Hub
Sharing selected technical articles synced from CSDN. Follow us on CSDN: Dunmao.
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.
