校验 , 全局异常处理

This commit is contained in:
2026-10-28 10:11:55 +08:00
parent 0b97e86d59
commit f58eafd10a
8 changed files with 170 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.linlangjz</groupId>
<artifactId>SpringStudy</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>Spring-ch12-verify</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,22 @@
package com.ycjy;
import com.ycjy.config.AppConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MyInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class); // 登记表
context.setServletContext(servletContext);
context.refresh();
servletContext
.addServlet("dispatcherServlet", new DispatcherServlet(context))
.addMapping("/");
}
}
@@ -0,0 +1,11 @@
package com.ycjy.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan({"com.ycjy.web","com.ycjy.exception"})
public class AppConfig {
}
@@ -0,0 +1,42 @@
package com.ycjy.dao;
import jakarta.validation.constraints.*;
public class User {
@NotNull(message = "id not null")
@Min(value = 1, message = "min value 1") // 最小值(包含)
@Max(value = 5,message = "max value 5") // 最大值(包含)
private Integer id;
@NotBlank(message = "name not null")
@NotBlank // 非空
@Size(min = 1, max = 5, message = "name long 1~5") // 长度区间(包含)
private String name;
@Min(value = 1, message = "age notmin 0")
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@@ -0,0 +1,9 @@
注解 用于 含义
@NotNull 任意 不能为 null
@NotBlank String 不能为 null、不能为空串、不能全是空格
@NotEmpty String、集合、数组 不能为 null、不能为空
@Size(min, max) String、集合、数组 长度/大小范围
@Min(value) 数字 最小值
@Max(value) 数字 最大值
@Email String 邮箱格式
@Pattern(regexp) String 正则匹配
@@ -0,0 +1,17 @@
package com.ycjy.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice // 全局控制器增强
public class GlobalExceptionHandler extends RuntimeException {
@ExceptionHandler(Exception.class)
public void getAllExceptions(Exception e) {
System.out.println("We have detected an anomaly: " + e.getMessage());
}
@ExceptionHandler(value = ArithmeticException.class)
public void getByZeroExceptions(Exception e) {
System.out.println("byZero: " + e.getMessage());
}
}
@@ -0,0 +1,26 @@
package com.ycjy.web;
import com.ycjy.dao.User;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ApiController {
@RequestMapping("/m1")
public void getDate(@Valid User user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
bindingResult.getAllErrors().forEach(
e -> System.out.println("错误信息: " + e.getDefaultMessage())
);
return;
}
System.out.println("id: " + user.getId());
System.out.println("name: " + user.getName());
System.out.println("age: " + user.getAge());
System.out.println(5/0);
}
}
+8
View File
@@ -20,6 +20,7 @@
<module>Spring-ch10-ssm-backend</module>
<module>Spring-RequestMapping</module>
<module>Spring-ch11-getData</module>
<module>Spring-ch12-verify</module>
</modules>
<properties>
@@ -110,6 +111,13 @@
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- 校验 -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>9.1.3.Final</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>