思考:
第一次遇到这种情况:想的是配置nginx的形式,每生成一次二维码就linux自动映射一次路径
但是我复制呢多个问卷星的考试连接对比发现:
相同类型的题路径是一样的,唯一不一样的地方是路径后缀 {id} 里面的值
复制的路径对比如下:
https://ks.wjx.top/vj/YHySy1c.aspx
https://ks.wjx.top/vj/hljVcYb.aspx
可以看出如果路径是: https://ks.wjx.top/vj/{id} 是不是就符合我们的猜想了呢?
验证假设:show me code 模式
- 因为路径关联到数据库的考试的题型,所以 数据库表肯定有字段对应{id} 生成的时候,更新数据库,访问的时候查询数据库,进行对应的展示,保证id在数据库唯一,我们可以采用密码生成器来做这个事情,集成密码生成器工具类 找个工具包可以,也可以自己写个实现类。参考的代码如下:
- 引用某位回答的,简单但是有些小问题,自己改改就可以了!
-
public class RandomPassword { /** * 密码的长度 */ private static int passwordLength; /** * 生成的密码 */ private static StringBuffer password = new StringBuffer(); public static void main(String[] args) { //初始化数字英文和符号 String num = "0123456789"; String english = "qwertyuiopasdfghjklzxcvbnm"; String englishBig = "QWERTYUIOPASDFGHJKLZXCVBNM"; String symbol = "!@#$%^&*_+-{}<>.*"; String stringSum = num + english + englishBig + symbol; int length = stringSum.length(); //定义密码长度,写死8 passwordLength = 8; for (int i = 0; i < passwordLength; i++) { Random random = new Random(); int a = random.nextInt(length + 1); char one = stringSum.charAt(a); password.append(one); } System.out.print(password + " "); } }
- 二维码生成ZXing,当然也可以用hutool,hutool也是基于zxing
- 如果对@PathVariable 映射 URL 绑定的占位符 不了解的请自行了解
- 基本用法示例如下;
-
@RequestMapping("/getUserById/{name}") public User getUser(@PathVariable("name") String name){ return userService.selectUser(name); }
假设每个人在问卷星上复制或者分享一套试卷的次数小于100次,按理说这个应该够了,如果密码生成器生成8位,10000次的复制和类似的操作,应该也可以满足的。
搭建基于spring boot的基本项目环境,包括lombok mybatis 数据库,postman进行测试
有两个基本的接口:
代码如下:
package com.example.demo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @Description: 用于生成二维码和简单访问对应的链接
* @author: LiuYC
* @date: 2021年04月21日 15:25
*/
@RestController
public class QrcodeController {
@Resource
ExamMapper examMapper;
@RequestMapping("/createQrcode/{name}")
public String createQrcode(@PathVariable("name") String name) {
System.out.println("输出的name:");
System.out.println(name);
// 工具类生成:
String s = PswUtil.PswUtils();
String http = "http://localhost:19999/getExam/";
// 更新数据库
Exam exam = new Exam();
exam.setA("a");
exam.setExamId(s);
exam.setId("3");
exam.setHead("题目是test");
exam.setContent("内容是:如何进行测试方案的编写");
examMapper.insert(exam);
System.out.println("插入数据库完成:");
// 返回值到前端
return http + s;
}
@RequestMapping("/getExam/{name}")
public Exam getExam(@PathVariable("name") String name) {
ExamExample examExample = new ExamExample();
examExample.createCriteria().andExamIdEqualTo(name);
List<Exam> exams = examMapper.selectByExample(examExample);
if (exams.isEmpty()) {
return null;
}
return exams.get(0);
}
}
完整的整个项目的代码配置请访问个人gitee获取:
https://gitee.com/daka1004/demo
1.生成二维码(链接)的接口
2.访问考试接口的链接
今天就到这里了,各位看官三连烦请。