Skip to content

Commit 8a37f93

Browse files
committed
feat:Add generic parameter passing functionality to the HTTP protocol of trpc-java
1 parent 8fc21d5 commit 8a37f93

18 files changed

Lines changed: 390 additions & 41 deletions

File tree

trpc-core/src/main/java/com/tencent/trpc/core/utils/JsonUtils.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Tencent is pleased to support the open source community by making tRPC available.
33
*
4-
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
55
* All rights reserved.
66
*
77
* If you have downloaded a copy of the tRPC source code from Tencent,
@@ -23,6 +23,7 @@
2323
import com.tencent.trpc.core.logger.LoggerFactory;
2424
import java.io.IOException;
2525
import java.io.InputStream;
26+
import java.lang.reflect.Type;
2627

2728
/**
2829
* JSON utility.
@@ -71,6 +72,26 @@ public static <T> T fromInputStream(InputStream is, Class<T> clz) {
7172
}
7273
}
7374

75+
/**
76+
* json to Type
77+
*
78+
* @param is JSON input stream
79+
* @param type the class of the Type to deserialize
80+
* @param <T> the type of the deserialized object
81+
* @return the deserialized object
82+
*/
83+
public static <T> T fromInputStream(InputStream is, Type type) {
84+
try {
85+
return objectMapper
86+
.readValue(is, objectMapper.getTypeFactory().constructType(type));
87+
} catch (Exception e) {
88+
logger.error("object mapper readValue error:", e);
89+
throw TRpcException.newException(ErrorCode.JSON_DESERIALIZATION_ERR, 0,
90+
"object mapper readValue to Type error, jsonStream:%s, type:%s",
91+
is, type.getTypeName());
92+
}
93+
}
94+
7495
/**
7596
* JSON to Object.
7697
*
@@ -126,6 +147,26 @@ public static <T> T fromJson(String json, TypeReference<T> typeReference) {
126147
}
127148
}
128149

150+
/**
151+
* json to Type
152+
*
153+
* @param json JSON string
154+
* @param type the type reference of the object to deserialize
155+
* @param <T> the type of the deserialized object
156+
* @return the deserialized object
157+
*/
158+
public static <T> T fromJson(String json, Type type) {
159+
try {
160+
return objectMapper
161+
.readValue(json, objectMapper.getTypeFactory().constructType(type));
162+
} catch (Exception e) {
163+
logger.error("object mapper readValue error:", e);
164+
throw TRpcException.newException(ErrorCode.JSON_DESERIALIZATION_ERR, 0,
165+
"object mapper readValue to Type error, json:%s, type:%s", json,
166+
type.getTypeName());
167+
}
168+
}
169+
129170
/**
130171
* Byte arrays to Object.
131172
*

trpc-demo/trpc-spring-demo/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<modules>
1010
<module>trpc-spring-server-demo</module>
1111
<module>trpc-spring-client-demo</module>
12+
<module>trpc-spring-demo-api</module>
1213
</modules>
1314
<packaging>pom</packaging>
1415

trpc-demo/trpc-spring-demo/trpc-spring-client-demo/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
<groupId>com.tencent.trpc</groupId>
1717
<artifactId>trpc-proto-http</artifactId>
1818
</dependency>
19+
<dependency>
20+
<groupId>com.tencent.trpc</groupId>
21+
<artifactId>trpc-spring-demo-api</artifactId>
22+
<version>${parent.version}</version>
23+
</dependency>
1924
</dependencies>
2025

2126
<build>

trpc-demo/trpc-spring-demo/trpc-spring-client-demo/src/main/java/com/tencent/trpc/spring/demo/ClientApplication.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Tencent is pleased to support the open source community by making tRPC available.
33
*
4-
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
55
* All rights reserved.
66
*
77
* If you have downloaded a copy of the tRPC source code from Tencent,
@@ -12,6 +12,8 @@
1212
package com.tencent.trpc.spring.demo;
1313

1414
import com.google.protobuf.Message;
15+
import com.tencent.trpc.GreeterService3API;
16+
import com.tencent.trpc.GreeterService3API.RequestParameterizedBean;
1517
import com.tencent.trpc.core.rpc.RpcClientContext;
1618
import com.tencent.trpc.core.rpc.RpcContext;
1719
import com.tencent.trpc.demo.proto.GreeterService2AsyncAPI;
@@ -39,20 +41,26 @@ public static void main(String[] args) throws Exception {
3941
GreeterService2AsyncAPI service2 = context.getBean(ProxyService.SERVICE_NAME2,
4042
GreeterService2AsyncAPI.class);
4143

44+
GreeterService3API service3 = context.getBean(ProxyService.SERVICE_NAME3,
45+
GreeterService3API.class);
46+
4247
RpcContext ctx = new RpcClientContext();
4348
HelloRequestProtocol.HelloRequest request = HelloRequestProtocol.HelloRequest.newBuilder()
4449
.setMessage("tRPC-Java")
4550
.build();
51+
RequestParameterizedBean<String> parameterizedBean = RequestParameterizedBean.of("message",
52+
"hello parameterizedBean");
4653

4754
int times = 5;
4855

4956
for (int i = 0; i < times; i++) {
5057
System.out.println("service1>>>>" + syncGetMessage(service1.sayHello(ctx, request)));
5158
System.out.println("service2>>>>" + syncGetMessage(service2.sayHi(ctx, request)));
52-
System.out.println("demo service1>>>>" +
53-
syncGetMessage(demoService.getGreeterService().sayHello(ctx, request)));
54-
System.out.println("demo service2>>>>" +
55-
syncGetMessage(demoService.getGreeterService2().sayHi(ctx, request)));
59+
System.out.println("service3>>>>" + service3.sayHelloParameterized(ctx, parameterizedBean));
60+
System.out.println("demo service1>>>>"
61+
+ syncGetMessage(demoService.getGreeterService().sayHello(ctx, request)));
62+
System.out.println("demo service2>>>>"
63+
+ syncGetMessage(demoService.getGreeterService2().sayHi(ctx, request)));
5664
TimeUnit.SECONDS.sleep(1);
5765
}
5866

trpc-demo/trpc-spring-demo/trpc-spring-client-demo/src/main/java/com/tencent/trpc/spring/demo/server/ProxyService.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Tencent is pleased to support the open source community by making tRPC available.
33
*
4-
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
55
* All rights reserved.
66
*
77
* If you have downloaded a copy of the tRPC source code from Tencent,
@@ -11,6 +11,7 @@
1111

1212
package com.tencent.trpc.spring.demo.server;
1313

14+
import com.tencent.trpc.GreeterService3API;
1415
import com.tencent.trpc.demo.proto.GreeterService2AsyncAPI;
1516
import com.tencent.trpc.demo.proto.GreeterServiceAsyncAPI;
1617
import com.tencent.trpc.spring.annotation.TRpcClient;
@@ -21,13 +22,17 @@ public class ProxyService {
2122

2223
public static final String SERVICE_NAME1 = "trpc.TestApp.TestServer.Greeter1";
2324
public static final String SERVICE_NAME2 = "trpc.TestApp.TestServer.Greeter2";
25+
public static final String SERVICE_NAME3 = "trpc.TestApp.TestServer.Greeter3";
2426

2527
@TRpcClient(id = SERVICE_NAME1)
2628
private GreeterServiceAsyncAPI greeterService;
2729

2830
@TRpcClient(id = SERVICE_NAME2)
2931
private GreeterService2AsyncAPI greeterService2;
3032

33+
@TRpcClient(id = SERVICE_NAME3)
34+
private GreeterService3API greeterService3;
35+
3136
public GreeterServiceAsyncAPI getGreeterService() {
3237
return greeterService;
3338
}
@@ -36,4 +41,8 @@ public GreeterService2AsyncAPI getGreeterService2() {
3641
return greeterService2;
3742
}
3843

44+
public GreeterService3API getGreeterService3() {
45+
return greeterService3;
46+
}
47+
3948
}

trpc-demo/trpc-spring-demo/trpc-spring-client-demo/src/main/resources/application.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ trpc:
1414
interface: com.tencent.trpc.demo.proto.GreeterService2AsyncAPI # Service interface
1515
protocol: http # Protocol type, default is trpc
1616
basePath: /rest # The base path of the client service url address
17+
- name: trpc.TestApp.TestServer.Greeter3 # Service name
18+
naming_url: ip://127.0.0.1:12322 # Router address
19+
interface: com.tencent.trpc.GreeterService3API # Service interface
20+
protocol: http # Protocol type, default is trpc
21+
basePath: /rest # The base path of the client service url address
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.tencent.trpc</groupId>
8+
<artifactId>trpc-spring-demo</artifactId>
9+
<version>1.4.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>trpc-spring-demo-api</artifactId>
13+
<packaging>jar</packaging>
14+
<name>${project.artifactId}</name>
15+
<properties>
16+
<maven.compiler.source>17</maven.compiler.source>
17+
<maven.compiler.target>17</maven.compiler.target>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
21+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.tencent.trpc;
2+
3+
import com.tencent.trpc.core.rpc.RpcContext;
4+
import com.tencent.trpc.core.rpc.anno.TRpcMethod;
5+
import com.tencent.trpc.core.rpc.anno.TRpcService;
6+
import java.util.Map;
7+
8+
@TRpcService(name = "trpc.TestApp.TestServer.GreeterService3")
9+
public interface GreeterService3API {
10+
11+
@TRpcMethod(name = "sayHelloParameterized")
12+
<T> Map sayHelloParameterized(RpcContext context, RequestParameterizedBean<T> request);
13+
14+
class RequestParameterizedBean<T> {
15+
16+
String message;
17+
T data;
18+
19+
public static <T> RequestParameterizedBean<T> of(String message, T data) {
20+
RequestParameterizedBean<T> bean = new RequestParameterizedBean<>();
21+
bean.setMessage(message);
22+
bean.setData(data);
23+
return bean;
24+
}
25+
26+
public String getMessage() {
27+
return message;
28+
}
29+
30+
public void setMessage(String message) {
31+
this.message = message;
32+
}
33+
34+
public T getData() {
35+
return data;
36+
}
37+
38+
public void setData(T data) {
39+
this.data = data;
40+
}
41+
}
42+
}

trpc-demo/trpc-spring-demo/trpc-spring-server-demo/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
<groupId>com.tencent.trpc</groupId>
1717
<artifactId>trpc-springmvc</artifactId>
1818
</dependency>
19+
<dependency>
20+
<groupId>com.tencent.trpc</groupId>
21+
<artifactId>trpc-spring-demo-api</artifactId>
22+
<version>${parent.version}</version>
23+
</dependency>
1924
</dependencies>
2025

2126
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tencent.trpc.spring.demo.server.impl;
2+
3+
import com.tencent.trpc.GreeterService3API;
4+
import com.tencent.trpc.core.logger.Logger;
5+
import com.tencent.trpc.core.logger.LoggerFactory;
6+
import com.tencent.trpc.core.rpc.RpcContext;
7+
import java.util.Collections;
8+
import java.util.Map;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public class GreeterServiceImpl3 implements GreeterService3API {
13+
14+
private static final Logger logger = LoggerFactory.getLogger(GreeterServiceImpl3.class);
15+
16+
@Override
17+
public <T> Map sayHelloParameterized(RpcContext context, RequestParameterizedBean<T> request) {
18+
logger.info("got hello json request, request is '{}'", request);
19+
20+
return Collections.singletonMap("message", "Hi:" + request.getData());
21+
}
22+
}

0 commit comments

Comments
 (0)