我想用Spring Boot消费来自FishBase的Rest Api:
https://fishbase.ropensci.org/sealifebase/comnames?limit=1&offset=0
我使用以下类:
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 + '\'' +
'}';
}
}
引用
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());
};
}
}
我的结果是:
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'}
内部JSON-Object“数据”只有NULL-VALUES。为什么呢?
最佳regardes
Lorem ipsum dolor sit amet,consetetur sadipscing elitr,sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,sed diam voluptua。在vero eos et accusam et justo duo dolores et ea rebum。
答案 0 :(得分:0)
尝试@JsonProperty
强制进行正确的属性拼写。
例如,如果你有像getConName
这样的getter - 那么预期的JSON字段是conName
...要检查 - 请尝试:
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
public class InComnames {
@Id
private int autoctr;
@JsonProperty("ConName")
private String ComName;
如果可以 - 修复其他字段。