@@ -1,6 +1,6 @@ | |||||
# Spring Boot Demo | # Spring Boot Demo | ||||
spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actuator(监控)、admin(可视化监控)、logback(日志)、aopLog(通过 AOP 记录 web 请求日志)、JPA(ORM 框架)、mybatis(ORM 框架)、redis-cache(缓存)、swagger(API 接口管理测试)、ureport2(中国式报表)模块,后续会集成activemq,email, freemarker,shiro,websocket,quartz,netty等模块。 | |||||
spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actuator(监控)、admin(可视化监控)、logback(日志)、aopLog(通过 AOP 记录 web 请求日志)、统一异常处理( json 级别和页面级别)、JPA(ORM 框架)、mybatis(ORM 框架)、redis-cache(缓存)、swagger(API 接口管理测试)、ureport2(中国式报表)模块,后续会集成activemq,email, freemarker,shiro,websocket,quartz,netty等模块。 | |||||
依赖的 Spring Boot 版本: | 依赖的 Spring Boot 版本: | ||||
@@ -51,6 +51,7 @@ spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actu | |||||
<module>../spring-boot-demo-admin</module> | <module>../spring-boot-demo-admin</module> | ||||
<module>../spring-boot-demo-logback</module> | <module>../spring-boot-demo-logback</module> | ||||
<module>../spring-boot-demo-aoplog</module> | <module>../spring-boot-demo-aoplog</module> | ||||
<module>../spring-boot-demo-exceptionhandler</module> | |||||
<module>../spring-boot-demo-orm-jpa</module> | <module>../spring-boot-demo-orm-jpa</module> | ||||
<module>../spring-boot-demo-orm-mybatis</module> | <module>../spring-boot-demo-orm-mybatis</module> | ||||
<module>../spring-boot-demo-cache-redis</module> | <module>../spring-boot-demo-cache-redis</module> | ||||
@@ -148,6 +149,7 @@ spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actu | |||||
| [spring-boot-demo-admin](./spring-boot-demo-admin) | spring-boot 集成 spring-boot-admin 来可视化的监控 spring-boot 程序的运行状态,可以与 actuator 互相搭配使用 | | | [spring-boot-demo-admin](./spring-boot-demo-admin) | spring-boot 集成 spring-boot-admin 来可视化的监控 spring-boot 程序的运行状态,可以与 actuator 互相搭配使用 | | ||||
| [spring-boot-demo-logback](./spring-boot-demo-logback) | spring-boot 集成 logback 日志 | | | [spring-boot-demo-logback](./spring-boot-demo-logback) | spring-boot 集成 logback 日志 | | ||||
| [spring-boot-demo-aoplog](./spring-boot-demo-aoplog) | spring-boot 使用 AOP 切面的方式记录 web 请求日志 | | | [spring-boot-demo-aoplog](./spring-boot-demo-aoplog) | spring-boot 使用 AOP 切面的方式记录 web 请求日志 | | ||||
| [spring-boot-demo-exceptionhandler](./spring-boot-demo-exceptionhandler) | spring-boot 统一异常处理,包括2种,一种返回统一的 json 格式,第二种统一跳转到异常页面 | | |||||
| [spring-boot-demo-orm-jpa](./spring-boot-demo-orm-jpa) | spring-boot 集成 spring-boot-starter-data-jpa 操作数据库 | | | [spring-boot-demo-orm-jpa](./spring-boot-demo-orm-jpa) | spring-boot 集成 spring-boot-starter-data-jpa 操作数据库 | | ||||
| [spring-boot-demo-orm-mybatis](./spring-boot-demo-orm-mybatis) | spring-boot 集成 [mybatis-spring-boot-starter](https://github.com/mybatis/spring-boot-starter)、[mybatis-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) | | | [spring-boot-demo-orm-mybatis](./spring-boot-demo-orm-mybatis) | spring-boot 集成 [mybatis-spring-boot-starter](https://github.com/mybatis/spring-boot-starter)、[mybatis-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) | | ||||
| [spring-boot-demo-cache-redis](./spring-boot-demo-cache-redis) | spring-boot 使用 Redis 做缓存 | | | [spring-boot-demo-cache-redis](./spring-boot-demo-cache-redis) | spring-boot 使用 Redis 做缓存 | | ||||
@@ -1,14 +1,14 @@ | |||||
# spring-boot-demo 项目 | # spring-boot-demo 项目 | ||||
## 模块计划 | |||||
## 模块计划(已完成:12 / 35) | |||||
- [x] ~~spring-boot-demo-helloworld(Helloworld 示例)~~ | - [x] ~~spring-boot-demo-helloworld(Helloworld 示例)~~ | ||||
- [x] ~~spring-boot-demo-properties(读取配置文件信息)~~ | - [x] ~~spring-boot-demo-properties(读取配置文件信息)~~ | ||||
- [x] ~~spring-boot-demo-actuator(对 Spring boot 的端点监控)~~ | - [x] ~~spring-boot-demo-actuator(对 Spring boot 的端点监控)~~ | ||||
- [x] ~~spring-boot-demo-admin(对 Spring boot 可视化管控)~~ | - [x] ~~spring-boot-demo-admin(对 Spring boot 可视化管控)~~ | ||||
- [x] ~~spring-boot-demo-logback(集成 logback 日志)~~ | - [x] ~~spring-boot-demo-logback(集成 logback 日志)~~ | ||||
- [x] spring-boot-demo-aoplog(使用 AOP 拦截请求日志信息) | |||||
- [ ] spring-boot-demo-exceptionHandler(统一异常处理) | |||||
- [x] ~~spring-boot-demo-aoplog(使用 AOP 拦截请求日志信息)~~ | |||||
- [x] ~~spring-boot-demo-exceptionhandler(统一异常处理)~~ | |||||
- [ ] spring-boot-demo-orm-jdbcTemplate(操作 SQL 关系型数据库 - JdbcTemplate) | - [ ] spring-boot-demo-orm-jdbcTemplate(操作 SQL 关系型数据库 - JdbcTemplate) | ||||
- [x] ~~spring-boot-demo-orm-jpa(操作 SQL 关系型数据库 - JPA)~~ | - [x] ~~spring-boot-demo-orm-jpa(操作 SQL 关系型数据库 - JPA)~~ | ||||
- [x] ~~spring-boot-demo-orm-mybatis(操作 SQL 关系型数据库 - mybatis)~~ | - [x] ~~spring-boot-demo-orm-mybatis(操作 SQL 关系型数据库 - mybatis)~~ | ||||
@@ -12,7 +12,7 @@ | |||||
<artifactId>spring-boot-demo-aoplog</artifactId> | <artifactId>spring-boot-demo-aoplog</artifactId> | ||||
<version>0.0.1-SNAPSHOT</version> | <version>0.0.1-SNAPSHOT</version> | ||||
<packaging>jar</packaging> | |||||
<packaging>war</packaging> | |||||
<name>spring-boot-demo-aoplog</name> | <name>spring-boot-demo-aoplog</name> | ||||
<description>Demo project for Spring Boot</description> | <description>Demo project for Spring Boot</description> | ||||
@@ -5,7 +5,7 @@ | |||||
<artifactId>spring-boot-demo-aoplog</artifactId> | <artifactId>spring-boot-demo-aoplog</artifactId> | ||||
<version>0.0.1-SNAPSHOT</version> | <version>0.0.1-SNAPSHOT</version> | ||||
<packaging>jar</packaging> | |||||
<packaging>war</packaging> | |||||
<name>spring-boot-demo-aoplog</name> | <name>spring-boot-demo-aoplog</name> | ||||
<description>Demo project for Spring Boot</description> | <description>Demo project for Spring Boot</description> | ||||
@@ -0,0 +1,180 @@ | |||||
# spring-boot-demo-exceptionhandler | |||||
依赖[spring-boot-demo-parent](../spring-boot-demo-parent)、`spring-boot-starter-thymeleaf`(关于 thymeleaf 的会在后面的模板引擎专门有demo) | |||||
### pom.xml | |||||
```xml | |||||
<?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> | |||||
<artifactId>spring-boot-demo-exceptionhandler</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<packaging>war</packaging> | |||||
<name>spring-boot-demo-exceptionhandler</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo-parent</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath> | |||||
</parent> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-thymeleaf</artifactId> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-exceptionhandler</finalName> | |||||
</build> | |||||
</project> | |||||
``` | |||||
### application.yml | |||||
```yaml | |||||
server: | |||||
port: 8080 | |||||
context-path: /demo | |||||
spring: | |||||
thymeleaf: | |||||
mode: HTML5 | |||||
encoding: UTF-8 | |||||
content-type: text/html | |||||
cache: false | |||||
``` | |||||
### DemoExceptionHandler.java | |||||
```java | |||||
/** | |||||
* 自定义异常统一处理 | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler.handler | |||||
* @description: 自定义异常统一处理 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午1:37 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@ControllerAdvice | |||||
@Slf4j | |||||
public class DemoExceptionHandler { | |||||
public static final String DEFAULT_ERROR_VIEW = "error"; | |||||
/** | |||||
* 统一 json 异常处理 | |||||
* | |||||
* @param exception DemoJsonException | |||||
* @return 统一返回 json 格式 | |||||
*/ | |||||
@ExceptionHandler(value = DemoJsonException.class) | |||||
@ResponseBody | |||||
public R jsonErrorHandler(DemoJsonException exception) { | |||||
log.error("【DemoJsonException】:{}", exception.getMessage()); | |||||
return R.error(exception); | |||||
} | |||||
/** | |||||
* 统一 页面 异常处理 | |||||
* | |||||
* @param exception DemoPageException | |||||
* @return 统一跳转到异常页面 | |||||
*/ | |||||
@ExceptionHandler(value = DemoPageException.class) | |||||
public ModelAndView pageErrorHandler(DemoPageException exception) { | |||||
log.error("【DemoPageException】:{}", exception.getMessage()); | |||||
ModelAndView view = new ModelAndView(); | |||||
view.addObject("message", exception.getMessage()); | |||||
view.setViewName(DEFAULT_ERROR_VIEW); | |||||
return view; | |||||
} | |||||
} | |||||
``` | |||||
### R.java | |||||
```java | |||||
/** | |||||
* 统一返回的 json 对象 | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler | |||||
* @description: 统一返回的 json 对象 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午1:42 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Data | |||||
@Builder | |||||
@NoArgsConstructor | |||||
@AllArgsConstructor | |||||
public class R<T> { | |||||
private Integer code; | |||||
private String message; | |||||
private T data; | |||||
public R(Status status) { | |||||
this.code = status.getCode(); | |||||
this.message = status.getMessage(); | |||||
} | |||||
public static R success(Integer code, String message, Object data) { | |||||
return new R(code, message, data); | |||||
} | |||||
public static R success() { | |||||
return new R(Status.OK); | |||||
} | |||||
public static R success(String message) { | |||||
return success(message, null); | |||||
} | |||||
public static R success(String message, Object data) { | |||||
return success(Code.SUCCESS.getCode(), message, data); | |||||
} | |||||
public static R error(Integer code, String message, Object data) { | |||||
return new R(code, message, data); | |||||
} | |||||
public static R error(Integer code, String message) { | |||||
return error(code, message, null); | |||||
} | |||||
public static R error(DemoJsonException exception) { | |||||
return error(exception.getCode(), exception.getMessage()); | |||||
} | |||||
} | |||||
``` | |||||
### error.html (在目录 `resources/templates` 下) | |||||
```html | |||||
<!DOCTYPE html> | |||||
<html xmlns:th="http://www.thymeleaf.org"> | |||||
<head lang="en"> | |||||
<meta charset="UTF-8"/> | |||||
<title>统一页面异常处理</title> | |||||
</head> | |||||
<body> | |||||
<h1>统一页面异常处理</h1> | |||||
<div th:text="${message}"></div> | |||||
</body> | |||||
</html> | |||||
``` | |||||
### 其余代码 | |||||
详情请参见本demo。 |
@@ -0,0 +1,31 @@ | |||||
<?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> | |||||
<artifactId>spring-boot-demo-exceptionhandler</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<packaging>war</packaging> | |||||
<name>spring-boot-demo-exceptionhandler</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo-parent</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath> | |||||
</parent> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-thymeleaf</artifactId> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-exceptionhandler</finalName> | |||||
</build> | |||||
</project> |
@@ -0,0 +1,12 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler; | |||||
import org.springframework.boot.SpringApplication; | |||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||
@SpringBootApplication | |||||
public class SpringBootDemoExceptionhandlerApplication { | |||||
public static void main(String[] args) { | |||||
SpringApplication.run(SpringBootDemoExceptionhandlerApplication.class, args); | |||||
} | |||||
} |
@@ -0,0 +1,28 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler.controller; | |||||
import com.xkcoding.springbootdemoexceptionhandler.domain.R; | |||||
import com.xkcoding.springbootdemoexceptionhandler.exception.DemoJsonException; | |||||
import org.springframework.web.bind.annotation.GetMapping; | |||||
import org.springframework.web.bind.annotation.RequestMapping; | |||||
import org.springframework.web.bind.annotation.RestController; | |||||
/** | |||||
* 测试 json 异常处理 | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler.controller | |||||
* @description: 测试 json 异常处理 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午2:08 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@RestController | |||||
@RequestMapping("/json") | |||||
public class JsonController { | |||||
@GetMapping({"", "/"}) | |||||
public R index() { | |||||
throw new DemoJsonException(501, "DemoJsonException"); | |||||
} | |||||
} |
@@ -0,0 +1,28 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler.controller; | |||||
import com.xkcoding.springbootdemoexceptionhandler.exception.DemoPageException; | |||||
import org.springframework.stereotype.Controller; | |||||
import org.springframework.web.bind.annotation.GetMapping; | |||||
import org.springframework.web.bind.annotation.RequestMapping; | |||||
import org.springframework.web.servlet.ModelAndView; | |||||
/** | |||||
* TODO | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler.controller | |||||
* @description: TODO | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午2:29 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Controller | |||||
@RequestMapping("/page") | |||||
public class PageController { | |||||
@GetMapping({"", "/"}) | |||||
public ModelAndView index() { | |||||
throw new DemoPageException(600, "DemoPageException"); | |||||
} | |||||
} |
@@ -0,0 +1,63 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler.domain; | |||||
import com.xkcoding.springbootdemoexceptionhandler.enums.Code; | |||||
import com.xkcoding.springbootdemoexceptionhandler.enums.Status; | |||||
import com.xkcoding.springbootdemoexceptionhandler.exception.DemoJsonException; | |||||
import lombok.AllArgsConstructor; | |||||
import lombok.Builder; | |||||
import lombok.Data; | |||||
import lombok.NoArgsConstructor; | |||||
/** | |||||
* 统一返回的 json 对象 | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler | |||||
* @description: 统一返回的 json 对象 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午1:42 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Data | |||||
@Builder | |||||
@NoArgsConstructor | |||||
@AllArgsConstructor | |||||
public class R<T> { | |||||
private Integer code; | |||||
private String message; | |||||
private T data; | |||||
public R(Status status) { | |||||
this.code = status.getCode(); | |||||
this.message = status.getMessage(); | |||||
} | |||||
public static R success(Integer code, String message, Object data) { | |||||
return new R(code, message, data); | |||||
} | |||||
public static R success() { | |||||
return new R(Status.OK); | |||||
} | |||||
public static R success(String message) { | |||||
return success(message, null); | |||||
} | |||||
public static R success(String message, Object data) { | |||||
return success(Code.SUCCESS.getCode(), message, data); | |||||
} | |||||
public static R error(Integer code, String message, Object data) { | |||||
return new R(code, message, data); | |||||
} | |||||
public static R error(Integer code, String message) { | |||||
return error(code, message, null); | |||||
} | |||||
public static R error(DemoJsonException exception) { | |||||
return error(exception.getCode(), exception.getMessage()); | |||||
} | |||||
} |
@@ -0,0 +1,25 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler.enums; | |||||
import lombok.Getter; | |||||
/** | |||||
* 状态码 | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler.enums | |||||
* @description: 状态码 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午1:56 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Getter | |||||
public enum Code { | |||||
SUCCESS(200); | |||||
private Integer code; | |||||
Code(Integer code) { | |||||
this.code = code; | |||||
} | |||||
} |
@@ -0,0 +1,26 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler.enums; | |||||
import lombok.Getter; | |||||
/** | |||||
* 返回状态 | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler.enums | |||||
* @description: 返回状态 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午1:47 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Getter | |||||
public enum Status { | |||||
OK(200, "成功"), UNKNOW_ERROR(-1, "未知错误"); | |||||
private Integer code; | |||||
private String message; | |||||
Status(Integer code, String message) { | |||||
this.code = code; | |||||
this.message = message; | |||||
} | |||||
} |
@@ -0,0 +1,27 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler.exception; | |||||
import lombok.Getter; | |||||
/** | |||||
* 统一的 json 异常处理 | |||||
* <p> | |||||
* 要继承 RuntimeException | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler.exception | |||||
* @description: 统一的 json 异常处理 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午1:32 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Getter | |||||
public class DemoJsonException extends RuntimeException { | |||||
public Integer code; | |||||
public DemoJsonException(Integer code, String message) { | |||||
super(message); | |||||
this.code = code; | |||||
} | |||||
} |
@@ -0,0 +1,27 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler.exception; | |||||
import lombok.Getter; | |||||
/** | |||||
* 统一的页面异常处理 | |||||
* <p> | |||||
* 要继承 RuntimeException | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler.exception | |||||
* @description: 统一的页面异常处理 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午1:32 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Getter | |||||
public class DemoPageException extends RuntimeException { | |||||
public Integer code; | |||||
public DemoPageException(Integer code, String message) { | |||||
super(message); | |||||
this.code = code; | |||||
} | |||||
} |
@@ -0,0 +1,55 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler.handler; | |||||
import com.xkcoding.springbootdemoexceptionhandler.domain.R; | |||||
import com.xkcoding.springbootdemoexceptionhandler.exception.DemoJsonException; | |||||
import com.xkcoding.springbootdemoexceptionhandler.exception.DemoPageException; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
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.servlet.ModelAndView; | |||||
/** | |||||
* 自定义异常统一处理 | |||||
* | |||||
* @package: com.xkcoding.springbootdemoexceptionhandler.handler | |||||
* @description: 自定义异常统一处理 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2017/11/24 下午1:37 | |||||
* @copyright: Copyright (c) 2017 | |||||
* @version: 0.0.1 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@ControllerAdvice | |||||
@Slf4j | |||||
public class DemoExceptionHandler { | |||||
public static final String DEFAULT_ERROR_VIEW = "error"; | |||||
/** | |||||
* 统一 json 异常处理 | |||||
* | |||||
* @param exception DemoJsonException | |||||
* @return 统一返回 json 格式 | |||||
*/ | |||||
@ExceptionHandler(value = DemoJsonException.class) | |||||
@ResponseBody | |||||
public R jsonErrorHandler(DemoJsonException exception) { | |||||
log.error("【DemoJsonException】:{}", exception.getMessage()); | |||||
return R.error(exception); | |||||
} | |||||
/** | |||||
* 统一 页面 异常处理 | |||||
* | |||||
* @param exception DemoPageException | |||||
* @return 统一跳转到异常页面 | |||||
*/ | |||||
@ExceptionHandler(value = DemoPageException.class) | |||||
public ModelAndView pageErrorHandler(DemoPageException exception) { | |||||
log.error("【DemoPageException】:{}", exception.getMessage()); | |||||
ModelAndView view = new ModelAndView(); | |||||
view.addObject("message", exception.getMessage()); | |||||
view.setViewName(DEFAULT_ERROR_VIEW); | |||||
return view; | |||||
} | |||||
} |
@@ -0,0 +1,9 @@ | |||||
server: | |||||
port: 8080 | |||||
context-path: /demo | |||||
spring: | |||||
thymeleaf: | |||||
mode: HTML5 | |||||
encoding: UTF-8 | |||||
content-type: text/html | |||||
cache: false |
@@ -0,0 +1,11 @@ | |||||
<!DOCTYPE html> | |||||
<html xmlns:th="http://www.thymeleaf.org"> | |||||
<head lang="en"> | |||||
<meta charset="UTF-8"/> | |||||
<title>统一页面异常处理</title> | |||||
</head> | |||||
<body> | |||||
<h1>统一页面异常处理</h1> | |||||
<div th:text="${message}"></div> | |||||
</body> | |||||
</html> |
@@ -0,0 +1,16 @@ | |||||
package com.xkcoding.springbootdemoexceptionhandler; | |||||
import org.junit.Test; | |||||
import org.junit.runner.RunWith; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
import org.springframework.test.context.junit4.SpringRunner; | |||||
@RunWith(SpringRunner.class) | |||||
@SpringBootTest | |||||
public class SpringBootDemoExceptionhandlerApplicationTests { | |||||
@Test | |||||
public void contextLoads() { | |||||
} | |||||
} |
@@ -18,6 +18,7 @@ | |||||
<module>../spring-boot-demo-admin</module> | <module>../spring-boot-demo-admin</module> | ||||
<module>../spring-boot-demo-logback</module> | <module>../spring-boot-demo-logback</module> | ||||
<module>../spring-boot-demo-aoplog</module> | <module>../spring-boot-demo-aoplog</module> | ||||
<module>../spring-boot-demo-exceptionhandler</module> | |||||
<module>../spring-boot-demo-orm-jpa</module> | <module>../spring-boot-demo-orm-jpa</module> | ||||
<module>../spring-boot-demo-orm-mybatis</module> | <module>../spring-boot-demo-orm-mybatis</module> | ||||
<module>../spring-boot-demo-cache-redis</module> | <module>../spring-boot-demo-cache-redis</module> | ||||