Настройте интерфейс httpexchange для использования с Springboot ⇐ JAVA
-
Anonymous
Настройте интерфейс httpexchange для использования с Springboot
Чего я пытаюсь достичь: < /li>
< /ul>
Я хотел бы иметь возможность настроить через приложение. Внешние сервисы, которые обнажают конечную точку REST.
Я не владею ни одной из двух сервисов.
Две службы даже не знают друг о друге. /> на http://some-gold-price-service.com/getprice
@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");
}
}
< /code>
Вопрос: < /li>
< /ul>
На данный момент, когда нам нужна служба, чтобы получить цену для золота, мы делаем ужасную вещь комментирования другой строки // map priceresponse = spocpricehtpexchange.getprice () < /code -inporture orpricerure the orplure и urplure orpruerure the orblir the orpruerure the orplirre urplired the orpruerure the orpruerure the orpruerure the orpricerure the orpricerure orpricerure orpricerure. Применение свойства на http://some-gold-price-service.com/getprice , чтобы затем полностью развернуть приложение. Настройте приложение.
@Autowired
GoldPriceHttpExchange goldPriceHttpExchange;
@Autowired
StockPriceHttpExchange stockPriceHttpExchange;
public String getSomePriceDependingOnConfiguration() {
Map priceResponse = {get either goldPriceHttpExchange bean or stockPriceHttpExchange bean based on a property}.getPrice();
return priceResponse.get("price");
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... springboot
Чего я пытаюсь достичь: < /li>
< /ul>
Я хотел бы иметь возможность настроить через приложение. Внешние сервисы, которые обнажают конечную точку REST.
Я не владею ни одной из двух сервисов.
Две службы даже не знают друг о друге. /> на http://some-gold-price-service.com/getprice
@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");
}
}
< /code>
Вопрос: < /li>
< /ul>
На данный момент, когда нам нужна служба, чтобы получить цену для золота, мы делаем ужасную вещь комментирования другой строки // map priceresponse = spocpricehtpexchange.getprice () < /code -inporture orpricerure the orplure и urplure orpruerure the orblir the orpruerure the orplirre urplired the orpruerure the orpruerure the orpruerure the orpricerure the orpricerure orpricerure orpricerure. Применение свойства на http://some-gold-price-service.com/getprice , чтобы затем полностью развернуть приложение. Настройте приложение.
@Autowired
GoldPriceHttpExchange goldPriceHttpExchange;
@Autowired
StockPriceHttpExchange stockPriceHttpExchange;
public String getSomePriceDependingOnConfiguration() {
Map priceResponse = {get either goldPriceHttpExchange bean or stockPriceHttpExchange bean based on a property}.getPrice();
return priceResponse.get("price");
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... springboot