From ecfcb02e8d53c5f322678b69d94331b122137c78 Mon Sep 17 00:00:00 2001 From: JeffLi1993 Date: Tue, 14 Mar 2017 14:56:45 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=20HTTP=20over=20JSON=20=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=A0=81=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../result/GlobalErrorInfoEnum.java | 28 +++++++++++++++++++ .../spring/springboot/result/ResultBody.java | 14 +++++++--- .../springboot/web/ErrorJsonController.java | 20 +++++++++---- 3 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 springboot-validation-over-json/src/main/java/org/spring/springboot/result/GlobalErrorInfoEnum.java diff --git a/springboot-validation-over-json/src/main/java/org/spring/springboot/result/GlobalErrorInfoEnum.java b/springboot-validation-over-json/src/main/java/org/spring/springboot/result/GlobalErrorInfoEnum.java new file mode 100644 index 0000000..390634a --- /dev/null +++ b/springboot-validation-over-json/src/main/java/org/spring/springboot/result/GlobalErrorInfoEnum.java @@ -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; + } +} diff --git a/springboot-validation-over-json/src/main/java/org/spring/springboot/result/ResultBody.java b/springboot-validation-over-json/src/main/java/org/spring/springboot/result/ResultBody.java index 638ecf4..f024a0e 100644 --- a/springboot-validation-over-json/src/main/java/org/spring/springboot/result/ResultBody.java +++ b/springboot-validation-over-json/src/main/java/org/spring/springboot/result/ResultBody.java @@ -5,7 +5,7 @@ package org.spring.springboot.result; * * Created by bysocket on 14/03/2017. */ -public class ResultBody { +public class ResultBody { /** * 响应代码 */ @@ -19,13 +19,19 @@ public class ResultBody { /** * 响应结果 */ - private T result; + private Object result; public ResultBody(ErrorInfoInterface errorInfo) { this.code = errorInfo.getCode(); this.message = errorInfo.getMessage(); } + public ResultBody(Object result) { + this.code = GlobalErrorInfoEnum.SUCCESS.getCode(); + this.message = GlobalErrorInfoEnum.SUCCESS.getMessage(); + this.result = result; + } + public String getCode() { return code; } @@ -42,11 +48,11 @@ public class ResultBody { this.message = message; } - public T getResult() { + public Object getResult() { return result; } - public void setResult(T result) { + public void setResult(Object result) { this.result = result; } } diff --git a/springboot-validation-over-json/src/main/java/org/spring/springboot/web/ErrorJsonController.java b/springboot-validation-over-json/src/main/java/org/spring/springboot/web/ErrorJsonController.java index 0634ff7..2f1d221 100644 --- a/springboot-validation-over-json/src/main/java/org/spring/springboot/web/ErrorJsonController.java +++ b/springboot-validation-over-json/src/main/java/org/spring/springboot/web/ErrorJsonController.java @@ -2,7 +2,11 @@ package org.spring.springboot.web; import org.spring.springboot.constant.CityErrorInfoEnum; 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.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** @@ -13,14 +17,20 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class ErrorJsonController { + /** - * 模拟入参不完整案例 + * 获取城市接口 * + * @param cityName * @return * @throws GlobalErrorInfoException */ - @RequestMapping("/param") - public String errorJsonParams() throws GlobalErrorInfoException { - throw new GlobalErrorInfoException(CityErrorInfoEnum.PARAMS_NO_COMPLETE); + @RequestMapping(value = "/api/city", method = RequestMethod.GET) + public ResultBody findOneCity(@RequestParam("cityName") String cityName) throws GlobalErrorInfoException { + // 入参为空 + if (StringUtils.isEmpty(cityName)) { + throw new GlobalErrorInfoException(CityErrorInfoEnum.PARAMS_NO_COMPLETE); + } + return new ResultBody(new City(1L,2L,"温岭","是我的故乡")); } -} +} \ No newline at end of file