Create New Microservice CurrencyConverser
List of Dependencied for Createing Currency Conversion Service
user same Group as com.khan.microserservices
- Web
- DevTools
- Actuator
- Config Client
Also configure application.properties
spring.application.name=currency-conversion-service
server.port=8100
Make sure to add
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
in pom.xml to avoid the spring.config.import error
With Currency Calculation we have to calculate the FROM to To like USD to INR and convert
@RestController
public class CurrencyConversionController {
@GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversionBean convertCurrency(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity) {
return new CurrencyConversionBean(1L, from, to, BigDecimal.ONE, quantity, quantity);
}
}
public class CurrencyConversionBean {
private Long id;
private String from;
private String to;
private BigDecimal conversionMultiple;
private BigDecimal quantity;
private BigDecimal totalCalculatedAmount;
private int port;
//Getter Setter
//Constructore argument and without argument
}
To get the Currency Converstion through Currency
Exchange Service
@RestController
public class CurrencyConversionController {
@GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversionBean convertCurrency(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity) {
Map<String, String> uriVariables = new HashMap<String, String>();
uriVariables.put("from", from);
uriVariables.put("to", to);
ResponseEntity<CurrencyConversionBean> responseEntity = new RestTemplate()
.getForEntity("http://localhost:8000/currency-exchange/from/{from}/to/{to}",
CurrencyConversionBean.class,
uriVariables);
CurrencyConversionBean response = responseEntity.getBody();
return new CurrencyConversionBean(response.getId(), from, to, response.getConversionMultiple(), quantity,
quantity.multiply(response.getConversionMultiple()), response.getPort());
}
}
Comments
Post a Comment