【问题标题】:Combined Mapping with Spring Data JPA and Feign结合 Spring Data JPA 和 Feign 的映射
【发布时间】:2019-08-12 19:50:40
【问题描述】:

我有一个 ShopMicroService、一个 CustomerMicroService 和一个 CartMicroService

ShopMicroService 应该作为 API 网关工作,并且应该能够控制所有其他服务。它们通过 Netflix Zuul 连接和路由。

我希望能够打电话给例如localhost:8080/list,并查看来自 CustomerMicroService 和 CartMicroService 的数据。但我也无法在我的 ShopController 中返回两个方法。我该如何解决这个问题?

Shop2CartConnector:

@FeignClient("cartmicroservice")
public interface Shop2CartConnectorRequester {

    @GetMapping("/list")
    public List<?> getCart();

Shop2CustomerConnector:

@FeignClient("customermicroservice")
public interface Shop2CustomerConnectorRequester {

    @GetMapping("/list")
    public List<?> getCustomer();

ShopController:

@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();

    }

我已经尝试只调用一种方法并使用两种方法,但它仍然只显示我返回的列表。

【问题讨论】:

    标签: java spring-data-jpa netflix-eureka netflix-zuul spring-cloud-feign


    【解决方案1】:

    基本上,当您调用 API 时,应用程序的 request handler 会将传入的 HTTPS 请求路由到控制器的特定处理程序方法。因此,您不能“返回两个方法”。

    但是,如果我对您的理解正确,您想加入两个列表并将它们返回给客户端 - 如果我错了,请纠正我 :) 为此,您可以使用提供 concat 方法的 Stream API。例如

    @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;
       }
    

    但我建议使用包装器对象来返回两种不同的对象类型,而不是将它们返回到单个列表中。您可能会发现从列表中检索对象不属于同一实现(强制转换等)时遇到问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      • 2019-03-27
      • 2021-09-15
      • 2013-01-17
      相关资源
      最近更新 更多