I have a ShopMicroService, a CustomerMicroService and a CartMicroService.
The ShopMicroService should work as an API Gateway and should be able ton control every other service. They are connected and routed with Netflix Zuul.
I want to be able to call e.g. localhost:8080/list, and see the data from both CustomerMicroService and CartMicroService. But I'm also not able to return two methods in my ShopController. How can I work around this?
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();
}
I already tried to call just one method and use both methods, but it still only shows of course only the list I am returning.
Basically, when you make an API call the request handler
of your application will route incoming HTTPS requests to specific handlers methods of your controllers. Therefore, you cannot "return two methods".
But, if I understand you correctly you want to join two Lists and return them to the client - correct me if I am wrong :) For this you can use Stream API
that provides a concat
method. For example
@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;
}
But I would recommend to use a wrapper object to return two different object types instead of returning them into a single list. You may find trouble retrieving objects from a list which objects do not belong to the same implementation (casting and so forth)