mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 06:03:52 +08:00
Spring Boot HTTP over JSON 的错误码异常处理
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
package org.spring.springboot.result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 应用系统级别的错误码
|
||||||
|
*
|
||||||
|
* Created by bysocket on 14/03/2017.
|
||||||
|
*/
|
||||||
|
public enum GlobalErrorInfoEnum implements ErrorInfoInterface{
|
||||||
|
SUCCESS("0", "success"),
|
||||||
|
NOT_FOUND("-1", "service not found");
|
||||||
|
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
GlobalErrorInfoEnum(String code, String message) {
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCode(){
|
||||||
|
return this.code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage(){
|
||||||
|
return this.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ package org.spring.springboot.result;
|
|||||||
*
|
*
|
||||||
* Created by bysocket on 14/03/2017.
|
* Created by bysocket on 14/03/2017.
|
||||||
*/
|
*/
|
||||||
public class ResultBody<T> {
|
public class ResultBody {
|
||||||
/**
|
/**
|
||||||
* 响应代码
|
* 响应代码
|
||||||
*/
|
*/
|
||||||
@@ -19,13 +19,19 @@ public class ResultBody<T> {
|
|||||||
/**
|
/**
|
||||||
* 响应结果
|
* 响应结果
|
||||||
*/
|
*/
|
||||||
private T result;
|
private Object result;
|
||||||
|
|
||||||
public ResultBody(ErrorInfoInterface errorInfo) {
|
public ResultBody(ErrorInfoInterface errorInfo) {
|
||||||
this.code = errorInfo.getCode();
|
this.code = errorInfo.getCode();
|
||||||
this.message = errorInfo.getMessage();
|
this.message = errorInfo.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ResultBody(Object result) {
|
||||||
|
this.code = GlobalErrorInfoEnum.SUCCESS.getCode();
|
||||||
|
this.message = GlobalErrorInfoEnum.SUCCESS.getMessage();
|
||||||
|
this.result = result;
|
||||||
|
}
|
||||||
|
|
||||||
public String getCode() {
|
public String getCode() {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
@@ -42,11 +48,11 @@ public class ResultBody<T> {
|
|||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T getResult() {
|
public Object getResult() {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setResult(T result) {
|
public void setResult(Object result) {
|
||||||
this.result = result;
|
this.result = result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,11 @@ package org.spring.springboot.web;
|
|||||||
|
|
||||||
import org.spring.springboot.constant.CityErrorInfoEnum;
|
import org.spring.springboot.constant.CityErrorInfoEnum;
|
||||||
import org.spring.springboot.result.GlobalErrorInfoException;
|
import org.spring.springboot.result.GlobalErrorInfoException;
|
||||||
|
import org.spring.springboot.result.ResultBody;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,14 +17,20 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@RestController
|
@RestController
|
||||||
public class ErrorJsonController {
|
public class ErrorJsonController {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 模拟入参不完整案例
|
* 获取城市接口
|
||||||
*
|
*
|
||||||
|
* @param cityName
|
||||||
* @return
|
* @return
|
||||||
* @throws GlobalErrorInfoException
|
* @throws GlobalErrorInfoException
|
||||||
*/
|
*/
|
||||||
@RequestMapping("/param")
|
@RequestMapping(value = "/api/city", method = RequestMethod.GET)
|
||||||
public String errorJsonParams() throws GlobalErrorInfoException {
|
public ResultBody findOneCity(@RequestParam("cityName") String cityName) throws GlobalErrorInfoException {
|
||||||
|
// 入参为空
|
||||||
|
if (StringUtils.isEmpty(cityName)) {
|
||||||
throw new GlobalErrorInfoException(CityErrorInfoEnum.PARAMS_NO_COMPLETE);
|
throw new GlobalErrorInfoException(CityErrorInfoEnum.PARAMS_NO_COMPLETE);
|
||||||
}
|
}
|
||||||
|
return new ResultBody(new City(1L,2L,"温岭","是我的故乡"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user