我正在使用 API 在 Java 上进行自我训练。我练习的主题是询问 google 图书 API 以获取有关图书的一些信息。我使用 Jersey 作为客户端,使用 Jackson 来解析 JSON。
我的问题是,当我运行时JerseyVolumeGet()
,响应是这个:
Volume{title='null', numberOfPages=null, author='null'}
为什么 ?我的错误在哪里?我怀疑我在解析 JSON 时出错了,但看不到确切的位置..
这是我的 getClass(搜索 url 带有条形码,对我来说不是问题)
public class JerseyVolumeGet {
public static void main(String[] args) {
try {
Client client = Client.create();
WebResource webResource = client
.resource("https://www.googleapis.com/books/v1/volumes?q=1984");
ClientResponse response = webResource.accept("application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Noob you Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
// read from file, convert it to user class
ObjectMapper mapper = new ObjectMapper();
Volume volume = mapper.readValue(output, Volume.class);
// display to console
System.out.println(volume);
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我查询的 JSON 结果:
{
"kind": "books#volumes",
"totalItems": 641,
"items": [
{
"kind": "books#volume",
"id": "RY8yQWeDVFYC",
"etag": "Lf0P50PVz9c",
"selfLink": "https://www.googleapis.com/books/v1/volumes/RY8yQWeDVFYC",
"volumeInfo": {
"title": "(1984).",
"subtitle": "",
"authors": [
"Guy Serbat",
"Jean Taillardat",
"Gilbert Lazard"
],
"publisher": "Peeters Publishers",
"publishedDate": "1984-01-01",
"description": "(Peeters 1984)",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "2904685030"
},
{
"type": "ISBN_13",
"identifier": "9782904685033"
}
],
"readingModes": {
"text": false,
"image": true
},
"pageCount": 280,
"printType": "BOOK",
"categories": [
"Language Arts & Disciplines"
],
"contentVersion": "1.1.1.0.preview.1",
"imageLinks": {
"smallThumbnail": "http://bks9.books.google.fr/books?id=RY8yQWeDVFYC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://bks9.books.google.fr/books?id=RY8yQWeDVFYC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "fr",
"previewLink": "http://books.google.fr/books?id=RY8yQWeDVFYC&printsec=frontcover&dq=1984&hl=&cd=1&source=gbs_api",
"infoLink": "http://books.google.fr/books?id=RY8yQWeDVFYC&dq=1984&hl=&source=gbs_api",
"canonicalVolumeLink": "http://books.google.fr/books/about/1984.html?hl=&id=RY8yQWeDVFYC"
},
"saleInfo": {
"country": "FR",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "FR",
"viewability": "PARTIAL",
"embeddable": true,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": false
},
"pdf": {
"isAvailable": false
},
"webReaderLink": "http://books.google.fr/books/reader?id=RY8yQWeDVFYC&hl=&printsec=frontcover&output=reader&source=gbs_api",
"accessViewStatus": "SAMPLE",
"quoteSharingAllowed": false
},
"searchInfo": {
"textSnippet": "(Peeters 1984)"
}
},
more items...
}
然后,我有一个Volume.class
这样的:
@JsonIgnoreProperties(ignoreUnknown=true)
public class Volume {
@JsonProperty("title")
String title;
@JsonProperty("pageCount")
Integer pageCount;
@JsonProperty("authors")
String authors;
getters , setters and toString..