I need to obtain a currency exchange rate from a rest API into my spring boot application and convert the JSON response to Java class. The API will give me the response as below:-
{
"USD_INR": {
"val": 71.884966
}
}
The code is as shown below:
@RestController
public class MainController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello")
public float hello(){
ExchangeRate exchangeRate = restTemplate.getForObject("https://free.currencyconverterapi.com/api/v6/convert?q=Source_Target&compact=y",ExchangeRate.class);
return exchangeRate.getSource_Target().getVal();
}
}
-
@JsonIgnoreProperties(ignoreUnknown = true)
public class ExchangeRate {
Source_Target SourceTargetObject;
public Source_Target getSource_Target() {
return SourceTargetObject;
}
public void setSource_Target(Source_Target SourceTargetObject) {
this.SourceTargetObject = SourceTargetObject;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Source_Target {
private float val;
public float getVal() {
return val;
}
public void setVal(float val) {
this.val = val;
}
}
After executing the REST call, I am getting null exception. Nothing is returned. I am not able to figure out the issue here. Please help