mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 22:23:54 +08:00
Spring Boot HTTP over JSON 的错误码异常处理
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package org.spring.springboot.result;
|
||||
|
||||
import org.spring.springboot.result.base.ErrorInfoInterface;
|
||||
|
||||
/**
|
||||
* Created by bysocket on 14/03/2017.
|
||||
*/
|
||||
public class GlobalErrorInfoException extends Exception {
|
||||
|
||||
private ErrorInfoInterface errorInfo;
|
||||
|
||||
public GlobalErrorInfoException (ErrorInfoInterface errorInfo) {
|
||||
this.errorInfo = errorInfo;
|
||||
}
|
||||
|
||||
public ErrorInfoInterface getErrorInfo() {
|
||||
return errorInfo;
|
||||
}
|
||||
|
||||
public void setErrorInfo(ErrorInfoInterface errorInfo) {
|
||||
this.errorInfo = errorInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.spring.springboot.result;
|
||||
|
||||
import org.spring.springboot.result.base.ErrorInfoInterface;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Created by bysocket on 14/03/2017.
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class GlobalErrorInfoHandler {
|
||||
|
||||
@ExceptionHandler(value = GlobalErrorInfoException.class)
|
||||
public ResultBody errorHandlerOverJson(HttpServletRequest request,
|
||||
GlobalErrorInfoException exception) {
|
||||
ErrorInfoInterface errorInfo = exception.getErrorInfo();
|
||||
ResultBody result = new ResultBody(errorInfo);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.spring.springboot.result;
|
||||
|
||||
import org.spring.springboot.result.base.ErrorInfoInterface;
|
||||
|
||||
/**
|
||||
* Created by bysocket on 14/03/2017.
|
||||
*/
|
||||
public class ResultBody<T> {
|
||||
/**
|
||||
* 响应代码
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 响应消息
|
||||
*/
|
||||
private String message;
|
||||
|
||||
/**
|
||||
* 响应结果
|
||||
*/
|
||||
private T result;
|
||||
|
||||
public ResultBody(ErrorInfoInterface errorInfo) {
|
||||
this.code = errorInfo.getCode();
|
||||
this.message = errorInfo.getMessage();
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public T getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(T result) {
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.spring.springboot.result.base;
|
||||
|
||||
/**
|
||||
* 错误码接口
|
||||
*
|
||||
* Created by bysocket on 13/03/2017.
|
||||
*/
|
||||
public interface ErrorInfoInterface {
|
||||
String getCode();
|
||||
String getMessage();
|
||||
}
|
||||
Reference in New Issue
Block a user