From 816397e6f5cf0e4887d6f515a2a05e91e102862f Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Tue, 2 Jul 2024 10:03:28 +0800 Subject: [PATCH 1/2] [enhancement] JCommon MongoDB supports pagination queries #851 --- jcommon/ai/bytedance/pom.xml | 7 +++ .../run/mone/ai/bytedance/DoubaoClient.java | 56 +++++++++++++++++++ .../src/main/java/run/mone/auth/AuthAop.java | 5 ++ .../src/main/java/run/mone/bo/User.java | 6 +- .../java/run/mone/service/MongoService.java | 22 ++++++++ .../youpin/docean/test/HttpServerTest.java | 3 +- jcommon/pom.xml | 30 +++++----- 7 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 jcommon/ai/bytedance/src/main/java/run/mone/ai/bytedance/DoubaoClient.java diff --git a/jcommon/ai/bytedance/pom.xml b/jcommon/ai/bytedance/pom.xml index bbadac9dd..b19d7e7e2 100644 --- a/jcommon/ai/bytedance/pom.xml +++ b/jcommon/ai/bytedance/pom.xml @@ -29,6 +29,13 @@ jackson-databind 2.12.4 + + + com.squareup.okhttp3 + okhttp + 4.10.0 + + diff --git a/jcommon/ai/bytedance/src/main/java/run/mone/ai/bytedance/DoubaoClient.java b/jcommon/ai/bytedance/src/main/java/run/mone/ai/bytedance/DoubaoClient.java new file mode 100644 index 000000000..94808fb5f --- /dev/null +++ b/jcommon/ai/bytedance/src/main/java/run/mone/ai/bytedance/DoubaoClient.java @@ -0,0 +1,56 @@ +package run.mone.ai.bytedance; + + +import okhttp3.*; + +/** + * @author goodjava@qq.com + * @date 2024/5/18 17:20 + */ +public class DoubaoClient { + + private static String ARK_API_KEY = ""; + + public static void main(String[] args) { + OkHttpClient client = new OkHttpClient(); + String json = "{\n" + + " \"model\": \"\",\n" + + " \"messages\": [\n" + + " {\n" + + " \"role\": \"system\",\n" + + " \"content\": \"You are a helpful assistant.\"\n" + + " },\n" + + " {\n" + + " \"role\": \"user\",\n" + + " \"content\": \"Hello!\"\n" + + " }\n" + + " ],\n" + + " \"stream\": false\n" + + "}"; + + MediaType JSON = MediaType.get("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(json, JSON); + + Request request = new Request.Builder() + .url("") + .addHeader("Content-Type", "application/json") + .addHeader("Authorization", "Bearer " + ARK_API_KEY) // 确保你已经设置了 ARK_API_KEY 变量 + .post(body) + .build(); + + try (Response response = client.newCall(request).execute()) { + // 处理响应 + if (response.isSuccessful()) { + String responseBody = response.body().string(); + System.out.println(responseBody); + } else { + System.out.println(response); + } + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/auth/AuthAop.java b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/auth/AuthAop.java index f9ea28bfa..ad8252e42 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/auth/AuthAop.java +++ b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/auth/AuthAop.java @@ -68,6 +68,11 @@ public void before(ProceedingJoinPoint point) { } } + //没有就当普通用户处理 + if (null == user.getRole()) { + user.setRole("user"); + } + //必须有后台管理权限 if (role.equals("admin")) { if (null == user || !user.getRole().equals("admin")) { diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/bo/User.java b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/bo/User.java index 01f224d39..0480cfc6c 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/bo/User.java +++ b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/bo/User.java @@ -7,6 +7,8 @@ import lombok.Data; import lombok.NoArgsConstructor; +import java.util.Map; + /** * 用户 * @@ -17,7 +19,7 @@ @AllArgsConstructor @NoArgsConstructor @Entity("user") -public class User implements MongoBo{ +public class User implements MongoBo { @Id private String id; @@ -57,6 +59,8 @@ public class User implements MongoBo{ private String token; + private Map meta; + public User(String username, String password) { this.username = username; this.password = password; diff --git a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/service/MongoService.java b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/service/MongoService.java index 7b0171630..8b5726413 100644 --- a/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/service/MongoService.java +++ b/jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/service/MongoService.java @@ -3,6 +3,7 @@ import com.xiaomi.youpin.docean.anno.Service; import dev.morphia.Datastore; import dev.morphia.UpdateOptions; +import dev.morphia.query.FindOptions; import dev.morphia.query.filters.Filter; import dev.morphia.query.filters.Filters; import dev.morphia.query.updates.UpdateOperator; @@ -65,6 +66,27 @@ public List findAll(Filter filter) { return datastore.find(this.clazz).filter(filter).iterator().toList(); } + public List findAll() { + return datastore.find(this.clazz).iterator().toList(); + } + + //实现分页查询(class) + public List findWithPagination(Filter filter, int page, int size) { + FindOptions options = new FindOptions() + .skip((page - 1) * size) + .limit(size); + return datastore.find(this.clazz) + .filter(filter) + .iterator(options) + .toList(); + } + + //查询总页数(class) + public long getTotalPages(Filter filter, int pageSize) { + long totalRecords = datastore.find(this.clazz).filter(filter).count(); + return (totalRecords + pageSize - 1) / pageSize; + } + public boolean delete(T t) { datastore.delete(t); return true; diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java index bce62cebd..33ede037e 100644 --- a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java @@ -116,7 +116,8 @@ public void testWebSocketServer() { @Test public void testWebSocketClient() { OkHttpClient client = new OkHttpClient(); - Request request = new Request.Builder().url("ws://127.0.0.1:8899/ws").build(); +// Request request = new Request.Builder().url("ws://127.0.0.1:8899/ws").build(); + Request request = new Request.Builder().url("").build(); WebSocketListener listener = new WebSocketListener() { @Override public void onOpen(WebSocket webSocket, Response response) { diff --git a/jcommon/pom.xml b/jcommon/pom.xml index bd8a0c84c..66ebff815 100644 --- a/jcommon/pom.xml +++ b/jcommon/pom.xml @@ -232,21 +232,21 @@ - - - - - - - - - - - - - - - + + maven-source-plugin + 2.1 + + + compile + + jar + + + + + true + + From afc6ef022fdc6d7b64eddc8f75e3dc6a5a2d939a Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Wed, 3 Jul 2024 18:48:46 +0800 Subject: [PATCH 2/2] [enhancement] support Native Return in docean-mvc Module #855 --- .../java/com/xiaomi/youpin/docean/Mvc.java | 23 +++++++++++++++---- .../youpin/docean/anno/RequestMapping.java | 3 +++ .../youpin/docean/mvc/HttpRequestMethod.java | 3 +++ .../xiaomi/youpin/docean/mvc/MvcContext.java | 2 +- .../xiaomi/youpin/docean/mvc/MvcRunnable.java | 1 - 5 files changed, 25 insertions(+), 7 deletions(-) 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 7d5f6c201..0a73ec97f 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 @@ -16,6 +16,7 @@ package com.xiaomi.youpin.docean; +import com.google.common.base.Throwables; import com.google.common.collect.Maps; import com.google.gson.Gson; import com.google.gson.JsonElement; @@ -138,6 +139,7 @@ private void registerControllerMethods(Bean bean) { } HttpRequestMethod hrm = new HttpRequestMethod(); hrm.setTimeout(rm.timeout()); + hrm.setOriginalRes(rm.originalRes()); hrm.setPath(path); hrm.setObj(bean.getObj()); hrm.setMethod(m); @@ -252,11 +254,8 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo return; } // get whether the configuration returns an unwrapped value - boolean needOriginalValue = this.mvcConfig.isResponseOriginalValue(); - if (!needOriginalValue && StringUtils.isNotBlank(this.mvcConfig.getResponseOriginalPath())) { - needOriginalValue = Arrays.stream(mvcConfig.getResponseOriginalPath().split(",")) - .anyMatch(i -> i.equals(method.getPath())); - } + boolean needOriginalValue = isNeedOriginalValue(method); + if (needOriginalValue) { String responseData = data instanceof String ? (String) data : gson.toJson(data); response.writeAndFlush(context, responseData); @@ -269,6 +268,7 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo context.setResponse(ex); return; } + ex = Throwables.getRootCause(ex); Throwable unwrapThrowable = ExceptionUtil.unwrapThrowable(ex); result.setCode(500); int httpCode = 200; @@ -284,6 +284,19 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo }); } + private boolean isNeedOriginalValue(HttpRequestMethod method) { + //优先级最高 + if (method.isOriginalRes()) { + return true; + } + boolean needOriginalValue = this.mvcConfig.isResponseOriginalValue(); + if (!needOriginalValue && StringUtils.isNotBlank(this.mvcConfig.getResponseOriginalPath())) { + needOriginalValue = Arrays.stream(mvcConfig.getResponseOriginalPath().split(",")) + .anyMatch(i -> i.equals(method.getPath())); + } + return needOriginalValue; + } + private Object[] getMethodParams(MvcContext context, MvcRequest request, HttpRequestMethod method) { Object[] params = new Object[]{null}; if (context.isWebsocket()) { diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/RequestMapping.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/RequestMapping.java index a1cb37aa1..9b24e7d98 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/RequestMapping.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/RequestMapping.java @@ -33,4 +33,7 @@ long timeout() default 2000; + //是否是原始res + boolean originalRes() default false; + } diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java index 70756ec32..8faa6bc89 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java @@ -37,6 +37,9 @@ public class HttpRequestMethod { private long timeout; + //结果不封装 + private boolean originalRes; + private Map genericSuperclassTypeArguments; 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 d3acec10a..a05a23d17 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,7 +63,7 @@ public class MvcContext { private Object response; - private String contentType; + private String contentType = "application/json; charset=utf-8"; /** * rate limited or exceeded quota 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 8e848470c..70242831e 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 @@ -2,7 +2,6 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; -import com.google.gson.JsonParser; import com.xiaomi.youpin.docean.Mvc; import com.xiaomi.youpin.docean.bo.MvcConfig; import com.xiaomi.youpin.docean.common.Cons;