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:
@@ -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 {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.spring.springboot.result.base;
|
package org.spring.springboot.result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 错误码接口
|
* 错误码接口
|
||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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> {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user