How to Congifure Dynamic port in spring boot Microservice
STEP 1; POJO Class
public class ExchangeValue {
private Long id;
private String from;
private String to;
private BigDecimal conversionMultiple;
private int port;
public ExchangeValue() {
}
public ExchangeValue(Long id, String from, String to, BigDecimal conversionMultiple) {
super();
this.id = id;
this.from = from;
this.to = to;
this.conversionMultiple = conversionMultiple;
}
public Long getId() {
return id;
}
public String getFrom() {
return from;
}
public String getTo() {
return to;
}
public BigDecimal getConversionMultiple() {
return conversionMultiple;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
STEP 2; Controller Class
@RestController
public class CurrencyExchangeController {
@Autowired
private Environment environment;
@GetMapping("currency-exchange/from/{from}/to/{to}")
public ExchangeValue retrieveExchangeValue(@PathVariable String from, @PathVariable String to) {
ExchangeValue exchangeValue = new ExchangeValue(1L, from, to, BigDecimal.valueOf(81));
exchangeValue.setPort(Integer.parseInt(environment.getProperty("local.server.port")));
return exchangeValue;
}
}
STEP 3: Main Class
@SpringBootApplication
public class CurrencyExchangeServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CurrencyExchangeServiceApplication.class, args);
}
}
STEP 4: Application properties file
Comments
Post a Comment