[英]Spring Boot consuming Rest null-values
I'd like to consume the Rest Api from FishBase with Spring Boot: 我想通过Spring Boot来使用FishBase的Rest Api:
https://fishbase.ropensci.org/sealifebase/comnames?limit=1&offset=0 https://fishbase.ropensci.org/sealifebase/comnames?limit=1&offset=0
I use the following classes: 我使用以下类:
InComnames InComnames
package com.example.demo.Model.input;
import javax.persistence.Entity;
import javax.persistence.Id;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
public class InComnames {
@Id
private int autoctr;
private String ComName;
private String Transliteration;
private int StockCode;
private int SpecCode;
private String C_Code;
private String Language;
private String Script;
private String UnicodeText;
private String NameType;
private int PreferredName;
private int TradeName;
private int TradeNameRef;
private int ComNamesRefNo;
private int Misspelling;
private String Size;
private String Sex;
private String Language2;
private String Locality2;
private int Rank;
private String Remarks;
private String SecondWord;
private String ThirdWord;
private String FourthWord;
private int Entered;
private String DateEntered;
private int Modified;
private String DateModified;
private int Expert;
private String DateChecked;
private String Core;
private String modifier1;
private String modifier2;
private int CLOFFSCA;
private String E_Append;
private String E_DateAppend;
private String TS;
public InComnames() {
}
//Getter and Setter
@Override
public String toString() {
return "InComnames{" +
"autoctr=" + autoctr +
", ComName='" + ComName + '\'' +
", Transliteration='" + Transliteration + '\'' +
", StockCode=" + StockCode +
", SpecCode=" + SpecCode +
", C_Code='" + C_Code + '\'' +
", Language='" + Language + '\'' +
", Script='" + Script + '\'' +
", UnicodeText='" + UnicodeText + '\'' +
", NameType='" + NameType + '\'' +
", PreferredName=" + PreferredName +
", TradeName=" + TradeName +
", TradeNameRef=" + TradeNameRef +
", ComNamesRefNo=" + ComNamesRefNo +
", Misspelling=" + Misspelling +
", Size='" + Size + '\'' +
", Sex='" + Sex + '\'' +
", Language2='" + Language2 + '\'' +
", Locality2='" + Locality2 + '\'' +
", Rank=" + Rank +
", Remarks='" + Remarks + '\'' +
", SecondWord='" + SecondWord + '\'' +
", ThirdWord='" + ThirdWord + '\'' +
", FourthWord='" + FourthWord + '\'' +
", Entered=" + Entered +
", DateEntered='" + DateEntered + '\'' +
", Modified=" + Modified +
", DateModified='" + DateModified + '\'' +
", Expert=" + Expert +
", DateChecked='" + DateChecked + '\'' +
", Core='" + Core + '\'' +
", modifier1='" + modifier1 + '\'' +
", modifier2='" + modifier2 + '\'' +
", CLOFFSCA=" + CLOFFSCA +
", E_Append='" + E_Append + '\'' +
", E_DateAppend='" + E_DateAppend + '\'' +
", TS='" + TS + '\'' +
'}';
}
}
Quote 引用
package com.example.demo.Model;
import com.example.demo.Model.input.InComnames;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {
private int count;
private int returned;
private List<InComnames> data;
private String error;
public Quote() {
}
//Getter and Setter
@Override
public String toString() {
return "Quote{" +
"count=" + count +
", returned=" + returned +
", data=" + data +
", error='" + error + '\'' +
'}';
}
}
DemoApplication 演示应用
package com.example.demo;
import com.example.demo.Model.Quote;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Quote quote = restTemplate.getForObject(
"https://fishbase.ropensci.org/sealifebase/comnames?limit=1&offset=0", Quote.class);
System.out.println(quote.toString());
};
}
}
My Result is : 我的结果是:
Quote{count=31138, returned=1, data=[InComnames{autoctr=40545, ComName='null', Transliteration='null', StockCode=0, SpecCode=0, C_Code='null', Language='null', Script='null', UnicodeText='null', NameType='null', PreferredName=0, TradeName=0, TradeNameRef=0, ComNamesRefNo=0, Misspelling=0, Size='null', Sex='null', Language2='null', Locality2='null', Rank=0, Remarks='null', SecondWord='null', ThirdWord='null', FourthWord='null', Entered=0, DateEntered='null', Modified=0, DateModified='null', Expert=0, DateChecked='null', Core='null', modifier1='null', modifier2='null', CLOFFSCA=0, E_Append='null', E_DateAppend='null', TS='null'}], error='null'}
The inner JSON-Object "data" have only NULL-VALUES. 内部JSON对象“数据”只有NULL值。 Why? 为什么?
Try @JsonProperty
to force right property spelling. 尝试@JsonProperty
强制正确的属性拼写。
For instance if you have getter like getConName
- then expected JSON field is conName
... To check - please try: 例如,如果您有类似getConName
则预期的JSON字段为conName
...要进行检查-请尝试:
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
public class InComnames {
@Id
private int autoctr;
@JsonProperty("ConName")
private String ComName;
If that is OK - fix other fields. 如果可以,请修复其他字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.