This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit 2b143d2a37a7f134271c43afe8c9be589ca52354 Author: liubao <[email protected]> AuthorDate: Tue Feb 23 22:13:40 2021 +0800 [#2243]add examples for this issue --- .../zeroconfig/client/ClientServerEndpoint.java | 23 ++++++++++++++ .../demo/zeroconfig/client/IRpcEndpoint.java | 26 ++++++++++++++++ .../demo/zeroconfig/server/RpcEndpoint.java | 35 ++++++++++++++++++++++ .../demo/zeroconfig/tests/ServerTest.java | 28 +++++++++++++++++ 4 files changed, 112 insertions(+) diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientServerEndpoint.java b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientServerEndpoint.java index 4fe6eb1..f2fb732 100644 --- a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientServerEndpoint.java +++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientServerEndpoint.java @@ -27,12 +27,17 @@ import org.apache.servicecomb.provider.pojo.RpcReference; import org.apache.servicecomb.provider.rest.common.RestSchema; import org.apache.servicecomb.registry.DiscoveryManager; import org.apache.servicecomb.registry.api.registry.Microservice; +import org.apache.servicecomb.swagger.extend.annotations.RawJsonRequestBody; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import io.vertx.core.json.JsonObject; + @RestSchema(schemaId = "ClientServerEndpoint") @RequestMapping(path = "/register/url/prefix", produces = MediaType.APPLICATION_JSON) public class ClientServerEndpoint { @@ -42,6 +47,9 @@ public class ClientServerEndpoint { @RpcReference(microserviceName = "demo-zeroconfig-schemadiscovery-registry-server", schemaId = "ServerEndpoint") private IServerEndpoint serverEndpoint; + @RpcReference(microserviceName = "demo-zeroconfig-schemadiscovery-registry-server", schemaId = "RpcEndpoint") + private IRpcEndpoint rpcEndpoint; + @GetMapping(path = "/getName") public String getName(@RequestParam(name = "name") String name) { return serverEndpoint.getName(name); @@ -60,4 +68,19 @@ public class ClientServerEndpoint { } return names; } + + @PostMapping(path = "/jsonObject") + public JsonObject jsonObject(@RequestBody JsonObject jsonObject) { + JsonObject param = new JsonObject(); + param.put("map", jsonObject); + JsonObject message = rpcEndpoint.getJsonObject(param); + JsonObject inner = new JsonObject(); + inner.put("map", message); + return inner; + } + + @PostMapping(path = "/getString") + public String getString(@RawJsonRequestBody String jsonString) { + return jsonString; + } } \ No newline at end of file diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/IRpcEndpoint.java b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/IRpcEndpoint.java new file mode 100644 index 0000000..edbfc7f --- /dev/null +++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/IRpcEndpoint.java @@ -0,0 +1,26 @@ +/* + * 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.servicecomb.demo.zeroconfig.client; + +import io.vertx.core.json.JsonObject; + +public interface IRpcEndpoint { + JsonObject getJsonObject(JsonObject message); + + String getString(String message); +} diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-server/src/main/java/org/apache/servicecomb/demo/zeroconfig/server/RpcEndpoint.java b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-server/src/main/java/org/apache/servicecomb/demo/zeroconfig/server/RpcEndpoint.java new file mode 100644 index 0000000..37dba61 --- /dev/null +++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-server/src/main/java/org/apache/servicecomb/demo/zeroconfig/server/RpcEndpoint.java @@ -0,0 +1,35 @@ +/* + * 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.servicecomb.demo.zeroconfig.server; + +import org.apache.servicecomb.provider.pojo.RpcSchema; + +import io.vertx.core.json.JsonObject; + +@RpcSchema(schemaId = "RpcEndpoint") +public class RpcEndpoint { + public JsonObject getJsonObject(JsonObject message) { + JsonObject inner = new JsonObject(); + inner.put("map", message); + return inner; + } + + public String getString(String message) { + return message; + } +} diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ServerTest.java b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ServerTest.java index 1d906f5..21585d8 100644 --- a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ServerTest.java +++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ServerTest.java @@ -26,6 +26,8 @@ import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; +import io.vertx.core.json.JsonObject; + @Component public class ServerTest implements CategorizedTestCase { @@ -35,6 +37,8 @@ public class ServerTest implements CategorizedTestCase { public void testRestTransport() throws Exception { testServerGetName(); testGetAllMicroservice(); + testJsonObject(); + testString(); } private void testServerGetName() throws Exception { @@ -81,4 +85,28 @@ public class ServerTest implements CategorizedTestCase { + "/register/url/prefix/getRegisteredMicroservice", List.class).size()); } + + private void testJsonObject() { + JsonObject in = new JsonObject(); + JsonObject inner = new JsonObject(); + //调用者需要按照swagger传参 + inner.put("hello", "world"); + in.put("map", inner); + + JsonObject result = template + .postForObject( + "cse://demo-zeroconfig-schemadiscovery-registry-client" + + "/register/url/prefix/jsonObject", in, JsonObject.class); + TestMgr.check(inner.toString(), result.toString()); + TestMgr.check(result.getString("hello"), "world"); + } + + private void testString() { + String in = "{\"hello\":\"world\"}"; + String result = template + .postForObject( + "cse://demo-zeroconfig-schemadiscovery-registry-client" + + "/register/url/prefix/getString", in, String.class); + TestMgr.check(in, result); + } }
