mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Spring Boot HTTP over JSON 的错误码异常处理
This commit is contained in:
4
pom.xml
4
pom.xml
@@ -15,6 +15,8 @@
|
||||
<module>springboot-freemarker</module>
|
||||
<module>springboot-dubbo-server</module>
|
||||
<module>springboot-dubbo-client</module>
|
||||
<module>springboot-redis</module>
|
||||
<module>springboot-validation-over-json</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user