You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2023/05/11 12:02:13 UTC
[dubbo] branch 3.2 updated: dubbo 3.2 Rest easyrest response add adaptation (#12264)
This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new 66eff807e6 dubbo 3.2 Rest easyrest response add adaptation (#12264)
66eff807e6 is described below
commit 66eff807e6ddbd8f0541a5188606ecfcf32bc5bc
Author: jojocodeX <57...@qq.com>
AuthorDate: Thu May 11 20:02:05 2023 +0800
dubbo 3.2 Rest easyrest response add adaptation (#12264)
* 添加Jackson序列化器
支持Jackson序列化器扩展配置
代码格式化调整
* Jackson module 注册错误忽略异常
* Jackson module 注册错误忽略异常
* add LdapJackson2Module Jackson module
* add spi to dubbo-all pom config
* remove unused code
* undo remove code
* Run CI
* merge conflict
* ignore codec error
* Change log output
* Add log error code
* rest resteasy response codec
* change reflect invoke
* NPE
* NPE
---
.../rpc/protocol/rest/message/codec/JsonCodec.java | 5 +-
.../{JsonCodec.java => ResteasyResponseCodec.java} | 65 +++++++++++-------
...ubbo.rpc.protocol.rest.message.HttpMessageCodec | 1 +
.../rpc/protocol/rest/ResteasyResponseTest.java | 78 ++++++++++++++++++++++
.../rpc/protocol/rest/rest/RestDemoService.java | 5 ++
.../protocol/rest/rest/RestDemoServiceImpl.java | 13 +++-
6 files changed, 138 insertions(+), 29 deletions(-)
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java
index 7d494b53b2..8ff5f2b60f 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java
@@ -23,7 +23,7 @@ import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
import org.apache.dubbo.rpc.protocol.rest.message.MediaTypeMatcher;
import org.apache.dubbo.rpc.protocol.rest.util.DataParseUtils;
-
+import javax.ws.rs.core.Response;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
@@ -37,10 +37,9 @@ public class JsonCodec implements HttpMessageCodec<byte[], OutputStream> {
private static final Set<Class> unSupportClasses = new HashSet<>();
static {
-
unSupportClasses.add(byte[].class);
unSupportClasses.add(String.class);
-
+ unSupportClasses.add(Response.class);
}
@Override
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/ResteasyResponseCodec.java
similarity index 52%
copy from dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java
copy to dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/ResteasyResponseCodec.java
index 7d494b53b2..31fe9e2db9 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/ResteasyResponseCodec.java
@@ -14,48 +14,40 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.dubbo.rpc.protocol.rest.message.codec;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.JsonUtils;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
-import org.apache.dubbo.rpc.protocol.rest.message.MediaTypeMatcher;
-import org.apache.dubbo.rpc.protocol.rest.util.DataParseUtils;
import java.io.OutputStream;
+import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
-import java.util.HashSet;
-import java.util.Set;
-
-/**
- * body is json
- */
-@Activate("json")
-public class JsonCodec implements HttpMessageCodec<byte[], OutputStream> {
- private static final Set<Class> unSupportClasses = new HashSet<>();
- static {
-
- unSupportClasses.add(byte[].class);
- unSupportClasses.add(String.class);
+@Activate(onClass="javax.ws.rs.core.Response")
+public class ResteasyResponseCodec implements HttpMessageCodec<byte[], OutputStream> {
- }
-
- @Override
- public Object decode(byte[] body, Class<?> targetType) throws Exception {
- return DataParseUtils.jsonConvert(targetType, body);
+ private Class<?> responseClass;
+ public ResteasyResponseCodec(){
+ try {
+ responseClass = ClassUtils.forName("javax.ws.rs.core.Response");
+ } catch (Exception exception) {
+ responseClass = null;
+ }
}
@Override
public boolean contentTypeSupport(MediaType mediaType, Class<?> targetType) {
- return MediaTypeMatcher.APPLICATION_JSON.mediaSupport(mediaType) && !unSupportClasses.contains(targetType);
+ return isMatch(targetType);
}
@Override
public boolean typeSupport(Class<?> targetType) {
- return !unSupportClasses.contains(targetType) && !DataParseUtils.isTextType(targetType);
+ return isMatch(targetType);
}
@Override
@@ -63,9 +55,34 @@ public class JsonCodec implements HttpMessageCodec<byte[], OutputStream> {
return MediaType.APPLICATION_JSON_VALUE;
}
+ @Override
+ public Object decode(byte[] body, Class<?> targetType) throws Exception {
+ if (null == body || body.length == 0) {
+ return null;
+ }
+
+ Class<?> builtResponse = ClassUtils.forName("org.jboss.resteasy.specimpl.BuiltResponse");
+
+ Object o = builtResponse.newInstance();
+
+ Method method = builtResponse.getMethod("setEntity", Object.class);
+
+ method.invoke(o, new String(body, StandardCharsets.UTF_8));
+
+ return o;
+ }
@Override
- public void encode(OutputStream outputStream, Object unSerializedBody, URL url) throws Exception {
- outputStream.write(JsonUtils.toJson(unSerializedBody).getBytes(StandardCharsets.UTF_8));
+ public void encode(OutputStream os, Object target, URL url) throws Exception {
+ if (target != null) {
+ Method method = target.getClass().getMethod("getEntity");
+ method.setAccessible(true);
+ Object result = method.invoke(target);
+ os.write(JsonUtils.toJson(result).getBytes(StandardCharsets.UTF_8));
+ }
+ }
+
+ private boolean isMatch(Class<?> targetType) {
+ return responseClass != null && null != targetType && responseClass.isAssignableFrom(targetType);
}
}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec b/dubbo-rpc/dubbo-rpc-rest/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec
index 11a7b32539..5a4daf8134 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec
@@ -4,3 +4,4 @@ json=org.apache.dubbo.rpc.protocol.rest.message.codec.JsonCodec
string=org.apache.dubbo.rpc.protocol.rest.message.codec.StringCodec
byteArray=org.apache.dubbo.rpc.protocol.rest.message.codec.ByteArrayCodec
xml=org.apache.dubbo.rpc.protocol.rest.message.codec.XMLCodec
+resteasyResponseCodec=org.apache.dubbo.rpc.protocol.rest.message.codec.ResteasyResponseCodec
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ResteasyResponseTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ResteasyResponseTest.java
new file mode 100644
index 0000000000..8884ba780e
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ResteasyResponseTest.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.rpc.protocol.rest;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.rpc.Protocol;
+import org.apache.dubbo.rpc.ProxyFactory;
+import org.apache.dubbo.rpc.model.*;
+import org.apache.dubbo.rpc.protocol.rest.rest.RestDemoService;
+import org.apache.dubbo.rpc.protocol.rest.rest.RestDemoServiceImpl;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import javax.ws.rs.core.Response;
+
+import static org.apache.dubbo.remoting.Constants.SERVER_KEY;
+
+public class ResteasyResponseTest {
+
+ private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest");
+ private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
+ private final int availablePort = NetUtils.getAvailablePort();
+ private final URL exportUrl = URL.valueOf("rest://127.0.0.1:" + availablePort + "/rest?interface=org.apache.dubbo.rpc.protocol.rest.rest.RestDemoService");
+ private final ModuleServiceRepository repository = ApplicationModel.defaultModel().getDefaultModule().getServiceRepository();
+
+ @AfterEach
+ public void tearDown() {
+ protocol.destroy();
+ FrameworkModel.destroyAll();
+ }
+ @Test
+ void testResponse() {
+ RestDemoService server = new RestDemoServiceImpl();
+ URL url = this.registerProvider(exportUrl, server, RestDemoService.class);
+
+ URL nettyUrl = url.addParameter(SERVER_KEY, "netty").addParameter("timeout", 3000000);
+
+ protocol.export(proxy.getInvoker(new RestDemoServiceImpl(), RestDemoService.class, nettyUrl));
+
+ RestDemoService demoService = this.proxy.getProxy(protocol.refer(RestDemoService.class, nettyUrl));
+
+ Response response = demoService.findUserById(10);
+
+ Assertions.assertNotNull(response);
+ }
+
+ private URL registerProvider(URL url, Object impl, Class<?> interfaceClass) {
+ ServiceDescriptor serviceDescriptor = repository.registerService(interfaceClass);
+ ProviderModel providerModel = new ProviderModel(
+ url.getServiceKey(),
+ impl,
+ serviceDescriptor,
+ null,
+ null);
+ repository.registerProvider(providerModel);
+ return url.setServiceModel(providerModel);
+ }
+
+
+
+}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java
index c423bbf3cf..56b3fd1ee7 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java
@@ -18,6 +18,7 @@ package org.apache.dubbo.rpc.protocol.rest.rest;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
@Path("/demoService")
@@ -26,6 +27,10 @@ public interface RestDemoService {
@Path("/hello")
Integer hello(@QueryParam("a")Integer a,@QueryParam("b") Integer b);
+ @GET
+ @Path("/findUserById")
+ Response findUserById(@QueryParam("id")Integer id);
+
@GET
@Path("/error")
String error();
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java
index 3ba0858b23..b0b32ed9a8 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java
@@ -17,10 +17,11 @@
package org.apache.dubbo.rpc.protocol.rest.rest;
import org.apache.dubbo.rpc.RpcContext;
-
+import org.jboss.resteasy.specimpl.BuiltResponse;
+import javax.ws.rs.core.Response;
+import java.util.HashMap;
import java.util.Map;
-
public class RestDemoServiceImpl implements RestDemoService {
private static Map<String, Object> context;
private boolean called;
@@ -53,6 +54,14 @@ public class RestDemoServiceImpl implements RestDemoService {
return a + b;
}
+ @Override
+ public Response findUserById(Integer id) {
+ Map<String,Object> content = new HashMap<>();
+ content.put("username","jack");
+ content.put("id",id);
+
+ return BuiltResponse.ok(content).build();
+ }
@Override
public String error() {