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 f29f402f..8be4d8ca 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 @@ -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 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); diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/Param1.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/Param1.java new file mode 100644 index 00000000..445043e0 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/Param1.java @@ -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; +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/Param2.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/Param2.java new file mode 100644 index 00000000..095f21fb --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/Param2.java @@ -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; + +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/TestController.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/TestController.java index 0290c962..7770905d 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/TestController.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/TestController.java @@ -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"; + } }