нет конструктора аргумента строки / метода фабрики для десериализации из значения строки ('4.01') - PullRequest
0 голосов
/ 05 апреля 2019

Я пытаюсь получить ответ от Goodreads rest API, который возвращает xml в мое весеннее загрузочное приложение.

Ответ 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>

В то время как ответ должен быть направлен в ПОЖО, я получаю исключение.com.fasterxml.jackson.databind.exc.MismatchedInputException.

Методы invoker выглядят так, как показано ниже.

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])

Это проблема с jackson-databind?

Я пробовал то же самое, используя 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()));
...