支持使用实体类接收GET、POST参数

This commit is contained in:
aoshiguchen
2022-07-27 00:00:09 +08:00
parent 39d91947d9
commit 42cefd1926
4 changed files with 103 additions and 7 deletions
@@ -44,10 +44,12 @@ import io.netty.handler.codec.http.*;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Date;
import java.util.Set;
/**
*
@@ -131,8 +133,9 @@ public class HttpRequestHandler {
String bodyString = HttpContextHolder.getHttpRequestParser().getContentAsString();
if (TypeUtil.isNormalBasicType(parameter.getType())) {
params[i] = TypeUtil.conversion(bodyString, parameter.getType());
} else {
params[i] = JSONObject.parseObject(bodyString, parameter.getType());
}
// TODO 实体转换
} else if (parameter.isAnnotationPresent(RequestParam.class)) {
RequestParam requestParam = parameter.getAnnotation(RequestParam.class);
if (StringUtil.isEmpty(requestParam.value())) {
@@ -147,7 +150,22 @@ public class HttpRequestHandler {
} else {
params[i] = TypeUtil.conversion(val, parameter.getType());
}
}
} else if (!TypeUtil.isNormalBasicType(parameter.getType())) {
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.getHttpRequestParser().getParameter(name), field.getType());
ReflectUtil.setFieldValue(field, obj, value);
}
params[i] = obj;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
Object invokeResult = method.invoke(instance, params);
@@ -0,0 +1,36 @@
/**
* 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.core.web.test1;
import lombok.Data;
/**
*
* @author: aoshiguchen
* @date: 2022/7/26
*/
@Data
public class Param1 {
private Integer x;
private Integer y;
private String msg;
}
@@ -0,0 +1,37 @@
/**
* 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.core.web.test1;
import lombok.Data;
/**
*
* @author: aoshiguchen
* @date: 2022/7/26
*/
@Data
public class Param2 {
private String name;
private Integer age;
}
@@ -21,11 +21,9 @@
*/
package fun.asgc.neutrino.core.web.test1;
import com.alibaba.fastjson.JSONObject;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.web.annotation.GetMapping;
import fun.asgc.neutrino.core.web.annotation.RequestMapping;
import fun.asgc.neutrino.core.web.annotation.RequestParam;
import fun.asgc.neutrino.core.web.annotation.RestController;
import fun.asgc.neutrino.core.web.annotation.*;
import fun.asgc.neutrino.core.web.param.HttpContextHolder;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
@@ -52,8 +50,9 @@ public class TestController {
}
@GetMapping("add")
public Integer add(@RequestParam("x") int x, @RequestParam("y") int y) {
public Integer add(@RequestParam("x") int x, @RequestParam("y") int y, Param1 p) {
log.info("另一种取参方式 msg:{}", HttpContextHolder.getHttpRequestParser().getParameterForString("msg"));
log.info("p : {}", JSONObject.toJSONString(p));
return x + y;
}
@@ -68,4 +67,10 @@ public class TestController {
public void hello3(@RequestParam("a") String a, @RequestParam("b") String b, @RequestParam(value = "c", required = false) String c) {
log.info("a:{} b:{} c:{}", a, b, c);
}
@PostMapping("hello4")
public String hello4(@RequestParam("a") String a, Param1 p, @RequestBody String body, @RequestBody Param2 p2) {
log.info("a: {} p:{} body:{} p2:{}", a, JSONObject.toJSONString(p), body, JSONObject.toJSONString(p2));
return "hello4";
}
}