1. 新建Maven项目 exception
2. pom.xml
<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> <groupId>com.java</groupId> <artifactId>exception</artifactId> <version>1.0.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
3. ExceptionStarter.java
package com.java; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ExceptionStarter { public static void main(String[] args) { SpringApplication.run(ExceptionStarter.class, args); } }
4. User.java
package com.java.vo; public class User { private String name; private Integer age; 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; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
5. TestException.java
package com.java.exception; public class TestException extends RuntimeException { private static final long serialVersionUID = -6025232952122532866L; private String username; public TestException(String username) { super("测试演习,用户名不存在"); this.username = username; } public String getUsername() { return username; } }
6. ControllerExceptionHandler.java
package com.java.handler; import java.util.HashMap; import java.util.Map; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import com.java.exception.TestException; @ControllerAdvice public class ControllerExceptionHandler { @ResponseBody @ExceptionHandler(TestException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public Map<String, Object> handlerTestException(TestException e) { Map<String, Object> result = new HashMap<>(); result.put("username", e.getUsername()); result.put("message", e.getMessage()); return result; } }
7. TestController.java
package com.java.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.java.exception.TestException; import com.java.vo.User; @RestController public class TestController { @GetMapping("/test") public User test(User user) { System.out.println(user); if ("test".equals(user.getName())) { throw new TestException(user.getName()); } return user; } }
8. 运行ExceptionStarter.java,启动测试
浏览器输入 http://localhost:8080/test?name=Logen&age=16
正常场景返回结果如下:
{"name":"Logen","age":16}
浏览器输入 http://localhost:8080/test?name=test&age=16
异常场景返回结果如下:
{"message":"测试演习,用户名不存在","username":"test"}
搭建完成,配置生效。
.
原文地址:https://www.cnblogs.com/jonban/p/exception.html
时间: 2024-10-09 04:11:33