Skip to content

Commit

Permalink
Support sending generic requests in JSON format (apache#7637)
Browse files Browse the repository at this point in the history
  • Loading branch information
goodjava committed May 19, 2021
1 parent 1b53938 commit a873a1d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ public interface CommonConstants {

String GENERIC_SERIALIZATION_NATIVE_JAVA = "nativejava";

String GENERIC_SERIALIZATION_GSON = "gson";

String GENERIC_SERIALIZATION_DEFAULT = "true";

String GENERIC_SERIALIZATION_BEAN = "bean";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.GENERIC_SERIALIZATION_BEAN;
import static org.apache.dubbo.common.constants.CommonConstants.GENERIC_SERIALIZATION_DEFAULT;
import static org.apache.dubbo.common.constants.CommonConstants.GENERIC_SERIALIZATION_NATIVE_JAVA;
import static org.apache.dubbo.common.constants.CommonConstants.GENERIC_SERIALIZATION_GSON;
import static org.apache.dubbo.common.constants.CommonConstants.GENERIC_SERIALIZATION_PROTOBUF;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
Expand Down Expand Up @@ -58,6 +59,7 @@ public static boolean isGeneric(String generic) {
|| GENERIC_SERIALIZATION_NATIVE_JAVA.equalsIgnoreCase(generic) /* Streaming generalization call supporting jdk serialization */
|| GENERIC_SERIALIZATION_BEAN.equalsIgnoreCase(generic)
|| GENERIC_SERIALIZATION_PROTOBUF.equalsIgnoreCase(generic)
|| GENERIC_SERIALIZATION_GSON.equalsIgnoreCase(generic)
|| GENERIC_RAW_RETURN.equalsIgnoreCase(generic));

}
Expand All @@ -77,6 +79,11 @@ public static boolean isJavaGenericSerialization(String generic) {
&& GENERIC_SERIALIZATION_NATIVE_JAVA.equalsIgnoreCase(generic);
}

public static boolean isGsonGenericSerialization(String generic) {
return isGeneric(generic)
&& GENERIC_SERIALIZATION_GSON.equalsIgnoreCase(generic);
}

public static boolean isBeanGenericSerialization(String generic) {
return isGeneric(generic) && GENERIC_SERIALIZATION_BEAN.equals(generic);
}
Expand Down
6 changes: 6 additions & 0 deletions dubbo-rpc/dubbo-rpc-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@
<artifactId>hessian-lite</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.apache.dubbo.rpc.filter;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import org.apache.dubbo.common.beanutil.JavaBeanAccessor;
import org.apache.dubbo.common.beanutil.JavaBeanDescriptor;
import org.apache.dubbo.common.beanutil.JavaBeanSerializeUtil;
Expand Down Expand Up @@ -45,10 +49,13 @@

import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.stream.IntStream;

import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE;
import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE_ASYNC;
import static org.apache.dubbo.common.constants.CommonConstants.GENERIC_SERIALIZATION_BEAN;
import static org.apache.dubbo.common.constants.CommonConstants.GENERIC_SERIALIZATION_GSON;
import static org.apache.dubbo.common.constants.CommonConstants.GENERIC_SERIALIZATION_NATIVE_JAVA;
import static org.apache.dubbo.common.constants.CommonConstants.GENERIC_SERIALIZATION_PROTOBUF;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
Expand Down Expand Up @@ -98,6 +105,8 @@ public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
} catch (IllegalArgumentException e) {
throw new RpcException(e);
}
} else if (ProtocolUtils.isGsonGenericSerialization(generic)) {
args = getGsonGenericArgs(args, method.getGenericParameterTypes());
} else if (ProtocolUtils.isJavaGenericSerialization(generic)) {
Configuration configuration = ApplicationModel.getEnvironment().getConfiguration();
if (!configuration.getBoolean(CommonConstants.ENABLE_NATIVE_JAVA_GENERIC_SERIALIZE, false)) {
Expand Down Expand Up @@ -180,6 +189,19 @@ public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
return invoker.invoke(inv);
}

private Object[] getGsonGenericArgs(final Object[] args, Type[] types) {
Gson gson = new Gson();
return IntStream.range(0, args.length).mapToObj(i -> {
String str = args[i].toString();
Type type = TypeToken.get(types[i]).getType();
try {
return gson.fromJson(str, type);
} catch (JsonSyntaxException ex) {
throw new RpcException(String.format("Generic serialization [%s] Json syntax exception thrown when parsing (message:%s type:%s) error:%s", GENERIC_SERIALIZATION_GSON, str, type.toString(), ex.getMessage()));
}
}).toArray();
}

@Override
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation inv) {
if ((inv.getMethodName().equals($INVOKE) || inv.getMethodName().equals($INVOKE_ASYNC))
Expand Down

0 comments on commit a873a1d

Please sign in to comment.