Springboot-将JSON转换为JAVA

时间:2018-11-16 16:40:48

标签: json spring-boot resttemplate

我需要从rest API获取货币汇率到我的spring boot应用程序中,并将JSON响应转换为Java类。 API将给我以下响应:-

{
    "USD_INR": {
        "val": 71.884966
    }
}

代码如下所示:

@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;
  }
}

执行REST调用后,我得到了null异常。什么也没退。我无法在这里找出问题。请帮助

3 个答案:

答案 0 :(得分:2)

您可以使用jsonscheme2pojo轻松地从json生成Java bean。

JSON:

{
"USD_INR": {
    "val": 71.884966
}

}

对应的Java bean:

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"USD_INR"
})
public class Example implements Serializable
{

@JsonProperty("USD_INR")
private USDINR uSDINR;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
private final static long serialVersionUID = -932753391825750904L;

@JsonProperty("USD_INR")
public USDINR getUSDINR() {
return uSDINR;
}

@JsonProperty("USD_INR")
public void setUSDINR(USDINR uSDINR) {
this.uSDINR = uSDINR;
}

public Example withUSDINR(USDINR uSDINR) {
this.uSDINR = uSDINR;
return this;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

public Example withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}

}
-----------------------------------com.example.USDINR.java-----------------------------------

package com.example;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"val"
})
public class USDINR implements Serializable
{

@JsonProperty("val")
private Double val;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
private final static long serialVersionUID = -6384976748235176082L;

@JsonProperty("val")
public Double getVal() {
return val;
}

@JsonProperty("val")
public void setVal(Double val) {
this.val = val;
}

public USDINR withVal(Double val) {
this.val = val;
return this;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

public USDINR withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}

}

答案 1 :(得分:1)

您的ExchangeRate类应为:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ExchangeRate {
    @JsonProperty("USD_INR")
    Source_Target SourceTargetObject;

    public Source_Target getSource_Target() {
        return SourceTargetObject;
    }

    public void setSource_Target(Source_Target SourceTargetObject) {
        this.SourceTargetObject = SourceTargetObject;
    }

然后您的Source_Target类应该是:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Source_Target {
    @JsonProperty("val")
    private float val;

    public float getVal() {
         return val;
    }

    public void setVal(float val) {
        this.val = val;
    }
}

请注意您的类命名约定。我将Source_Target重命名为SourceTarget以遵循Java命名约定

答案 2 :(得分:0)

您创建自定义地图:

public class ExchangeRate extends HashMap<String, ExchangeRateValue> implements Serializable {        
}

public class ExchangeRateValue implements Serializable {

    private float val;

    public float getVal() {
        return val;
    }

    public void setVal(float val) {
        this.val = val;
    }
}

并命名为:

 ExchangeRate exchangeRate = restTemplate.getForObject("https://free.currencyconverterapi.com/api/v6/convert?q=Source_Target&compact=y", ExchangeRate.class);
 float data = exchangeRate.get("USD_INR").getVal();