我正在尝试从Goodreads rest API获取响应,该响应将xml返回到我的spring boot应用程序中。
响应xml如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<GoodreadsResponse>
<Request>
<authentication>true</authentication>
<key><![CDATA[zgSbC4m146z8vgHwwbLIg]]></key>
<method><![CDATA[search_index]]></method>
</Request>
<search>
<query><![CDATA[Salem Hates Sarah]]></query>
<results-start>1</results-start>
<results-end>1</results-end>
<total-results>1</total-results>
<source>Goodreads</source>
<query-time-seconds>0.04</query-time-seconds>
<results>
<work>
<id type="integer">68061845</id>
<books_count type="integer">1</books_count>
<ratings_count type="integer">5</ratings_count>
<text_reviews_count type="integer">3</text_reviews_count>
<original_publication_year type="integer" nil="true"/>
<original_publication_month type="integer" nil="true"/>
<original_publication_day type="integer" nil="true"/>
<average_rating>3.00</average_rating>
<best_book type="Book">
<id type="integer">43732286</id>
<title>Salem Hates Sarah</title>
<author>
<id type="integer">14202724</id>
<name>Simen Oem</name>
</author>
<image_url>https://images.gr-assets.com/books/1548944050m/43732286.jpg</image_url>
<small_image_url>https://images.gr-assets.com/books/1548944050s/43732286.jpg</small_image_url>
</best_book>
</work>
</results>
</search>
</GoodreadsResponse>
虽然需要将响应解组到pojo中,但我得到了一个例外。 com.fasterxml.jackson.databind.exc.MismatchedInputException.
调用方方法如下所示。
public List<Book> getBooksByTitle(String bookTitle) {
restTemplate = new RestTemplate();
List<Book> books = new ArrayList<Book>();
try {
GoodreadsResponse response = restTemplate.getForObject(SEARCH_URL, GoodreadsResponse.class);
for(Work work : response.getSearch().getResults().getWork()) {
books.add(new Book(work.getBestBook().getTitle(),work.getBestBook().getAuthor().getName()));
}
}catch (Exception e) {
System.out.println("exception while parsing" + e.getMessage());
}
return books;
}
整个堆栈跟踪如下:
解析时例外
Error while extracting response for type [class bbs.goodreads.api.schemas.search.GoodreadsResponse] and content type [application/xml;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `bbs.goodreads.api.schemas.search.Work` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('4.01'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `bbs.goodreads.api.schemas.search.Work` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('4.01')
at [Source: (PushbackInputStream); line: 24, column: 23] (through reference chain: bbs.goodreads.api.schemas.search.GoodreadsResponse["search"]->bbs.goodreads.api.schemas.search.Search["results"]->bbs.goodreads.api.schemas.search.Results["work"]->java.util.ArrayList[7])
杰克逊数据绑定有问题吗?
我已经使用jaxbInstance尝试了同样的方法,并且效果很好。
JAXBContext jaxbContext = JAXBContext.newInstance(GoodreadsResponse.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
goodreadsResponse = (GoodreadsResponse) unmarshaller.unmarshal(new URL(SEARCH_URL));
for(Work work : goodreadsResponse.getSearch().getResults().getWork()) {
books.add(new Book(work.getBestBook().getTitle(),work.getBestBook().getAuthor().getName()));