1. RestTemplate
@SpringBootApplication
public class CartServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CartServiceApplication.class, args);
System.out.println("cart启动成功");
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
private final RestTemplate restTemplate;
Set<Long> itemIds = vos.stream().map(CartVO::getItemId).collect(Collectors.toSet());
ResponseEntity<List<ItemDTO>> response = restTemplate.exchange(
"http://localhost:8081/items?ids={ids}",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<ItemDTO>>() {
},
Map.of("ids", CollUtil.join(itemIds, ","))
);
if (!response.getStatusCode().is2xxSuccessful()) {
return;
}
List<ItemDTO> items = response.getBody();