diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java index a9c1815a0..2da42bd0c 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java @@ -186,7 +186,7 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo Safe.run(() -> { Object[] params = new Object[]{null}; //If there is only one parameter and it is a String, no further parsing is necessary; it can be used directly. - if (isSingleStringParameterMethod(method)) { + if (isSingleStringParameterMethod(method) && request.getMethod().toUpperCase().equals("POST")) { params[0] = new String(request.getBody()); } else { JsonElement args = getArgs(method, request.getMethod().toLowerCase(Locale.ROOT), request, context); diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcContext.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcContext.java index acd896819..d3acec10a 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcContext.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcContext.java @@ -63,6 +63,8 @@ public class MvcContext { private Object response; + private String contentType; + /** * rate limited or exceeded quota */ diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcResponse.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcResponse.java index aed8d0b30..1b7409746 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcResponse.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcResponse.java @@ -42,6 +42,9 @@ public void writeAndFlush(MvcContext context, HttpResponseStatus status, String } else { FullHttpResponse response = HttpResponseUtils.create(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.wrappedBuffer(message.getBytes()))); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, message.getBytes().length); + if (StringUtils.isNotEmpty(context.getContentType())) { + response.headers().set(HttpHeaderNames.CONTENT_TYPE, context.getContentType()); + } context.getResHeaders().forEach((k, v) -> response.headers().set(k, v)); if (context.isAllowCross()) { response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); @@ -53,7 +56,7 @@ public void writeAndFlush(MvcContext context, HttpResponseStatus status, String HttpSessionManager.setSessionId(context, HttpSessionManager.isHasSessionId(context.getHeaders()), response); } if (StringUtils.isNotEmpty(context.getRequest().headers().get(HttpHeaderNames.CONNECTION))) { - response.headers().add(HttpHeaderNames.CONNECTION,context.getRequest().headers().get(HttpHeaderNames.CONNECTION)); + response.headers().add(HttpHeaderNames.CONNECTION, context.getRequest().headers().get(HttpHeaderNames.CONNECTION)); } ctx.writeAndFlush(response); } diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java index ab2bd9867..56af4e867 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java @@ -104,7 +104,7 @@ public void run() { private void call() { if (context.isWebsocket()) { - WsRequest req = new Gson().fromJson(new String(request.getBody()), WsRequest.class); + WsRequest req = GsonUtils.gson.fromJson(new String(request.getBody()), WsRequest.class); request.setPath(req.getPath()); request.setBody(GsonUtils.gson.toJson(req.getParams()).getBytes()); } @@ -122,6 +122,9 @@ private void call() { sendNotFoundResponse(); return; } + if(path.endsWith(".html")) { + context.setContentType("text/html; charset=utf-8"); + } response.writeAndFlush(context, content); return; } diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/DemoController.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/DemoController.java index fe68c91b7..e2322945e 100644 --- a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/DemoController.java +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/demo/DemoController.java @@ -157,6 +157,12 @@ public String testGet(@RequestParam("a") int a, @RequestParam("b") int b) { return String.valueOf(a + b); } + @TAnno + @RequestMapping(path = "/testv", method = "get") + public String testV(@RequestParam("a") String a) { + return a; + } + @RequestMapping(path = "/testpost") public String testPost(String b) { log.info("b={}", b);