Spring Boot - 将 JSON 转换为 JAVA
Posted
技术标签:
【中文标题】Spring Boot - 将 JSON 转换为 JAVA【英文标题】:Springboot - convert JSON to 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
堆栈跟踪发布到您的问题?
【参考方案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();
【讨论】:
以上是关于Spring Boot - 将 JSON 转换为 JAVA的主要内容,如果未能解决你的问题,请参考以下文章
将 RequestBody json 转换为对象 - Spring Boot
如何将嵌套的 JSON 对象转换为数组 Spring Boot?
将从 spring-boot 检索到的数据转换为 JSON 并在 Angular 前端获取它
如何在 Spring Boot 中将输入字符串转换为 json 字符串或 json 对象
Kotlin > Spring Boot > 使用路由器 DSL,POST 方法主体,无法将 JSON 转换为 POJO