Skip to content

Commit

Permalink
fix: increase requests for json and xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Similarityoung committed Nov 8, 2024
1 parent d71fe34 commit 0a6dbf5
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@
*/


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;
import org.springframework.http.HttpHeaders;
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");
Expand Down Expand Up @@ -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<MyObject> requestEntity = new HttpEntity<>(myObject, headers);

ResponseEntity<String> 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<MyXmlObject> requestEntity = new HttpEntity<>(xmlObject, headers);

ResponseEntity<String> response = restTemplate.postForEntity(toUri("demo/sayXml"), requestEntity, String.class);

System.out.println("Response: " + response.getBody());
Assert.assertTrue(response.getBody().contains("Hello world"));
}


}

0 comments on commit 0a6dbf5

Please sign in to comment.