forked from xuwujing/springBoot-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
628 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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> | ||
|
||
<groupId>springboot-exceptionHandler</groupId> | ||
<artifactId>springboot-exceptionHandler</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.17.RELEASE</version> | ||
<relativePath /> | ||
</parent> | ||
<dependencies> | ||
<!-- Spring Boot Web 依赖 核心 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<!-- Spring Boot Test 依赖 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<version>1.2.41</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
77 changes: 77 additions & 0 deletions
77
springboot-exceptionHandler/springboot-exceptionHandler.iml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="FacetManager"> | ||
<facet type="Spring" name="Spring"> | ||
<configuration /> | ||
</facet> | ||
<facet type="web" name="Web"> | ||
<configuration> | ||
<webroots /> | ||
<sourceRoots> | ||
<root url="file://$MODULE_DIR$/src/main/java" /> | ||
<root url="file://$MODULE_DIR$/src/main/resources" /> | ||
</sourceRoots> | ||
</configuration> | ||
</facet> | ||
</component> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:1.5.17.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:1.5.17.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:1.5.17.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:1.5.17.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:1.5.17.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.1.11" level="project" /> | ||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.1.11" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:jcl-over-slf4j:1.7.25" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.25" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:log4j-over-slf4j:1.7.25" level="project" /> | ||
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.17" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:1.5.17.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:8.5.34" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.tomcat:tomcat-annotations-api:8.5.34" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:8.5.34" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:8.5.34" level="project" /> | ||
<orderEntry type="library" name="Maven: org.hibernate:hibernate-validator:5.3.6.Final" level="project" /> | ||
<orderEntry type="library" name="Maven: javax.validation:validation-api:1.1.0.Final" level="project" /> | ||
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.2.Final" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.3.4" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.11.2" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.11" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.3.20.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.20.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.3.20.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.20.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.3.20.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.3.20.RELEASE" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:1.5.17.RELEASE" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:1.5.17.RELEASE" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:1.5.17.RELEASE" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.2.0" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.2.1" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.1" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.3" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:2.6.0" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:1.10.19" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.1" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.4.0" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.3.20.RELEASE" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:4.3.20.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.41" level="project" /> | ||
</component> | ||
</module> |
25 changes: 25 additions & 0 deletions
25
springboot-exceptionHandler/src/main/java/com/pancm/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.pancm; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
|
||
/** | ||
* | ||
* @Title: App | ||
* @Description: | ||
* 主程序入口 | ||
* @Version:1.0.0 | ||
* @author pancm | ||
* @date 2018年1月9日 | ||
*/ | ||
|
||
@SpringBootApplication | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
SpringApplication.run(App.class, args); | ||
System.out.println("程序正在运行..."); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
springboot-exceptionHandler/src/main/java/com/pancm/enums/CommonEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.pancm.enums; | ||
|
||
import com.pancm.exception.BaseErrorInfoInterface; | ||
|
||
/** | ||
* | ||
* @Title: CommonEnum | ||
* @Description: 公用描述枚举类 | ||
* @Version:1.0.0 | ||
* @author pancm | ||
* @date 2018年6月25日 | ||
*/ | ||
public enum CommonEnum implements BaseErrorInfoInterface { | ||
// 数据操作错误定义 | ||
SUCCESS("200", "成功!"), | ||
BODY_NOT_MATCH("400","请求的数据格式不符!"), | ||
SIGNATURE_NOT_MATCH("401","请求的数字签名不匹配!"), | ||
NOT_FOUND("404", "未找到该资源!"), | ||
INTERNAL_SERVER_ERROR("500", "服务器内部错误!"), | ||
SERVER_BUSY("503","服务器正忙,请稍后再试!") | ||
; | ||
|
||
/** 错误码 */ | ||
private String resultCode; | ||
|
||
/** 错误描述 */ | ||
private String resultMsg; | ||
|
||
CommonEnum(String resultCode, String resultMsg) { | ||
this.resultCode = resultCode; | ||
this.resultMsg = resultMsg; | ||
} | ||
|
||
@Override | ||
public String getResultCode() { | ||
return resultCode; | ||
} | ||
|
||
@Override | ||
public String getResultMsg() { | ||
return resultMsg; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
springboot-exceptionHandler/src/main/java/com/pancm/exception/BaseErrorInfoInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.pancm.exception; | ||
|
||
/** | ||
* | ||
* @Title: BaseErrorInfoInterface | ||
* @Description: 基础接口 | ||
* 自定义的错误描述枚举类需实现该接口 | ||
* @Version:1.0.0 | ||
* @author pancm | ||
* @date 2018年6月25日 | ||
*/ | ||
public interface BaseErrorInfoInterface { | ||
/** 错误码*/ | ||
String getResultCode(); | ||
|
||
/** 错误描述*/ | ||
String getResultMsg(); | ||
} |
85 changes: 85 additions & 0 deletions
85
springboot-exceptionHandler/src/main/java/com/pancm/exception/BizException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.pancm.exception; | ||
|
||
/** | ||
* | ||
* @Title: BizException | ||
* @Description: 业务异常类 | ||
* @Version:1.0.0 | ||
* @author pancm | ||
* @date 2018年10月24日 | ||
*/ | ||
public class BizException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* 错误码 | ||
*/ | ||
protected String errorCode; | ||
/** | ||
* 错误信息 | ||
*/ | ||
protected String errorMsg; | ||
|
||
public BizException() { | ||
super(); | ||
} | ||
|
||
public BizException(BaseErrorInfoInterface errorInfoInterface) { | ||
super(errorInfoInterface.getResultCode()); | ||
this.errorCode = errorInfoInterface.getResultCode(); | ||
this.errorMsg = errorInfoInterface.getResultMsg(); | ||
} | ||
|
||
public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) { | ||
super(errorInfoInterface.getResultCode(), cause); | ||
this.errorCode = errorInfoInterface.getResultCode(); | ||
this.errorMsg = errorInfoInterface.getResultMsg(); | ||
} | ||
|
||
public BizException(String errorMsg) { | ||
super(errorMsg); | ||
this.errorMsg = errorMsg; | ||
} | ||
|
||
public BizException(String errorCode, String errorMsg) { | ||
super(errorCode); | ||
this.errorCode = errorCode; | ||
this.errorMsg = errorMsg; | ||
} | ||
|
||
public BizException(String errorCode, String errorMsg, Throwable cause) { | ||
super(errorCode, cause); | ||
this.errorCode = errorCode; | ||
this.errorMsg = errorMsg; | ||
} | ||
|
||
|
||
public String getErrorCode() { | ||
return errorCode; | ||
} | ||
|
||
public void setErrorCode(String errorCode) { | ||
this.errorCode = errorCode; | ||
} | ||
|
||
public String getErrorMsg() { | ||
return errorMsg; | ||
} | ||
|
||
public void setErrorMsg(String errorMsg) { | ||
this.errorMsg = errorMsg; | ||
} | ||
|
||
public String getMessage() { | ||
return errorMsg; | ||
} | ||
|
||
/** | ||
*/ | ||
@Override | ||
public Throwable fillInStackTrace() { | ||
return this; | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
springboot-exceptionHandler/src/main/java/com/pancm/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.pancm.exception; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import com.pancm.result.ResultBody; | ||
|
||
|
||
/** | ||
* | ||
* @Title: GlobalExceptionHandler | ||
* @Description: 全局异常处理 | ||
如果使用@RestControllerAdvice 注解 | ||
则会将返回的数据类型转换成JSON格式 | ||
* @Version:1.0.0 | ||
* @author pancm | ||
* @date 2018年10月24日 | ||
*/ | ||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
|
||
/** | ||
* 处理自定义的业务异常 | ||
* @param req | ||
* @param e | ||
* @return | ||
*/ | ||
@ExceptionHandler(value = BizException.class) | ||
@ResponseBody | ||
public ResultBody bizExceptionHandler(HttpServletRequest req, BizException e){ | ||
logger.error("发生业务异常!原因是:{}",e.getErrorMsg()); | ||
return ResultBody.error(e.getErrorCode(),e.getErrorMsg()); | ||
} | ||
|
||
|
||
/** | ||
* 处理其他异常 | ||
* @param req | ||
* @param e | ||
* @return | ||
*/ | ||
@ExceptionHandler(value =Exception.class) | ||
@ResponseBody | ||
public ResultBody exceptionHandler(HttpServletRequest req, Exception e){ | ||
logger.error("未知异常!原因是:",e); | ||
return ResultBody.error("未知异常!原因是:"+e.getMessage()); | ||
} | ||
|
||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
springboot-exceptionHandler/src/main/java/com/pancm/exception/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Title: package-info | ||
* Description: | ||
* 异常类处理 | ||
* Version:1.0.0 | ||
* @author pancm | ||
* @date 2018年3月21日 | ||
*/ | ||
package com.pancm.exception; |
Oops, something went wrong.