From 5becc065354de1119adccd41094c2104191591b3 Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Thu, 21 Feb 2019 01:42:54 +0800 Subject: [PATCH] Polish spring-cloud-incubator/spring-cloud-alibaba#348 : Update test cases --- .../util/HttpMessageConverterResolver.java | 16 +- .../bootstrap/DubboSpringCloudBootstrap.java | 74 ++++++---- .../dubbo/metadata/RequestMetadataTest.java | 2 +- .../dubbo/service/DefaultEchoService.java | 99 ------------- .../{EchoService.java => RestService.java} | 12 +- .../dubbo/service/StandardRestService.java | 137 ++++++++++++++++++ .../cloud/alibaba/dubbo/service/User.java | 68 +++++++++ 7 files changed, 273 insertions(+), 135 deletions(-) delete mode 100644 spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/DefaultEchoService.java rename spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/{EchoService.java => RestService.java} (81%) create mode 100644 spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/StandardRestService.java create mode 100644 spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/User.java diff --git a/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/http/util/HttpMessageConverterResolver.java b/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/http/util/HttpMessageConverterResolver.java index 4e549654..bf2fa95f 100644 --- a/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/http/util/HttpMessageConverterResolver.java +++ b/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/http/util/HttpMessageConverterResolver.java @@ -23,6 +23,7 @@ import org.springframework.core.MethodParameter; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRequest; import org.springframework.http.MediaType; +import org.springframework.http.converter.GenericHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.http.server.ServletServerHttpResponse; @@ -71,10 +72,19 @@ public class HttpMessageConverterResolver { } for (HttpMessageConverter converter : this.messageConverters) { - if (converter.canRead(parameterType, contentType)) { - httpMessageConverterHolder = new HttpMessageConverterHolder(contentType, converter); - break; + if (converter instanceof GenericHttpMessageConverter) { + GenericHttpMessageConverter genericConverter = (GenericHttpMessageConverter) converter; + if (genericConverter.canRead(parameterType, parameterType, contentType)) { + httpMessageConverterHolder = new HttpMessageConverterHolder(contentType, converter); + break; + } + } else { + if (converter.canRead(parameterType, contentType)) { + httpMessageConverterHolder = new HttpMessageConverterHolder(contentType, converter); + break; + } } + } return httpMessageConverterHolder; diff --git a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/bootstrap/DubboSpringCloudBootstrap.java b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/bootstrap/DubboSpringCloudBootstrap.java index b986f753..1516b8f3 100644 --- a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/bootstrap/DubboSpringCloudBootstrap.java +++ b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/bootstrap/DubboSpringCloudBootstrap.java @@ -23,7 +23,8 @@ import org.springframework.boot.ApplicationRunner; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.alibaba.dubbo.annotation.DubboTransported; -import org.springframework.cloud.alibaba.dubbo.service.EchoService; +import org.springframework.cloud.alibaba.dubbo.service.RestService; +import org.springframework.cloud.alibaba.dubbo.service.User; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.EnableFeignClients; @@ -32,6 +33,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Lazy; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @@ -39,9 +41,6 @@ import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; -import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE; -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; - /** * Dubbo Spring Cloud Bootstrap */ @@ -52,15 +51,15 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; public class DubboSpringCloudBootstrap { @Reference(version = "1.0.0") - private EchoService echoService; + private RestService restService; @Autowired @Lazy - private FeignEchoService feignEchoService; + private FeignRestService feignRestService; @Autowired @Lazy - private DubboFeignEchoService dubboFeignEchoService; + private DubboFeignRestService dubboFeignRestService; @Autowired @LoadBalanced @@ -68,59 +67,80 @@ public class DubboSpringCloudBootstrap { @GetMapping(value = "/dubbo/call/echo") public String dubboEcho(@RequestParam("message") String message) { - return echoService.echo(message); + return restService.param(message); } @GetMapping(value = "/feign/call/echo") public String feignEcho(@RequestParam("message") String message) { - return feignEchoService.echo(message); + return feignRestService.param(message); } @GetMapping(value = "/feign-dubbo/call/echo") public String feignDubboEcho(@RequestParam("message") String message) { - return dubboFeignEchoService.echo(message); + return dubboFeignRestService.param(message); } @FeignClient("spring-cloud-alibaba-dubbo") - public interface FeignEchoService { + public interface FeignRestService { - @GetMapping(value = "/echo", consumes = APPLICATION_JSON_VALUE) - String echo(@RequestParam("message") String message); + @GetMapping(value = "/param") + String param(@RequestParam("param") String param); + + @PostMapping("/params") + public int params(@RequestParam int a, @RequestParam int b); } @FeignClient("spring-cloud-alibaba-dubbo") @DubboTransported - public interface DubboFeignEchoService { + public interface DubboFeignRestService { - @GetMapping(value = "/echo", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_UTF8_VALUE) - String echo(@RequestParam("message") String message); + @GetMapping(value = "/param") + String param(@RequestParam("param") String param); + + @PostMapping("/params") + public int params(@RequestParam int a, @RequestParam int b); } @Bean - public ApplicationRunner applicationRunner() { + public ApplicationRunner paramRunner() { return arguments -> { + + // To call /param // Dubbo Service call - System.out.println(echoService.echo("mercyblitz")); - // Spring Cloud Open Feign REST Call - System.out.println(feignEchoService.echo("mercyblitz")); + System.out.println(restService.param("mercyblitz")); // Spring Cloud Open Feign REST Call (Dubbo Transported) - System.out.println(dubboFeignEchoService.echo("mercyblitz")); + System.out.println(dubboFeignRestService.param("mercyblitz")); + // Spring Cloud Open Feign REST Call + System.out.println(feignRestService.param("mercyblitz")); + + // To call /params + // Dubbo Service call + System.out.println(restService.params(1, 1)); + // Spring Cloud Open Feign REST Call (Dubbo Transported) + System.out.println(dubboFeignRestService.params(1, 1)); + // Spring Cloud Open Feign REST Call + System.out.println(feignRestService.params(1, 1)); }; } @Bean public ApplicationRunner restTemplateRunner() { return arguments -> { - ResponseEntity entity = restTemplate.getForEntity("http://spring-cloud-alibaba-dubbo/echo?message=小马哥", String.class); - System.out.println(entity.getHeaders()); - System.out.println(entity.getBody()); -// Still issue + + ResponseEntity entity = restTemplate.getForEntity("http://spring-cloud-alibaba-dubbo/param?param=小马哥", String.class); + System.out.println(entity); + Map data = new HashMap<>(); + data.put("id", 1); data.put("name", "小马哥"); data.put("age", 33); - data.put("height", 173); - System.out.println(restTemplate.postForEntity("http://spring-cloud-alibaba-dubbo/toString", data, String.class)); + User user = restTemplate.postForObject("http://spring-cloud-alibaba-dubbo/request/body/map", data, User.class); + + System.out.println(restTemplate.postForObject("http://spring-cloud-alibaba-dubbo/request/body/map", data, String.class)); + + Map map = restTemplate.postForObject("http://spring-cloud-alibaba-dubbo/request/body/user", user, Map.class); + System.out.println(map); }; } diff --git a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/metadata/RequestMetadataTest.java b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/metadata/RequestMetadataTest.java index 704964e7..d84e5fe3 100644 --- a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/metadata/RequestMetadataTest.java +++ b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/metadata/RequestMetadataTest.java @@ -32,7 +32,7 @@ public class RequestMetadataTest { private String method = "GET"; - private String url = "/echo"; + private String url = "/param"; private Set paramNames = new LinkedHashSet<>(Arrays.asList("a", "b", "c")); diff --git a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/DefaultEchoService.java b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/DefaultEchoService.java deleted file mode 100644 index b75a01a9..00000000 --- a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/DefaultEchoService.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.springframework.cloud.alibaba.dubbo.service; - -import com.alibaba.dubbo.config.annotation.Service; -import com.alibaba.dubbo.rpc.RpcContext; - -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.RequestHeader; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import javax.ws.rs.FormParam; -import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import java.util.Map; - -import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE; - -/** - * Default {@link EchoService} - * - * @author Mercy - */ -@Service(version = "1.0.0", protocol = {"dubbo", "rest"}) -@RestController -@Path("/") -public class DefaultEchoService implements EchoService { - - @Override - @GetMapping(value = "/echo", produces = APPLICATION_JSON_UTF8_VALUE) - @Path("/echo") - @GET - @Produces(APPLICATION_JSON_UTF8_VALUE) - public String echo(@RequestParam @QueryParam("message") String message) { - return RpcContext.getContext().getUrl() + " [echo] : " + message; - } - - @Override - @PostMapping("/plus") - @Path("/plus") - @POST - public String plus(@RequestParam @QueryParam("a") int a, @RequestParam @QueryParam("b") int b) { - return String.valueOf(a + b); - } - - @Override - @PostMapping("/toString") - @Path("/toString") - @POST - public String toString(@RequestBody Map data) { - return data.toString(); - } - - @Override - @GetMapping("/header") - @Path("/header") - @GET - public String header(@RequestHeader("h") @HeaderParam("h") String header) { - return String.valueOf(header); - } - - @Override - @PostMapping("/form") - @Path("/form") - @POST - public String form(@RequestParam("f") @FormParam("f") String form) { - return String.valueOf(form); - } - - @Override - @GetMapping("/paramAndHeader") - @Path("/paramAndHeader") - @GET - public String paramAndHeader(@RequestHeader("h") @HeaderParam("h") @RequestParam("p") @QueryParam("p") String param, - @RequestHeader("h") @HeaderParam("h") String header) { - return param + header; - } -} diff --git a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/EchoService.java b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/RestService.java similarity index 81% rename from spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/EchoService.java rename to spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/RestService.java index a4d04635..b2842811 100644 --- a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/EchoService.java +++ b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/RestService.java @@ -23,17 +23,19 @@ import java.util.Map; * * @author Mercy */ -public interface EchoService { +public interface RestService { - String echo(String message); + String param(String message); - String plus(int a, int b); + int params(int a, int b); - String toString(Map data); + User requestBody(Map data); + + Map requestBody(User user); String header(String header); String form(String form); - String paramAndHeader(String param, String header); + String cookie(String userAgent); } diff --git a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/StandardRestService.java b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/StandardRestService.java new file mode 100644 index 00000000..a4f3ee67 --- /dev/null +++ b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/StandardRestService.java @@ -0,0 +1,137 @@ +/* + * 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.springframework.cloud.alibaba.dubbo.service; + +import com.alibaba.dubbo.rpc.RpcContext; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.CookieValue; +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.RequestHeader; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.ws.rs.Consumes; +import javax.ws.rs.CookieParam; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import java.util.HashMap; +import java.util.Map; + +import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE; +import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE; + +/** + * Default {@link RestService} + * + * @author Mercy + */ +@com.alibaba.dubbo.config.annotation.Service(version = "1.0.0", protocol = {"dubbo", "rest"}) +@RestController +@Path("/") +public class StandardRestService implements RestService { + + private Logger logger = LoggerFactory.getLogger(getClass()); + + @Override + @GetMapping(value = "/param") + @Path("/param") + @GET + public String param(@RequestParam @QueryParam("param") String param) { + log("/param", param); + return param; + } + + @Override + @PostMapping("/params") + @Path("/params") + @POST + public int params(@RequestParam @QueryParam("a") int a, @RequestParam @QueryParam("b") int b) { + log("/params", a + b); + return a + b; + } + + @Override + @GetMapping("/header") + @Path("/header") + @GET + public String header(@RequestHeader("h") @HeaderParam("h") String header) { + return String.valueOf(header); + } + + @Override + @PostMapping("/form") + @Path("/form") + @POST + public String form(@RequestParam("f") @FormParam("f") String form) { + return String.valueOf(form); + } + + @Override + @PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE) + @Path("/request/body/map") + @POST + @Produces(APPLICATION_JSON_VALUE) + public User requestBody(@RequestBody Map data) { + User user = new User(); + user.setId(((Integer) data.get("id")).longValue()); + user.setName((String) data.get("name")); + user.setAge((Integer) data.get("age")); + return user; + } + + @PostMapping(value = "/request/body/user", consumes = APPLICATION_JSON_UTF8_VALUE) + @Path("/request/body/user") + @POST + @Override + @Consumes(APPLICATION_JSON_UTF8_VALUE) + public Map requestBody(@RequestBody User user) { + Map map = new HashMap<>(); + map.put("id", user.getId()); + map.put("name", user.getName()); + map.put("age", user.getAge()); + return map; + } + + @Override + @GetMapping("/cookie") + @Path("/cookie") + @GET + public String cookie(@CookieParam("User-Agent") @CookieValue("User-Agent") String userAgent) { + return userAgent; + } + + private void log(String url, Object result) { + String message = String.format("The client[%s] uses '%s' protocol to call %s : %s", + RpcContext.getContext().getRemoteHostName(), + RpcContext.getContext().getUrl() == null ? "N/A" : RpcContext.getContext().getUrl().getProtocol(), + url, + result + ); + if (logger.isInfoEnabled()) { + logger.info(message); + } + } +} diff --git a/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/User.java b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/User.java new file mode 100644 index 00000000..79d54107 --- /dev/null +++ b/spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/User.java @@ -0,0 +1,68 @@ +/* + * 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.springframework.cloud.alibaba.dubbo.service; + +import javax.ws.rs.FormParam; +import java.io.Serializable; + +/** + * User Entity + */ +public class User implements Serializable { + + @FormParam("id") + private Long id; + + @FormParam("name") + private String name; + + @FormParam("age") + private Integer age; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + ", age=" + age + + '}'; + } +}