From c287889a8263cf4389613de4e83788393bfa26b8 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Mon, 1 Aug 2022 12:07:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E8=B0=83=E6=95=B4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../neutrino/core/web/HttpRequestHandler.java | 15 +++++-- .../server/base/rest/ExceptionConstant.java | 5 ++- .../server/base/rest/ServiceException.java | 7 +++ .../server/controller/IndexController.java | 9 ++-- .../proxy/server/util/ParamCheckUtil.java | 43 +++++++++++++++++++ 5 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java index 064827a4..3d9de77c 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java @@ -194,6 +194,9 @@ public class HttpRequestHandler { } private Object exceptionHandler(Throwable e) { + if (e instanceof InvocationTargetException) { + e = ((InvocationTargetException)e).getTargetException(); + } ExceptionHandlerRegistry exceptionHandlerRegistry = WebContextHolder.getExceptionHandlerRegistry(); if (CollectionUtil.isEmpty(exceptionHandlerRegistry.getExceptionHandlerList())) { log.error("Http处理异常", e); @@ -210,7 +213,7 @@ public class HttpRequestHandler { return null; } - private Object invoke(Object instance, Method method) throws InvocationTargetException, IllegalAccessException { + private Object invoke(Object instance, Method method) throws InvocationTargetException, IllegalAccessException, InstantiationException { Object[] params = new Object[method.getParameterCount()]; if (method.getParameterCount() > 0) { for (int i = 0; i < method.getParameters().length; i++) { @@ -226,7 +229,11 @@ public class HttpRequestHandler { if (TypeUtil.isNormalBasicType(parameter.getType())) { params[i] = TypeUtil.conversion(bodyString, parameter.getType()); } else { - params[i] = JSONObject.parseObject(bodyString, parameter.getType()); + if (StringUtil.isEmpty(bodyString)) { + params[i] = parameter.getType().newInstance(); + } else { + params[i] = JSONObject.parseObject(bodyString, parameter.getType()); + } } } else if (parameter.isAnnotationPresent(RequestParam.class)) { RequestParam requestParam = parameter.getAnnotation(RequestParam.class); @@ -243,16 +250,16 @@ public class HttpRequestHandler { params[i] = TypeUtil.conversion(val, parameter.getType()); } } else if (!TypeUtil.isNormalBasicType(parameter.getType())) { + Object obj = parameter.getType().newInstance(); + params[i] = obj; Set fields = ReflectUtil.getDeclaredFields(parameter.getType()); if (CollectionUtil.notEmpty(fields)) { try { - Object obj = parameter.getType().newInstance(); for (Field field : fields) { String name = field.getName(); Object value = TypeUtil.conversion(HttpContextHolder.getHttpRequestWrapper().getParameter(name), field.getType()); ReflectUtil.setFieldValue(field, obj, value); } - params[i] = obj; } catch (Exception e) { e.printStackTrace(); } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ExceptionConstant.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ExceptionConstant.java index 3066c27f..e48fca4c 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ExceptionConstant.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ExceptionConstant.java @@ -33,8 +33,9 @@ import lombok.Getter; @AllArgsConstructor public enum ExceptionConstant { SUCCESS(0, "成功"), - PARAMS_INVALID(1, "参数不正确"), - USER_NOT_LOGIN(2, "用户未登录"), + USER_NOT_LOGIN(1, "用户未登录"), + PARAMS_INVALID(2, "参数不正确"), + PARAMS_NOT_NULL(3, "参数[%s]不能为空"), SYSTEM_ERROR(500, "系统异常"); private int code; diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java index f55b1ba5..d0b3c614 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java @@ -41,9 +41,16 @@ public class ServiceException extends RuntimeException { public ServiceException(int code, String msg) { this.code = code; + this.msg = msg; } public static ServiceException create(ExceptionConstant constant) { return new ServiceException(constant.getCode(), constant.getMsg()); } + + public static ServiceException create(ExceptionConstant constant, Object... params) { + int code = constant.getCode(); + String msg = String.format(constant.getMsg(), params); + return new ServiceException(code, msg); + } } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/IndexController.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/IndexController.java index 393d6011..4811be6a 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/IndexController.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/IndexController.java @@ -26,10 +26,9 @@ import fun.asgc.neutrino.core.web.annotation.PostMapping; import fun.asgc.neutrino.core.web.annotation.RequestBody; import fun.asgc.neutrino.core.web.annotation.RequestMapping; import fun.asgc.neutrino.core.web.annotation.RestController; -import fun.asgc.neutrino.proxy.server.base.rest.ExceptionConstant; -import fun.asgc.neutrino.proxy.server.base.rest.ServiceException; import fun.asgc.neutrino.proxy.server.controller.req.LoginReq; import fun.asgc.neutrino.proxy.server.controller.res.LoginRes; +import fun.asgc.neutrino.proxy.server.util.ParamCheckUtil; /** * @@ -43,9 +42,9 @@ public class IndexController { @PostMapping("login") public LoginRes login(@RequestBody LoginReq req) { - if (null == req) { - throw ServiceException.create(ExceptionConstant.PARAMS_INVALID); - } + ParamCheckUtil.checkNotEmpty(req.getLoginName(), "loginName"); + ParamCheckUtil.checkNotEmpty(req.getLoginPassword(), "loginPassword"); + return new LoginRes() .setToken("1111") .setUserId(1) diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java new file mode 100644 index 00000000..4391e7e6 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.util; + +import fun.asgc.neutrino.core.util.StringUtil; +import fun.asgc.neutrino.proxy.server.base.rest.ExceptionConstant; +import fun.asgc.neutrino.proxy.server.base.rest.ServiceException; + +/** + * + * @author: aoshiguchen + * @date: 2022/8/1 + */ +public class ParamCheckUtil { + + public static void checkNotEmpty(Object obj, String name) { + if (obj == null) { + throw ServiceException.create(ExceptionConstant.PARAMS_NOT_NULL, name); + } + if (obj instanceof String && StringUtil.isEmpty((String) obj)) { + throw ServiceException.create(ExceptionConstant.PARAMS_NOT_NULL, name); + } + } +}