diff --git a/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/MyObject.java b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/MyObject.java new file mode 100644 index 0000000..11dd18f --- /dev/null +++ b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/MyObject.java @@ -0,0 +1,17 @@ +package org.apache.dubbo.samples.spi.protocol.rest; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class MyObject { + + @JsonProperty + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/MyXmlObject.java b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/MyXmlObject.java new file mode 100644 index 0000000..022589f --- /dev/null +++ b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/MyXmlObject.java @@ -0,0 +1,21 @@ +package org.apache.dubbo.samples.spi.protocol.rest; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "MyXmlObject") +@XmlAccessorType(XmlAccessType.FIELD) +public class MyXmlObject { + @XmlElement + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/api/DemoService.java b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/api/DemoService.java index 626f5bb..f717d19 100644 --- a/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/api/DemoService.java +++ b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/api/DemoService.java @@ -20,12 +20,30 @@ package org.apache.dubbo.samples.spi.protocol.rest.api; +import org.apache.dubbo.samples.spi.protocol.rest.MyObject; +import org.apache.dubbo.samples.spi.protocol.rest.MyXmlObject; + +import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; @Path("/demo") public interface DemoService { @POST @Path("/sayHello") String sayHello(String name); + + @POST + @Path("/sayJson") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.TEXT_PLAIN) + String sayJson(MyObject obj); + + @POST + @Path("/sayXml") + @Consumes(MediaType.TEXT_XML) + @Produces(MediaType.TEXT_PLAIN) + String sayXml(MyXmlObject obj); } diff --git a/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/impl/DemoServiceImpl.java b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/impl/DemoServiceImpl.java index e07c187..2360f1e 100644 --- a/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/impl/DemoServiceImpl.java +++ b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/main/java/org/apache/dubbo/samples/spi/protocol/rest/impl/DemoServiceImpl.java @@ -19,6 +19,8 @@ package org.apache.dubbo.samples.spi.protocol.rest.impl; +import org.apache.dubbo.samples.spi.protocol.rest.MyObject; +import org.apache.dubbo.samples.spi.protocol.rest.MyXmlObject; import org.apache.dubbo.samples.spi.protocol.rest.api.DemoService; import org.apache.dubbo.rpc.RpcContext; @@ -32,4 +34,18 @@ public String sayHello(String name) { ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); } + + @Override + public String sayJson(MyObject obj) { + // Assuming MyObject has a method getName() that returns a String + return "Hello " + obj.getName(); + } + + + @Override + public String sayXml(MyXmlObject obj) { + return "Hello " + obj.getValue(); + } + + } diff --git a/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/test/java/DemoServiceIT.java b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/test/java/DemoServiceIT.java index ed464c3..7d9dd62 100644 --- a/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/test/java/DemoServiceIT.java +++ b/2-dubbo-spi-protocol-samples/samples-spi-protocol/samples-spi-protocol-rest/src/test/java/DemoServiceIT.java @@ -16,6 +16,8 @@ */ +import org.apache.dubbo.samples.spi.protocol.rest.MyObject; +import org.apache.dubbo.samples.spi.protocol.rest.MyXmlObject; import org.junit.Assert; import org.junit.Test; import org.springframework.http.HttpEntity; @@ -23,10 +25,14 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import java.util.Arrays; + public class DemoServiceIT { protected static final String HOST = System.getProperty("zookeeper.address", "localhost"); @@ -57,4 +63,47 @@ public void test() { Assert.assertTrue(response.getBody().startsWith("Hello world")); } + @Test + public void testPostJson() { + RestTemplate restTemplate = new RestTemplate(); + + MyObject myObject = new MyObject(); + myObject.setName("world"); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + HttpEntity requestEntity = new HttpEntity<>(myObject, headers); + + ResponseEntity response = restTemplate.postForEntity(toUri("demo/sayJson"), requestEntity, String.class); + + System.out.println("Response: " + response.getBody()); + Assert.assertTrue(response.getBody().contains("Hello world")); + } + + + @Test + public void testPostXml() { + + RestTemplate restTemplate = new RestTemplate(); + restTemplate.setMessageConverters(Arrays.asList( + new Jaxb2RootElementHttpMessageConverter(), + new StringHttpMessageConverter() + )); + + MyXmlObject xmlObject = new MyXmlObject(); + xmlObject.setValue("world"); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.TEXT_XML); + + HttpEntity requestEntity = new HttpEntity<>(xmlObject, headers); + + ResponseEntity response = restTemplate.postForEntity(toUri("demo/sayXml"), requestEntity, String.class); + + System.out.println("Response: " + response.getBody()); + Assert.assertTrue(response.getBody().contains("Hello world")); + } + + }