java 单机接口限流处理方案
278
2022-09-13
Java 在线考试云平台的实现
考试流程:
用户前台注册成为学生
管理员后台添加老师,系统将该用户角色上升为老师
老师登录,添加考试,添加题目,发布考试
考生登录前台参加考试,交卷
老师后台批改试卷,查看成绩
考试查看成绩
练习流程:
考生登录前台参加练习,练习完自动判分,记录错题
考生查看成绩,查看错题
用户登录操作业务:
@RestController
@Api(tags = "User APIs")
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
@ApiOperation("注册")
ResultVO
ResultVO
// 注册信息的完善,还有唯一性校验没(用户名、邮箱和手机号)已经在user表中通过unique来设置了
User user = userService.register(registerDTO);
if (user != null) {
// 注册成功
resultVO = new ResultVO<>(ResultEnum.REGISTER_SUCCESS.getCode(), ResultEnum.REGISTER_SUCCESS.getMessage(), user);
} else {
resultVO = new ResultVO<>(ResultEnum.REGISTER_FAILED.getCode(), ResultEnum.REGISTER_FAILED.getMessage(), null);
}
return resultVO;
}
@PostMapping("/login")
@ApiOperation("根据用户名或邮箱登录,登录成功返回token")
ResultVO
// 用户登录
ResultVO
String token = userService.login(loginQo);
if (token != null) {
// 登录成功
resultVO = new ResultVO<>(ResultEnum.LOGIN_SUCCESS.getCode(), ResultEnum.LOGIN_SUCCESS.getMessage(), token);
} else {
// 登录失败
resultVO = new ResultVO<>(ResultEnum.LOGIN_FAILED.getCode(), ResultEnum.LOGIN_FAILED.getMessage(), null);
}
return resultVO;
}
@GetMapping("/user-info")
@ApiOperation("获取用户信息")
ResultVO
String userId = (String) request.getAttribute("user_id");
UserVo userVo = userService.getUserInfo(userId);
return new ResultVO<>(ResultEnum.GET_INFO_SUCCESS.getCode(), ResultEnum.GET_INFO_SUCCESS.getMessage(), userVo);
}
@GetMapping("/info")
@ApiOperation("获取用户的详细信息,包括个人信息页面和操作权限")
ResultVO
System.out.println("进入/user/info的获取用户信息的接口");
String userId = (String) request.getAttribute("user_id");
UserInfoVo userInfoVo = userService.getInfo(userId);
return new ResultVO<>(ResultEnum.GET_INFO_SUCCESS.getCode(), ResultEnum.GET_INFO_SUCCESS.getMessage(), userInfoVo);
}
@GetMapping("/test")
@ApiOperation("测试接口")
String test(HttpServletRequest request) {
// 下面这两个属性都是登录拦截器从token中解析地,当用户名不对或者token过期时是走不到接口内的
String userId = (String) request.getAttribute("user_id");
String username = (String) request.getAttribute("username");
System.out.println("用户id:" + userId);
System.out.println("用户名:" + username);
return "用户id:" + userId + "\n用户名:" + username;
}
}
问题信息业务操作:
@RestController
@Api(tags = "Exam APIs")
@RequestMapping("/exam")
public class ExamController {
@Autowired
private ExamService examService;
@GetMapping("/question/all")
@ApiOperation("获取所有问题的列表")
ResultVO> getQuestionAll() {
ResultVO> resultVO;
try {
List
resultVO = new ResultVO<>(0, "获取全部问题列表成功", questionAll);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取全部问题列表失败", null);
}
return resultVO;
}
@PostMapping("/question/update")
@ApiOperation("更新问题")
ResultVO
// 完成问题的更新
System.out.println(questionVo);
try {
QuestionVo questionVoResult = examService.updateQuestion(questionVo);
return new ResultVO<>(0, "更新问题成功", questionVoResult);
} catch (Exception e) {
e.printStackTrace();
return new ResultVO<>(-1, "更新问题失败", null);
}
}
@PostMapping("/question/create")
@ApiOperation("创建问题")
ResultVO
QuestionCreateVo questionCreateVo = new QuestionCreateVo();
// 把能拷贝过来的属性都拷贝过来
BeanUtils.copyProperties(questionCreateSimplifyVo, questionCreateVo);
// 设置创建者信息
String userId = (String) request.getAttribute("user_id");
questionCreateVo.setQuestionCreatorId(userId);
System.out.println(questionCreateVo);
try {
examService.questionCreate(questionCreateVo);
return new ResultVO<>(0, "问题创建成功", null);
} catch (Exception e) {
e.printStackTrace();
return new ResultVO<>(-1, "创建问题失败", null);
}
}
@GetMapping("/question/selection")
@ApiOperation("获取问题分类的相关选项")
ResultVO
QuestionSelectionVo questionSelectionVo = examService.getSelections();
if (questionSelectionVo != null) {
return new ResultVO<>(0, "获取问题分类选项成功", questionSelectionVo);
} else {
return new ResultVO<>(-1, "获取问题分类选项失败", null);
}
}
@GetMapping("/question/detail/{id}")
@ApiOperation("根据问题的id获取问题的详细信息")
ResultVO
// 根据问题id获取问题的详细信息
System.out.println(id);
ResultVO
try {
QuestionDetailVo questionDetailVo = examService.getQuestionDetail(id);
resultVO = new ResultVO<>(0, "获取问题详情成功", questionDetailVo);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取问题详情失败", null);
}
return resultVO;
}
@GetMapping("/all")
@ApiOperation("获取全部考试的列表")
ResultVO> getExamAll() {
// 需要拼接前端需要的考试列表对象
ResultVO> resultVO;
try {
List
resultVO = new ResultVO<>(0, "获取全部考试的列表成功", examVos);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取全部考试的列表失败", null);
}
return resultVO;
}
@GetMapping("/question/type/list")
@ApiOperation("获取问题列表,按照单选、多选和判断题分类返回")
ResultVO
// 获取问题的分类列表
ResultVO
try {
ExamQuestionTypeVo examQuestionTypeVo = examService.getExamQuestionType();
resultVO = new ResultVO<>(0, "获取问题列表成功", examQuestionTypeVo);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取问题列表失败", null);
}
return resultVO;
}
@PostMapping("/create")
@ApiOperation("创建考试")
ResultVO
// 从前端传参数过来,在这里完成考试的入库
ResultVO
String userId = (String) request.getAttribute("user_id");
try {
Exam exam = examService.create(examCreateVo, userId);
resultVO = new ResultVO<>(0, "创建考试成功", exam);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "创建考试失败", null);
}
return resultVO;
}
@PostMapping("/update")
@ApiOperation("更新考试")
ResultVO
// 从前端传参数过来,在这里完成考试的入库
ResultVO
String userId = (String) request.getAttribute("user_id");
try {
Exam exam = examService.update(examVo, userId);
resultVO = new ResultVO<>(0, "更新考试成功", exam);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "更新考试失败", null);
}
return resultVO;
}
@GetMapping("/card/list")
@ApiOperation("获取考试列表,适配前端卡片列表")
ResultVO> getExamCardList() {
// 获取考试列表卡片
ResultVO> resultVO;
try {
List
resultVO = new ResultVO<>(0, "获取考试列表卡片成功", examCardVoList);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取考试列表卡片失败", null);
}
return resultVO;
}
@GetMapping("/detail/{id}")
@ApiOperation("根据考试的id,获取考试详情")
ResultVO
// 根据id获取考试详情
ResultVO
try {
ExamDetailVo examDetail = examService.getExamDetail(id);
resultVO = new ResultVO<>(0, "获取考试详情成功", examDetail);
} catch (Exception e) {
resultVO = new ResultVO<>(-1, "获取考试详情失败", null);
}
return resultVO;
}
@PostMapping("/finish/{examId}")
@ApiOperation("根据用户提交的答案对指定id的考试判分")
ResultVO
ResultVO
try {
// 拦截器里设置上的用户id
String userId = (String) request.getAttribute("user_id");
// 下面根据用户提交的信息进行判分,返回用户的得分情况
ExamRecord examRecord = examService.judge(userId, examId, answersMap);
resultVO = new ResultVO<>(0, "考卷提交成功", examRecord);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "考卷提交失败", null);
}
return resultVO;
}
@GetMapping("/record/list")
@ApiOperation("获取当前用户的考试记录")
ResultVO> getExamRecordList(HttpServletRequest request) {
ResultVO> resultVO;
try {
// 拦截器里设置上的用户id
String userId = (String) request.getAttribute("user_id");
// 下面根据用户账号拿到他(她所有的考试信息),注意要用VO封装下
List
resultVO = new ResultVO<>(0, "获取考试记录成功", examRecordVoList);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取考试记录失败", null);
}
return resultVO;
}
@GetMapping("/record/detail/{recordId}")
@ApiOperation("根据考试sEsNBkUl记录id获取考试记录详情")
ResultVO
ResultVO
try {
RecordDetailVo recordDetailVo = examService.getRecordDetail(recordId);
resultVO = new ResultVO<>(0, "获取考试记录详情成功", recordDetailVo);
} catch (Exception e) {
e.printStackTrace();
resultVO = new ResultVO<>(-1, "获取考试记录详情失败", null);
}
return resultVO;
}
}
文件上传业务控制器:
/***********************************************************
* @note : 文件上传下载的接口,由于Swagger的问题导致在SwaggerUI
* 里测试不成功,实际上前端是完全没有问题的
* * 为了支持Ajax请求和响应,最简单的解决方案返回一个ResponseEntity。
* * 以下示例演示了上传文件的三种可能方式:
* * 1. 单文件上传 - `MultipartFile`
* * 2. 多文件上传 - `MultipartFile []`
* * 3. 将文件上传到模型 - `@ModelAttribute`
@RestController
@Api(tags = "Upload And Download APIs")
@RequestMapping("/file")
@Slf4j
public class UploadDownloadController {
// @Autowired
// AITestConfig aiTestConfig;
//
// @PostMapping("/upload/single")
// @ApiOperation("单文件上传")
// public String uploadFile(@RequestParam("file") MultipartFile uploadfile) {
// return FileTransUtil.uploadFile(uploadfile, "/root/" + File.separator + uploadfile.getOriginalFilename());
// }
@ApiOperation("单文件上传,支持同时传入参数")
@PostMapping("/api/upload/singleAndparas")
public String uploadFileSingle(@RequestParam("dir") String dir, @RequestParam("file") MultipartFile uploadfile) {
return FileTransUtil.uploadFile(uploadfile, dir);
}
@ApiOperation("单文件上传,支持同时传入参数,Model")
@PostMapping("/upload/single/model")
public String singleUploadFileModel(@ModelAttribute("model") UploadModel2 model) {
return FileTransUtil.uploadFile(model.getFile(), model.getDir());
}
@ApiOperation("多文件上传,支持同时传入参数")
@PostMapping("upload/multiAndparas")
public String uploadFileMulti(@RequestParam("dir") String dir, @RequestParam("files") MultipartFile[] uploadfiles) {
return FileTransUtil.uploadFiles(uploadfiles, dir);
}
@ApiOperation("多文件上传,支持同时传入参数")
@PostMapping(value = "/upload/multi/model")
public String multiUploadFileModel(@ModelAttribute(("model")) UploadModel model) {
return FileTransUtil.uploadFiles(model.getFiles(), model.getDir());
}
@ApiOperation("Get下载文件")
@GetMapping(value = "/download/get")
public ResponseEntity
return FileTransUtil.downloadFile(filePath);
}
@ApiOperation("Post下载文件")
@PostMapping(value = "/download/post")
public ResponseEntity
return FileTransUtil.downloadFile(downloadQo.getPath());
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~