I’m playing around with spring boot trying to consume a third parties rest api.
The API call I’m using returns the below JSON object.
{"success":true,"terms":"https://coinlayer.com/terms","privacy":"https://coinlayer.com/privacy","timestamp":1645616586,"target":"USD","rates":{"BTC":39049.424242}}
My code successfully consumes this JSON object however it returns this.
LiveData{success='true'terms='https://coinlayer.com/terms'privacy='https://coinlayer.com/privacy'timestamp='1645619886'target='EUR'rates={BTC='null'}}
Note that rates={BTC=’null’} should be rates={BTC=’39049.424242′}. Any help is greatly appreciated.
Below is my code
LiveData.java
package com.example.consumingrest; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class LiveData { private Boolean success; private String terms; private String privacy; private Long timestamp; private String target; private Rates rates; public LiveData() { } public Boolean getSuccess() { return success; } public void setSuccess(Boolean success) { this.success = success; } public String getTerms() { return terms; } public void setTerms(String terms) { this.terms = terms; } public String getPrivacy() { return privacy; } public void setPrivacy(String privacy) { this.privacy = privacy; } public Long getTimestamp() { return timestamp; } public void setTimestamp(Long timestamp) { this.timestamp = timestamp; } public String getTarget() { return target; } public void setTarget(String target) { this.target = target; } public Rates getRates() { return rates; } public void setValue(Rates rates) { this.rates = rates; } @Override public String toString() { return "LiveData{" + "success='" + success + ''' + "terms='" + terms + ''' + "privacy='" + privacy + ''' + "timestamp='" + timestamp + ''' + "target='" + target + ''' + "rates=" + rates + '}'; } }
Rates.java
package com.example.consumingrest; import java.math.BigDecimal; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class Rates { private BigDecimal BTC; public Rates() { } public BigDecimal getBTC() { return this.BTC; } public void setId(BigDecimal BTC) { this.BTC = BTC; } @Override public String toString() { return "{" + "BTC='" + BTC + '''+ '}'; } }
ConsumingRest.java (main)
package com.example.consumingrest; import java.time.LocalDate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class ConsumingRestApplication { private static final Logger log = LoggerFactory.getLogger(ConsumingRestApplication.class); public static void main(String[] args) { SpringApplication.run(ConsumingRestApplication.class, args); } @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); } @Bean public CommandLineRunner run(RestTemplate restTemplate) throws Exception { return args -> { LiveData liveData = restTemplate.getForObject( "http://api.coinlayer.com/api/live?access_key=121a4df8b95fd5be872da3bad101cd73&target=EUR&symbols=BTC", LiveData.class); log.info(liveData.toString()); }; } }
Advertisement
Answer
You can user @JsonProperty annotation.
@JsonProperty(value = "BTC") BigDecimal btc;
it is not camel case that is the reason it is null also some fields can be problematic as well. It is better to write annotation to those attributes which is problematic.