本教程将帮助您公开 REST API 以使用 Spring Boot 框架生成二维码。 QRGen 库提供了一个 API 来生成 QR 码,我们之前的教程之一“如何在 Java 中生成 QR 码”对此进行了解释。
包 org.springframework.http
的 ResponseEntity<*T*>
将通过设置内容类型 MediaType.*IMAGE_JPEG*
或 MediaType.*IMAGE_PNG*
来帮助在 Web 浏览器上生成二维码图像。
您可以访问 Spring Boot-从数据库和类路径中显示图像,以了解有关如何使用 REST API 进行显示的更多信息。
查找此应用程序中使用的工具/技术/软件/库列表。
JDK 1.8
Spring Boot 2.3.4.RELEASE
QRGen library
Maven 3.2+
Your favorite IDE:
Spring Tool Suite (STS)
Eclipse
IntelliJ IDEA
这是 pom.xml 文件,包括此项目中使用的所需依赖项。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.websparrow</groupId>
<artifactId>spring-boot-qr-code</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-qr-code</name>
<properties>
<java.version>1.8</java.version>
</properties>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.kenglxn.qrgen</groupId>
<artifactId>javase</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建一个 QRCodeService
接口并声明 generate
方法,该方法接受 3 个参数,即 text、width、height,其返回类型为 byte []
。
text
是String 类型,扫描二维码时显示。width
是一个 Integer 类型,用于定义二维码的宽度。height
也是一个 Integer 类型,用于定义二维码的高度。package org.websparrow.service;
public interface QRCodeService {
byte[] generate(String text, int width, int height);
}
QRCodeServiceImpl
是 QRCodeService
接口的实现,它调用了 QRGen 库提供的 QRCode
类的静态方法 from(*text*)
和 withSize(*width*, *height*)
。
QRCodeServiceImpl.java
package org.websparrow.service;
import net.glxn.qrgen.javase.QRCode;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@Service
public class QRCodeServiceImpl implements QRCodeService {
@Override
public byte[] generate(String text, int width, int height) {
try (ByteArrayOutputStream bos = QRCode.from(text).withSize(width, height).stream(); ) {
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
QRCodeController
公开 REST 端点,供最终用户在 Web 浏览器上显示二维码。为了简单起见,我们定义了 QR text、width 和 height 的硬编码值。
QRCodeController.java
package org.websparrow.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.websparrow.service.QRCodeService;
@RestController
public class QRCodeController {
private final int WIDTH = 250;
private final int HEIGHT = 250;
private final String QR_TEXT = "Spring Boot REST API to generate QR Code - Websparrow.org";
@Autowired private QRCodeService qrCodeService;
@GetMapping("qr-code")
public ResponseEntity<byte[]> getQrCode() {
byte[] qrImage = qrCodeService.generate(QR_TEXT, WIDTH, HEIGHT);
return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(qrImage);
}
}
该类用于启动 Spring Boot 应用程序。
SpringBootQRCodeApp.java
package org.websparrow;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootQRCodeApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootQRCodeApp.class, args);
}
}
当应用程序成功启动时,在您喜欢的 Web 浏览器中点击以下 API。
API- http://localhost:8080/qr-code
您将找到您的二维码,如下所示:
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.websparrow.org/spring/generate-qr-code-using-spring-boot-rest-api
内容来源于网络,如有侵权,请联系作者删除!