mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish spring-cloud-incubator/spring-cloud-alibaba#348 : Update test cases
This commit is contained in:
parent
a49f6622d7
commit
5becc06535
@ -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;
|
||||
|
@ -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<String> 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<String> entity = restTemplate.getForEntity("http://spring-cloud-alibaba-dubbo/param?param=小马哥", String.class);
|
||||
System.out.println(entity);
|
||||
|
||||
Map<String, Object> 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);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ public class RequestMetadataTest {
|
||||
|
||||
private String method = "GET";
|
||||
|
||||
private String url = "/echo";
|
||||
private String url = "/param";
|
||||
|
||||
private Set<String> paramNames = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
|
||||
|
||||
|
@ -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 <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@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<String, Object> 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;
|
||||
}
|
||||
}
|
@ -23,17 +23,19 @@ import java.util.Map;
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
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<String, Object> data);
|
||||
User requestBody(Map<String, Object> data);
|
||||
|
||||
Map<String, Object> requestBody(User user);
|
||||
|
||||
String header(String header);
|
||||
|
||||
String form(String form);
|
||||
|
||||
String paramAndHeader(String param, String header);
|
||||
String cookie(String userAgent);
|
||||
}
|
@ -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 <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@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<String, Object> 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<String, Object> requestBody(@RequestBody User user) {
|
||||
Map<String, Object> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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 +
|
||||
'}';
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user