参数校验逻辑调整.
This commit is contained in:
@@ -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<Field> 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();
|
||||
}
|
||||
|
||||
+3
-2
@@ -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;
|
||||
|
||||
+7
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+4
-5
@@ -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)
|
||||
|
||||
+43
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user