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

This commit is contained in:
JeffLi1993
2017-03-14 11:52:14 +08:00
committed by liqiangqiang
parent 5346d02ac2
commit 07ffcd1a4d
6 changed files with 17 additions and 7 deletions

View File

@@ -1,8 +1,10 @@
package org.spring.springboot.constant; package org.spring.springboot.constant;
import org.spring.springboot.result.base.ErrorInfoInterface; import org.spring.springboot.result.ErrorInfoInterface;
/** /**
* 业务错误码 案例
*
* Created by bysocket on 14/03/2017. * Created by bysocket on 14/03/2017.
*/ */
public enum CityErrorInfoEnum implements ErrorInfoInterface { public enum CityErrorInfoEnum implements ErrorInfoInterface {

View File

@@ -1,4 +1,4 @@
package org.spring.springboot.result.base; package org.spring.springboot.result;
/** /**
* 错误码接口 * 错误码接口

View File

@@ -1,8 +1,8 @@
package org.spring.springboot.result; package org.spring.springboot.result;
import org.spring.springboot.result.base.ErrorInfoInterface;
/** /**
* 统一错误码异常
*
* Created by bysocket on 14/03/2017. * Created by bysocket on 14/03/2017.
*/ */
public class GlobalErrorInfoException extends Exception { public class GlobalErrorInfoException extends Exception {

View File

@@ -1,12 +1,13 @@
package org.spring.springboot.result; 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.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
/** /**
* 统一错误码异常处理
*
* Created by bysocket on 14/03/2017. * Created by bysocket on 14/03/2017.
*/ */
@RestControllerAdvice @RestControllerAdvice

View File

@@ -1,8 +1,8 @@
package org.spring.springboot.result; package org.spring.springboot.result;
import org.spring.springboot.result.base.ErrorInfoInterface;
/** /**
* 返回体
*
* Created by bysocket on 14/03/2017. * Created by bysocket on 14/03/2017.
*/ */
public class ResultBody<T> { public class ResultBody<T> {

View File

@@ -6,12 +6,19 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
* 错误码案例
* *
* Created by bysocket on 16/4/26. * Created by bysocket on 16/4/26.
*/ */
@RestController @RestController
public class ErrorJsonController { public class ErrorJsonController {
/**
* 模拟入参不完整案例
*
* @return
* @throws GlobalErrorInfoException
*/
@RequestMapping("/param") @RequestMapping("/param")
public String errorJsonParams() throws GlobalErrorInfoException { public String errorJsonParams() throws GlobalErrorInfoException {
throw new GlobalErrorInfoException(CityErrorInfoEnum.PARAMS_NO_COMPLETE); throw new GlobalErrorInfoException(CityErrorInfoEnum.PARAMS_NO_COMPLETE);