结合 Spring Data JPA 和 Feign 的映射
Combined Mapping with Spring Data JPA and Feign
我有一个 ShopMicroService、一个 CustomerMicroService 和一个 CartMicroService.
ShopMicroService 应该作为 API 网关工作,并且应该能够控制所有其他服务。它们与 Netflix Zuul 连接和路由。
我希望能够拨打例如localhost:8080/list,并查看来自 CustomerMicroService 和 CartMicroService 的数据。但是我也无法在我的 ShopController 中使用 return 两种方法。我该如何解决这个问题?
Shop2CartConnector:
@FeignClient("cartmicroservice")
public interface Shop2CartConnectorRequester {
@GetMapping("/list")
public List<?> getCart();
Shop2CustomerConnector:
@FeignClient("customermicroservice")
public interface Shop2CustomerConnectorRequester {
@GetMapping("/list")
public List<?> getCustomer();
店主:
@ComponentScan
@RestController
public class ShopController {
final Shop2CustomerConnectorRequester shop2CustomerConnectorRequester;
final Shop2CartConnectorRequester shop2CartConnectorRequester;
@Autowired
public ShopController(Shop2CustomerConnectorRequester shop2CustomerConnectorRequester,
Shop2CartConnectorRequester shop2CartConnectorRequester) {
this.shop2CustomerConnectorRequester = shop2CustomerConnectorRequester;
this.shop2CartConnectorRequester = shop2CartConnectorRequester;
}
@GetMapping("/getCustomer")
public List<?> getCustomer() {
return shop2CustomerConnectorRequester.getCustomer();
}
@GetMapping("/getCart")
public List<?> getCart() {
return shop2CartConnectorRequester.getCart();
}
我已经尝试过只调用一种方法并使用两种方法,但它当然仍然只显示我正在 returning 的列表。
基本上,当您进行 API 调用时,应用程序的 request handler
会将传入的 HTTPS 请求路由到控制器的特定处理程序方法。因此,你不能"return two methods"。
但是,如果我对你的理解正确的话,你想加入两个列表并将它们 return 发送给客户端 - 如果我错了请纠正我 :) 为此你可以使用 Stream API
它提供了一个concat
方法。例如
@RestController
public class ShopController {
final Shop2CustomerConnectorRequester shop2CustomerConnectorRequester;
final Shop2CartConnectorRequester shop2CartConnectorRequester;
@Autowired
public ShopController(Shop2CustomerConnectorRequester shop2CustomerConnectorRequester,
Shop2CartConnectorRequester shop2CartConnectorRequester) {
this.shop2CustomerConnectorRequester = shop2CustomerConnectorRequester;
this.shop2CartConnectorRequester = shop2CartConnectorRequester;
}
@GetMapping("/listAll")
public List getAllLists() {
List<Customer> customerList = hop2CustomerConnectorRequester.getCustomer();
List<Cart> cartList = hop2CartConnectorRequester.getCart();
List<?> list = Stream.concat(customerList.stream(), cartList.stream()).collect(Collectors.toList());
return list;
}
但我建议使用包装器对象 return 两种不同的对象类型,而不是 return 将它们合并到一个列表中。您可能会发现从列表中检索对象时遇到问题,这些对象不属于同一实现(转换等)
我有一个 ShopMicroService、一个 CustomerMicroService 和一个 CartMicroService.
ShopMicroService 应该作为 API 网关工作,并且应该能够控制所有其他服务。它们与 Netflix Zuul 连接和路由。
我希望能够拨打例如localhost:8080/list,并查看来自 CustomerMicroService 和 CartMicroService 的数据。但是我也无法在我的 ShopController 中使用 return 两种方法。我该如何解决这个问题?
Shop2CartConnector:
@FeignClient("cartmicroservice")
public interface Shop2CartConnectorRequester {
@GetMapping("/list")
public List<?> getCart();
Shop2CustomerConnector:
@FeignClient("customermicroservice")
public interface Shop2CustomerConnectorRequester {
@GetMapping("/list")
public List<?> getCustomer();
店主:
@ComponentScan
@RestController
public class ShopController {
final Shop2CustomerConnectorRequester shop2CustomerConnectorRequester;
final Shop2CartConnectorRequester shop2CartConnectorRequester;
@Autowired
public ShopController(Shop2CustomerConnectorRequester shop2CustomerConnectorRequester,
Shop2CartConnectorRequester shop2CartConnectorRequester) {
this.shop2CustomerConnectorRequester = shop2CustomerConnectorRequester;
this.shop2CartConnectorRequester = shop2CartConnectorRequester;
}
@GetMapping("/getCustomer")
public List<?> getCustomer() {
return shop2CustomerConnectorRequester.getCustomer();
}
@GetMapping("/getCart")
public List<?> getCart() {
return shop2CartConnectorRequester.getCart();
}
我已经尝试过只调用一种方法并使用两种方法,但它当然仍然只显示我正在 returning 的列表。
基本上,当您进行 API 调用时,应用程序的 request handler
会将传入的 HTTPS 请求路由到控制器的特定处理程序方法。因此,你不能"return two methods"。
但是,如果我对你的理解正确的话,你想加入两个列表并将它们 return 发送给客户端 - 如果我错了请纠正我 :) 为此你可以使用 Stream API
它提供了一个concat
方法。例如
@RestController
public class ShopController {
final Shop2CustomerConnectorRequester shop2CustomerConnectorRequester;
final Shop2CartConnectorRequester shop2CartConnectorRequester;
@Autowired
public ShopController(Shop2CustomerConnectorRequester shop2CustomerConnectorRequester,
Shop2CartConnectorRequester shop2CartConnectorRequester) {
this.shop2CustomerConnectorRequester = shop2CustomerConnectorRequester;
this.shop2CartConnectorRequester = shop2CartConnectorRequester;
}
@GetMapping("/listAll")
public List getAllLists() {
List<Customer> customerList = hop2CustomerConnectorRequester.getCustomer();
List<Cart> cartList = hop2CartConnectorRequester.getCart();
List<?> list = Stream.concat(customerList.stream(), cartList.stream()).collect(Collectors.toList());
return list;
}
但我建议使用包装器对象 return 两种不同的对象类型,而不是 return 将它们合并到一个列表中。您可能会发现从列表中检索对象时遇到问题,这些对象不属于同一实现(转换等)