表单校验

This commit is contained in:
none
2023-02-08 10:41:03 +08:00
parent c2aff11b22
commit a9296cdfe2
9 changed files with 325 additions and 89 deletions

View File

@@ -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);
}
}

View File

@@ -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("我是错误");
}
}

View File

@@ -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");
}
}