Я хотел бы иметь возможность настраивать через Application.properties Spring @httpexchange интерфейс для использования
Существует две внешние службы, которые выявляют конечную точку REST. Я не владею ни одной из двух услуг, и эти две службы даже не знают друг о друге. />
@RestController
public class GoldPriceController {
@GetMapping("/getPrice")
public Map getGoldPrice() throws InterruptedException {
Thread.sleep(800);
System.out.println("getGoldPrice");
int randomNum = (int)(Math.random() * 101);
return Map.of("price", String.valueOf(randomNum));
}
}
at http://company-for-stock.co.uk/getprice
@RestController
public class StockPriceController {
@GetMapping("/getPrice")
public Map getStockPrice() throws InterruptedException {
Thread.sleep(800);
System.out.println("Get Stock Price");
int randomNum = (int)(Math.random() * 101);
return Map.of("price", String.valueOf(randomNum));
}
}
< /code>
Как вы можете видеть, не зная друг друга, кажется, что они предлагают ответ полезной нагрузки, который содержит цену, и аналогичная конечная точка /getPrice.@Configuration
public class RestClientConfig {
@Value("configuration.url")
String url;
@Bean
public RestClient customRestClient() {
return RestClient.builder()
.baseUrl(url)
.build();
}
}
< /code>
@Configuration
public class RestClientGoldConfiguration {
@Bean
public GoldPriceHttpExchange goldPriceHttpExchange(RestClient restClient) {
return HttpServiceProxyFactory.builderFor(RestClientAdapter.create(restClient)).build().createClient(GoldPriceHttpExchange.class);
}
}
< /code>
@Configuration
public class RestClientStockConfiguration {
@Bean
public StockPriceHttpExchange stockPriceHttpExchange(RestClient restClient) {
return HttpServiceProxyFactory.builderFor(RestClientAdapter.create(restClient)).build().createClient(StockPriceHttpExchange.class);
}
}
< /code>
Вот интересные биты, httpexchanges, которые, кстати, не принимают имя бобов.@HttpExchange(accept = "application/json")
public interface GoldPriceHttpExchange {
@GetExchange("/getPrice")
Map getPrice();
}
< /code>
@HttpExchange(accept = "application/json")
public interface StockPriceHttpExchange {
@GetExchange("/getPrice")
Map getPrice();
}
< /code>
и, наконец, служба с использованием двух httpexchange: < /p>
@Autowired
GoldPriceHttpExchange goldPriceHttpExchange;
@Autowired
StockPriceHttpExchange stockPriceHttpExchange;
public String getSomePriceDependingOnConfiguration() {
// Map priceResponse = goldPriceHttpExchange.getPrice();
Map priceResponse = stockPriceHttpExchange.getPrice();
return priceResponse.get("price");
}
На данный момент, когда нам нужна услуга, чтобы получить цену на золото, мы делаем ужасную вещь комментировать другую строку // map priceresponse = stockpricehttpexchange.getprice ();
Подробнее здесь: https://stackoverflow.com/questions/795 ... pring-boot