mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-29 00:42:50 +08:00
表单校验
This commit is contained in:
parent
c2aff11b22
commit
a9296cdfe2
1
.gitignore
vendored
1
.gitignore
vendored
@ -33,3 +33,4 @@ build/
|
||||
.vscode/
|
||||
|
||||
/src/main/resources/application-dev.properties
|
||||
/logs
|
5
pom.xml
5
pom.xml
@ -62,6 +62,11 @@
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.5.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -1,22 +1,42 @@
|
||||
package xyz.playedu.api.controller;
|
||||
|
||||
import org.springframework.web.HttpRequestHandler;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import xyz.playedu.api.exception.ServiceException;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ExceptionController {
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public JsonResponse<String> exceptionHandler(Exception e) {
|
||||
return JsonResponse.error("系统错误", 500);
|
||||
}
|
||||
// @ExceptionHandler(Exception.class)
|
||||
// public JsonResponse<String> exceptionHandler(Exception e) {
|
||||
// return JsonResponse.error("系统错误", 500);
|
||||
// }
|
||||
|
||||
@ExceptionHandler(ServiceException.class)
|
||||
public JsonResponse<String> serviceExceptionHandler(ServiceException e) {
|
||||
return JsonResponse.error(e.getMessage(), 1);
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||
public JsonResponse<String> serviceExceptionHandler(HttpMessageNotReadableException e) {
|
||||
return JsonResponse.error("参数为空", 406);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public JsonResponse<String> serviceExceptionHandler(MethodArgumentNotValidException e) {
|
||||
StringBuffer errorMsg = new StringBuffer();
|
||||
List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
|
||||
for (ObjectError tmpError : allErrors) {
|
||||
errorMsg.append(tmpError.getDefaultMessage()).append(",");
|
||||
}
|
||||
String msg = errorMsg.substring(0, errorMsg.length() - 1);
|
||||
return JsonResponse.error(msg, 406);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,14 +17,9 @@ public class AdminUserController {
|
||||
private AdminUserServiceImpl adminUserService;
|
||||
|
||||
@GetMapping("/admin/user/index")
|
||||
public JsonResponse<Object> List(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
|
||||
public JsonResponse<Object> List(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "10") Integer size) {
|
||||
PaginationResult<AdminUser> result = adminUserService.paginate(page, size, null);
|
||||
return JsonResponse.data(result);
|
||||
}
|
||||
|
||||
@GetMapping("/admmin/user/test")
|
||||
public void TestException() throws ServiceException {
|
||||
throw new RuntimeException("我是错误");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
package xyz.playedu.api.controller.admin;
|
||||
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import xyz.playedu.api.request.LoginRequest;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/admin/v1/auth")
|
||||
public class LoginController {
|
||||
|
||||
@PostMapping("/login")
|
||||
public JsonResponse<Object> login(@RequestBody @Validated LoginRequest loginRequest) {
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("email", loginRequest.email);
|
||||
map.put("password", loginRequest.password);
|
||||
return JsonResponse.data(map);
|
||||
}
|
||||
|
||||
@PostMapping("/logout")
|
||||
public JsonResponse<String> logout() {
|
||||
return JsonResponse.success("success");
|
||||
}
|
||||
|
||||
}
|
54
src/main/java/xyz/playedu/api/request/LoginRequest.java
Normal file
54
src/main/java/xyz/playedu/api/request/LoginRequest.java
Normal file
@ -0,0 +1,54 @@
|
||||
package xyz.playedu.api.request;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class LoginRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@NotNull(message = "请输入邮箱")
|
||||
public String email;
|
||||
|
||||
@NotNull(message = "请输入密码")
|
||||
public String password;
|
||||
|
||||
@NotNull(message = "请输入图形验证码")
|
||||
public String captchaValue;
|
||||
|
||||
@NotNull(message = "captchaKey参数为空")
|
||||
public String captchaKey;
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getCaptchaValue() {
|
||||
return captchaValue;
|
||||
}
|
||||
|
||||
public void setCaptchaValue(String captchaValue) {
|
||||
this.captchaValue = captchaValue;
|
||||
}
|
||||
|
||||
public String getCaptchaKey() {
|
||||
return captchaKey;
|
||||
}
|
||||
|
||||
public void setCaptchaKey(String captchaKey) {
|
||||
this.captchaKey = captchaKey;
|
||||
}
|
||||
}
|
34
src/main/java/xyz/playedu/api/request/PaginationRequest.java
Normal file
34
src/main/java/xyz/playedu/api/request/PaginationRequest.java
Normal file
@ -0,0 +1,34 @@
|
||||
package xyz.playedu.api.request;
|
||||
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class PaginationRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Min(value = 1, message = "page参数值不能少于1")
|
||||
public Integer page = 1;
|
||||
|
||||
@Min(value = 1, message = "size参数值不能少于1")
|
||||
@Max(value = 1000, message = "size参数值不能超过1000")
|
||||
public Integer size = 10;
|
||||
|
||||
public Integer getPage() {
|
||||
return page;
|
||||
}
|
||||
|
||||
public void setPage(Integer page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
public Integer getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(Integer size) {
|
||||
this.size = size;
|
||||
}
|
||||
}
|
@ -1,5 +1,9 @@
|
||||
# ????
|
||||
spring.profiles.active=dev
|
||||
|
||||
#mysql
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
|
||||
#mybatis
|
||||
mybatis.mapper-locations=classpath:mapper/*.xml
|
||||
|
||||
|
93
src/main/resources/logback-spring.xml
Normal file
93
src/main/resources/logback-spring.xml
Normal file
@ -0,0 +1,93 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="60 seconds">
|
||||
|
||||
<springProperty scope="context" name="spring.application.name" source="spring.application.name"/>
|
||||
|
||||
<!-- 定义参数 -->
|
||||
<property name="log.lever" value="debug"/>
|
||||
<property name="log.maxHistory" value="365"/>
|
||||
<property name="log.filePath" value="logs"></property>
|
||||
<property name="log.pattern" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n"/>
|
||||
|
||||
<!-- 控制台设置 -->
|
||||
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<!--<pattern>%d %p (%file:%line\)- %m%n</pattern>-->
|
||||
<!--格式化输出:%d:表示日期 %thread:表示线程名 %-5level:级别从左显示5个字符宽度 %msg:日志消息 %n:是换行符-->
|
||||
<pattern>%red(%d{yyyy-MM-dd HH:mm:ss}) - %green([%thread]) - %highlight(%-5level) - %yellow(%logger) - %cyan(%msg%n)
|
||||
</pattern>
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- DEBUG -->
|
||||
<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<!-- 文件路径 -->
|
||||
<file>${log.filePath}/${spring.application.name}_debug.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 文件名称 -->
|
||||
<fileNamePattern>${log.filePath}/debug/${spring.application.name}_debug.%d{yyyy-MM-dd}.log.gz
|
||||
</fileNamePattern>
|
||||
<!-- 文件最大保存历史数量 -->
|
||||
<MaxHistory>${log.maxHistory}</MaxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>DEBUG</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- INFO -->
|
||||
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<!-- 文件路径 -->
|
||||
<file>${log.filePath}/${spring.application.name}_info.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 文件名称 -->
|
||||
<fileNamePattern>${log.filePath}/info/${spring.application.name}_info.%d{yyyy-MM-dd}.log.gz
|
||||
</fileNamePattern>
|
||||
<!-- 文件最大保存历史数量 -->
|
||||
<MaxHistory>${log.maxHistory}</MaxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>INFO</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- ERROR -->
|
||||
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<!-- 文件路径 -->
|
||||
<file>${log.filePath}/${spring.application.name}_error.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 文件名称 -->
|
||||
<fileNamePattern>${log.filePath}/error/${spring.application.name}_error.%d{yyyy-MM-dd}.log.gz
|
||||
</fileNamePattern>
|
||||
<!-- 文件最大保存历史数量 -->
|
||||
<MaxHistory>${log.maxHistory}</MaxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>ERROR</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- 上线后如果要查看错误日志,可以把level=info改为level=debug -->
|
||||
<root level="info">
|
||||
<appender-ref ref="consoleAppender"/>
|
||||
<appender-ref ref="debugAppender"/>
|
||||
<appender-ref ref="infoAppender"/>
|
||||
<appender-ref ref="errorAppender"/>
|
||||
</root>
|
||||
</configuration>
|
Loading…
x
Reference in New Issue
Block a user