Skip to content

Commit

Permalink
[SCB-2359]Implements RestOperations instead of RestTemplate (#3992)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Oct 23, 2023
1 parent 184911a commit 916a84d
Show file tree
Hide file tree
Showing 54 changed files with 188 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
Expand All @@ -52,7 +53,7 @@ public static void run() {
"sayHello", null, String.class);
TestMgr.check("hello world", result);

RestTemplate restTemplate = RestTemplateBuilder.create();
RestOperations restTemplate = RestTemplateBuilder.create();
result = restTemplate.getForObject("cse://appServer:appService/helloworld/hello", String.class);
TestMgr.check("hello world", result);
result = restTemplate.getForObject("servicecomb://appServer:appService/helloworld/hello", String.class);
Expand All @@ -68,7 +69,7 @@ public static void run() {
}

private static void testCorsHandler() {
RestTemplate springRestTemplate = new RestTemplate();
RestOperations springRestTemplate = new RestTemplate();
MultiValueMap<String, String> requestHeaders = new LinkedMultiValueMap<>();
requestHeaders.put("Origin", Collections.singletonList("http://localhost:8080"));
requestHeaders.put("Access-Control-Request-Method", Collections.singletonList("PUT"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import org.apache.servicecomb.demo.CategorizedTestCase;
import org.apache.servicecomb.demo.TestMgr;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

@Component
public class ConsumerConfigIT implements CategorizedTestCase {
RestTemplate template = new RestTemplate();
RestOperations template = new RestTemplate();

@Override
public void testRestTransport() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;


@Component
public class HelloWorldIT implements CategorizedTestCase {
RestTemplate template = new RestTemplate();
RestOperations template = new RestTemplate();

@Override
public void testRestTransport() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import org.apache.servicecomb.demo.CategorizedTestCase;
import org.apache.servicecomb.demo.TestMgr;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

@Component
public class ConsumerConfigIT implements CategorizedTestCase {
RestTemplate template = new RestTemplate();
RestOperations template = new RestTemplate();

@Override
public void testRestTransport() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import org.apache.servicecomb.demo.CategorizedTestCase;
import org.apache.servicecomb.demo.TestMgr;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

@Component
public class HelloWorldIT implements CategorizedTestCase {
RestTemplate template = new RestTemplate();
RestOperations template = new RestTemplate();

@Override
public void testRestTransport() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestOperations;

public class Consumer {
RestTemplate template = RestTemplateBuilder.create();
RestOperations template = RestTemplateBuilder.create();

ChannelRequestBase request = new ChannelRequestBase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestOperations;

@Component
public class EdgeServiceGovernanceTest implements CategorizedTestCase {
RestTemplate template = RestTemplateBuilder.create();
RestOperations template = RestTemplateBuilder.create();

String edgePrefix;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestOperations;

@RestSchema(schemaId = "ClientExceptionSchema")
@RequestMapping(path = "/exception", produces = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -38,7 +38,7 @@ interface IExceptionSchema {

private IExceptionSchema exceptionSchema;

private RestTemplate restTemplate = RestTemplateBuilder.create();
private RestOperations restTemplate = RestTemplateBuilder.create();

@RpcReference(microserviceName = "filterServer", schemaId = "ExceptionSchema")
public void setExceptionSchema(IExceptionSchema exceptionSchema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestOperations;

@RestSchema(schemaId = "RetryClientSchema")
@RequestMapping(path = "/retry", produces = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -36,7 +36,7 @@ interface RetrySchemaInf {
@RpcReference(microserviceName = "filterServer", schemaId = "RetrySchema")
private RetrySchemaInf retrySchemaInf;

RestTemplate restTemplate = RestTemplateBuilder.create();
RestOperations restTemplate = RestTemplateBuilder.create();

@GetMapping(path = "/governance/successWhenRetry")
public boolean successWhenRetry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestOperations;

@Component
public class TestExceptionSchemaFromClient implements CategorizedTestCase {

RestTemplate restTemplate = RestTemplateBuilder.create();
RestOperations restTemplate = RestTemplateBuilder.create();

private static final String SERVER = "servicecomb://filterClient";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestOperations;

@Component
public class TestRetrySchemaFromClient implements CategorizedTestCase {
Expand All @@ -36,7 +36,7 @@ interface RetrySchemaInf {
@RpcReference(microserviceName = "filterClient", schemaId = "RetryClientSchema")
private RetrySchemaInf retrySchemaInf;

RestTemplate restTemplate = RestTemplateBuilder.create();
RestOperations restTemplate = RestTemplateBuilder.create();

private static final String SERVER = "servicecomb://filterClient";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

@Component
Expand All @@ -36,7 +37,7 @@ interface RetrySchemaInf {
@RpcReference(microserviceName = "filterEdge", schemaId = "RetryClientSchema")
private RetrySchemaInf retrySchemaInf;

RestTemplate restTemplate = RestTemplateBuilder.create();
RestOperations restTemplate = RestTemplateBuilder.create();

RestTemplate springRestTemplate = new RestTemplate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestOperations;

import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response.Status;
Expand All @@ -59,7 +59,7 @@
public class JaxrsClient {
private static final Logger LOGGER = LoggerFactory.getLogger(JaxrsClient.class);

private static RestTemplate templateNew = RestTemplateBuilder.create();
private static RestOperations templateNew = RestTemplateBuilder.create();

public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(JaxrsClient.class).web(WebApplicationType.NONE).run(args);
Expand Down Expand Up @@ -100,7 +100,7 @@ public static void run() throws Exception {
testOnlyRest(templateNew);
}

private static void testOnlyRest(RestTemplate template) {
private static void testOnlyRest(RestOperations template) {
String microserviceName = "jaxrs";
String cseUrlPrefix = "cse://" + microserviceName;
InMemoryDynamicPropertiesSource.update("servicecomb.references.transport." + microserviceName, "rest");
Expand All @@ -109,7 +109,7 @@ private static void testOnlyRest(RestTemplate template) {
testSpringMvcDefaultValuesJavaPrimitiveRest(templateNew);
}

private static void testCompute(RestTemplate template) throws Exception {
private static void testCompute(RestOperations template) throws Exception {
String microserviceName = "jaxrs";
for (String transport : DemoConst.transports) {
InMemoryDynamicPropertiesSource.update("servicecomb.references.transport." + microserviceName, transport);
Expand All @@ -126,7 +126,7 @@ private static void testCompute(RestTemplate template) throws Exception {
}
}

private static void testValidator(RestTemplate template) {
private static void testValidator(RestOperations template) {
String microserviceName = "jaxrs";
for (String transport : DemoConst.transports) {
InMemoryDynamicPropertiesSource.update("servicecomb.references.transport." + microserviceName, transport);
Expand All @@ -150,7 +150,7 @@ private static void testValidator(RestTemplate template) {
}
}

private static void testJaxRSDefaultValuesRest(RestTemplate template) {
private static void testJaxRSDefaultValuesRest(RestOperations template) {
String result;
String microserviceName = "jaxrs";
String cseUrlPrefix = "cse://" + microserviceName + "/JaxRSDefaultValues/";
Expand Down Expand Up @@ -193,7 +193,7 @@ private static void testJaxRSDefaultValuesRest(RestTemplate template) {
TestMgr.check(failed, true);
}

private static void testJaxRSDefaultValuesAllTransport(RestTemplate template) {
private static void testJaxRSDefaultValuesAllTransport(RestOperations template) {
String microserviceName = "jaxrs";
for (String transport : DemoConst.transports) {
InMemoryDynamicPropertiesSource.update("servicecomb.references.transport." + microserviceName, transport);
Expand Down Expand Up @@ -288,7 +288,7 @@ private static void testJaxRSDefaultValuesAllTransport(RestTemplate template) {
}
}

private static void testGetRest(RestTemplate template, String cseUrlPrefix) {
private static void testGetRest(RestOperations template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("a", "5");
params.put("b", "3");
Expand All @@ -304,7 +304,7 @@ private static void testGetRest(RestTemplate template, String cseUrlPrefix) {
TestMgr.check(-1, result);
}

private static void testGetAllTransport(RestTemplate template, String cseUrlPrefix) {
private static void testGetAllTransport(RestOperations template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("a", "5");
params.put("b", "3");
Expand All @@ -320,7 +320,7 @@ private static void testGetAllTransport(RestTemplate template, String cseUrlPref
TestMgr.check(-1, result);
}

private static void testPost(RestTemplate template, String cseUrlPrefix) {
private static void testPost(RestOperations template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("a", "5");
params.put("b", "3");
Expand All @@ -341,17 +341,17 @@ private static void testPost(RestTemplate template, String cseUrlPrefix) {
template.postForObject(cseUrlPrefix + "/compute/saysomething", reqEntity, String.class));
}

private static void testPut(RestTemplate template, String cseUrlPrefix) {
private static void testPut(RestOperations template, String cseUrlPrefix) {
template.put(cseUrlPrefix + "/compute/sayhi/{name}", null, "world");
}

private static void testDelete(RestTemplate template, String cseUrlPrefix) {
private static void testDelete(RestOperations template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("name", "world");
template.delete(cseUrlPrefix + "/compute/sayhei/?name={name}", params);
}

private static void testExchange(RestTemplate template, String cseUrlPrefix) {
private static void testExchange(RestOperations template, String cseUrlPrefix) {
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON);
Person person = new Person();
Expand All @@ -368,7 +368,7 @@ private static void testExchange(RestTemplate template, String cseUrlPrefix) {
TestMgr.check("abcdef", resEntity2.getBody());
}

private static void testRawJsonParam(RestTemplate template, String cseUrlPrefix) throws Exception {
private static void testRawJsonParam(RestOperations template, String cseUrlPrefix) throws Exception {
Map<String, String> person = new HashMap<>();
person.put("name", "Tom");
String jsonPerson = RestObjectMapperFactory.getRestObjectMapper().writeValueAsString(person);
Expand All @@ -377,7 +377,7 @@ private static void testRawJsonParam(RestTemplate template, String cseUrlPrefix)
}

@SuppressWarnings({"rawtypes"})
private static void testValidatorAddFail(RestTemplate template, String cseUrlPrefix) {
private static void testValidatorAddFail(RestOperations template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("a", "5");
params.put("b", "3");
Expand Down Expand Up @@ -409,7 +409,7 @@ private static void testValidatorAddFail(RestTemplate template, String cseUrlPre
TestMgr.check(true, isExcep);
}

private static void testValidatorAddSuccess(RestTemplate template, String cseUrlPrefix) {
private static void testValidatorAddSuccess(RestOperations template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("a", "5");
params.put("b", "20");
Expand All @@ -418,7 +418,7 @@ private static void testValidatorAddSuccess(RestTemplate template, String cseUrl
}

@SuppressWarnings({"rawtypes"})
private static void testValidatorSayHiFail(RestTemplate template, String cseUrlPrefix) {
private static void testValidatorSayHiFail(RestOperations template, String cseUrlPrefix) {
boolean isExcep = false;
try {
template.exchange(cseUrlPrefix + "sayhi/{name}", HttpMethod.PUT, null, String.class, "te");
Expand All @@ -443,15 +443,15 @@ private static void testValidatorSayHiFail(RestTemplate template, String cseUrlP
TestMgr.check(true, isExcep);
}

private static void testValidatorSayHiSuccess(RestTemplate template, String cseUrlPrefix) {
private static void testValidatorSayHiSuccess(RestOperations template, String cseUrlPrefix) {
ResponseEntity<String> responseEntity =
template.exchange(cseUrlPrefix + "sayhi/{name}", HttpMethod.PUT, null, String.class, "world");
TestMgr.check(202, responseEntity.getStatusCode().value());
TestMgr.check("world sayhi", responseEntity.getBody());
}

@SuppressWarnings({"rawtypes"})
private static void testValidatorExchangeFail(RestTemplate template, String cseUrlPrefix) {
private static void testValidatorExchangeFail(RestOperations template, String cseUrlPrefix) {
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON);
Student student = new Student();
Expand Down Expand Up @@ -485,15 +485,15 @@ private static void testValidatorExchangeFail(RestTemplate template, String cseU
TestMgr.check(true, isExcep);
}

private static void testValidatorExchangeSuccess(RestTemplate template, String cseUrlPrefix) {
private static void testValidatorExchangeSuccess(RestOperations template, String cseUrlPrefix) {
Student student = new Student();
student.setName("test");
student.setAge(15);
Student result = template.postForObject(cseUrlPrefix + "sayhello", student, Student.class);
TestMgr.check("hello test 15", result);
}

private static void testSpringMvcDefaultValuesJavaPrimitiveRest(RestTemplate template) {
private static void testSpringMvcDefaultValuesJavaPrimitiveRest(RestOperations template) {
String microserviceName = "jaxrs";
String cseUrlPrefix = "cse://" + microserviceName + "/JaxRSDefaultValues/";

Expand Down Expand Up @@ -521,7 +521,7 @@ private static void testSpringMvcDefaultValuesJavaPrimitiveRest(RestTemplate tem
TestMgr.check("Hello false,\0,0,0,0,0,0.0,0.0,null", result);
}

private static void testSpringMvcDefaultValuesJavaPrimitiveAllTransport(RestTemplate template) {
private static void testSpringMvcDefaultValuesJavaPrimitiveAllTransport(RestOperations template) {
String microserviceName = "jaxrs";
for (String transport : DemoConst.transports) {
CategorizedTestCaseRunner.changeTransport(microserviceName, transport);
Expand Down
Loading

0 comments on commit 916a84d

Please sign in to comment.