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 : Add @PathVariable support
This commit is contained in:
@@ -33,14 +33,21 @@ 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.PathVariable;
|
||||
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 org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Bootstrap
|
||||
*/
|
||||
@@ -65,21 +72,6 @@ public class DubboSpringCloudBootstrap {
|
||||
@LoadBalanced
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@GetMapping(value = "/dubbo/call/echo")
|
||||
public String dubboEcho(@RequestParam("message") String message) {
|
||||
return restService.param(message);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/feign/call/echo")
|
||||
public String feignEcho(@RequestParam("message") String message) {
|
||||
return feignRestService.param(message);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/feign-dubbo/call/echo")
|
||||
public String feignDubboEcho(@RequestParam("message") String message) {
|
||||
return dubboFeignRestService.param(message);
|
||||
}
|
||||
|
||||
@FeignClient("spring-cloud-alibaba-dubbo")
|
||||
public interface FeignRestService {
|
||||
|
||||
@@ -89,6 +81,20 @@ public class DubboSpringCloudBootstrap {
|
||||
@PostMapping("/params")
|
||||
public String params(@RequestParam("b") String b, @RequestParam("a") int a);
|
||||
|
||||
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE)
|
||||
User requestBody(@RequestParam("param") String param, @RequestBody Map<String, Object> data);
|
||||
|
||||
@GetMapping("/headers")
|
||||
@Path("/headers")
|
||||
@GET
|
||||
public String headers(@RequestHeader("h2") String header2,
|
||||
@RequestHeader("h") String header,
|
||||
@RequestParam("v") Integer value);
|
||||
|
||||
@GetMapping("/path-variables/{p1}/{p2}")
|
||||
public String pathVariables(@PathVariable("p2") String path2,
|
||||
@PathVariable("p1") String path1,
|
||||
@RequestParam("v") String param);
|
||||
}
|
||||
|
||||
@FeignClient("spring-cloud-alibaba-dubbo")
|
||||
@@ -99,7 +105,22 @@ public class DubboSpringCloudBootstrap {
|
||||
String param(@RequestParam("param") String param);
|
||||
|
||||
@PostMapping("/params")
|
||||
public String params(@RequestParam("b") String paramB, @RequestParam("a") int paramA);
|
||||
String params(@RequestParam("b") String paramB, @RequestParam("a") int paramA);
|
||||
|
||||
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE)
|
||||
User requestBody(@RequestParam("param") String param, @RequestBody Map<String, Object> data);
|
||||
|
||||
@GetMapping("/headers")
|
||||
@Path("/headers")
|
||||
@GET
|
||||
public String headers(@RequestHeader("h2") String header2,
|
||||
@RequestParam("v") Integer value,
|
||||
@RequestHeader("h") String header);
|
||||
|
||||
@GetMapping("/path-variables/{p1}/{p2}")
|
||||
public String pathVariables(@RequestParam("v") String param,
|
||||
@PathVariable("p2") String path2,
|
||||
@PathVariable("p1") String path1);
|
||||
}
|
||||
|
||||
|
||||
@@ -107,24 +128,76 @@ public class DubboSpringCloudBootstrap {
|
||||
public ApplicationRunner paramRunner() {
|
||||
return arguments -> {
|
||||
|
||||
// To call /path-variables
|
||||
callPathVariables();
|
||||
|
||||
// To call /headers
|
||||
callHeaders();
|
||||
|
||||
// To call /param
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.param("mercyblitz"));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
System.out.println(dubboFeignRestService.param("mercyblitz"));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.param("mercyblitz"));
|
||||
callParam();
|
||||
|
||||
// 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));
|
||||
callParams();
|
||||
|
||||
// To call /request/body/map
|
||||
callRequestBodyMap();
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
private void callPathVariables() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.pathVariables("a", "b", "c"));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
System.out.println(dubboFeignRestService.pathVariables("c", "b", "a"));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.pathVariables("b", "a", "c"));
|
||||
}
|
||||
|
||||
private void callHeaders() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.headers("a", "b", 10));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
System.out.println(dubboFeignRestService.headers("b", 10, "a"));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.headers("b", "a", 10));
|
||||
}
|
||||
|
||||
private void callParam() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.param("mercyblitz"));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
System.out.println(dubboFeignRestService.param("mercyblitz"));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.param("mercyblitz"));
|
||||
}
|
||||
|
||||
private void callParams() {
|
||||
// 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));
|
||||
}
|
||||
|
||||
private void callRequestBodyMap() {
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("id", 1);
|
||||
data.put("name", "小马哥");
|
||||
data.put("age", 33);
|
||||
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.requestBody(data, "Hello,World"));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
System.out.println(dubboFeignRestService.requestBody("Hello,World", data));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.requestBody("Hello,World", data));
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner restTemplateRunner() {
|
||||
return arguments -> {
|
||||
@@ -136,11 +209,11 @@ public class DubboSpringCloudBootstrap {
|
||||
data.put("id", 1);
|
||||
data.put("name", "小马哥");
|
||||
data.put("age", 33);
|
||||
User user = restTemplate.postForObject("http://spring-cloud-alibaba-dubbo/request/setBody/map", data, User.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/setBody/map", data, String.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/setBody/user", user, Map.class);
|
||||
Map map = restTemplate.postForObject("http://spring-cloud-alibaba-dubbo/request/body/user", user, Map.class);
|
||||
System.out.println(map);
|
||||
};
|
||||
}
|
||||
|
@@ -25,17 +25,18 @@ import java.util.Map;
|
||||
*/
|
||||
public interface RestService {
|
||||
|
||||
String param(String message);
|
||||
String param(String param);
|
||||
|
||||
String params(int a, String b);
|
||||
|
||||
User requestBody(Map<String, Object> data);
|
||||
String headers(String header, String header2, Integer param);
|
||||
|
||||
Map<String, Object> requestBody(User user);
|
||||
|
||||
String header(String header);
|
||||
String pathVariables(String path1, String path2, String param);
|
||||
|
||||
String form(String form);
|
||||
|
||||
String cookie(String userAgent);
|
||||
User requestBody(Map<String, Object> data, String param);
|
||||
|
||||
Map<String, Object> requestBody(User user);
|
||||
|
||||
}
|
||||
|
@@ -20,8 +20,8 @@ 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.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
@@ -29,12 +29,12 @@ 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.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import java.util.HashMap;
|
||||
@@ -74,13 +74,32 @@ public class StandardRestService implements RestService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/header")
|
||||
@Path("/header")
|
||||
@GetMapping("/headers")
|
||||
@Path("/headers")
|
||||
@GET
|
||||
public String header(@RequestHeader("h") @HeaderParam("h") String header) {
|
||||
return String.valueOf(header);
|
||||
public String headers(@RequestHeader("h") @HeaderParam("h") String header,
|
||||
@RequestHeader("h2") @HeaderParam("h2") String header2,
|
||||
@RequestParam("v") @QueryParam("v") Integer param) {
|
||||
String result = header + " , " + header2 + " , " + param;
|
||||
log("/headers", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/path-variables/{p1}/{p2}")
|
||||
@Path("/path-variables/{p1}/{p2}")
|
||||
@GET
|
||||
public String pathVariables(@PathVariable("p1") @PathParam("p1") String path1,
|
||||
@PathVariable("p2") @PathParam("p2") String path2,
|
||||
@RequestParam("v") @QueryParam("v") String param) {
|
||||
String result = path1 + " , " + path2 + " , " + param;
|
||||
log("/path-variables", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// @CookieParam does not support : https://github.com/OpenFeign/feign/issues/913
|
||||
// @CookieValue also does not support
|
||||
|
||||
@Override
|
||||
@PostMapping("/form")
|
||||
@Path("/form")
|
||||
@@ -94,11 +113,12 @@ public class StandardRestService implements RestService {
|
||||
@Path("/request/setBody/map")
|
||||
@POST
|
||||
@Produces(APPLICATION_JSON_VALUE)
|
||||
public User requestBody(@RequestBody Map<String, Object> data) {
|
||||
public User requestBody(@RequestBody Map<String, Object> data, @RequestParam("param") @QueryParam("param") String param) {
|
||||
User user = new User();
|
||||
user.setId(((Integer) data.get("id")).longValue());
|
||||
user.setName((String) data.get("name"));
|
||||
user.setAge((Integer) data.get("age"));
|
||||
log("/request/body/map", param);
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -115,14 +135,6 @@ public class StandardRestService implements RestService {
|
||||
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(),
|
||||
|
@@ -7,6 +7,8 @@ spring:
|
||||
server-addr: 127.0.0.1:8848
|
||||
config:
|
||||
server-addr: 127.0.0.1:8848
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
eureka:
|
||||
client:
|
||||
|
Reference in New Issue
Block a user