【问题标题】:Springboot - convert JSON to JAVASpring Boot - 将 JSON 转换为 JAVA
【发布时间】:2019-04-19 21:21:49
【问题描述】:

我需要从 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 调用后,我得到空异常。什么都没有返回。我无法在这里弄清楚这个问题。请帮忙

【问题讨论】:

  • 您能否将确切的NullPointerException 堆栈跟踪发布到您的问题?

标签: json spring-boot resttemplate


【解决方案1】:

您可以使用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;
}

}

【讨论】:

  • @JsonProperty("USD_INR") - 不能这样给出,因为属性会随着不同的来源和目标货币而变化..
【解决方案2】:

您的 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 命名约定

【讨论】:

  • @JsonProperty("USD_INR") - 不能这样给出,因为属性会随着不同的来源和目标货币而变化..
【解决方案3】:

您创建一个自定义地图:

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-14
    • 2017-08-23
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2023-02-15
    • 2018-04-04
    • 2019-05-17
    相关资源
    最近更新 更多