Spring Boot HTTP over JSON 的错误码异常处理

This commit is contained in:
JeffLi1993
2017-03-14 11:48:12 +08:00
committed by liqiangqiang
parent e640e60da4
commit 5346d02ac2
8 changed files with 178 additions and 1 deletions

View File

@@ -0,0 +1,20 @@
package org.spring.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 16/4/26.
*/
// Spring Boot 应用的标识
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(Application.class,args);
}
}

View File

@@ -0,0 +1,28 @@
package org.spring.springboot.constant;
import org.spring.springboot.result.base.ErrorInfoInterface;
/**
* Created by bysocket on 14/03/2017.
*/
public enum CityErrorInfoEnum implements ErrorInfoInterface {
PARAMS_NO_COMPLETE("000001","params no complete"),
CITY_EXIT("000002","city exit");
private String code;
private String message;
CityErrorInfoEnum(String code, String message) {
this.code = code;
this.message = message;
}
public String getCode(){
return this.code;
}
public String getMessage(){
return this.message;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,11 @@
package org.spring.springboot.result.base;
/**
* 错误码接口
*
* Created by bysocket on 13/03/2017.
*/
public interface ErrorInfoInterface {
String getCode();
String getMessage();
}

View File

@@ -0,0 +1,19 @@
package org.spring.springboot.web;
import org.spring.springboot.constant.CityErrorInfoEnum;
import org.spring.springboot.result.GlobalErrorInfoException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* Created by bysocket on 16/4/26.
*/
@RestController
public class ErrorJsonController {
@RequestMapping("/param")
public String errorJsonParams() throws GlobalErrorInfoException {
throw new GlobalErrorInfoException(CityErrorInfoEnum.PARAMS_NO_COMPLETE);
}
}