【问题标题】:IllegalArgumentException: Cannot deserialize instance ofIllegalArgumentException:无法反序列化
【发布时间】:2021-03-09 19:13:07
【问题描述】:

我有一个 Spring Boot 应用程序,其中有一个 BlogsService 调用一个博客存储库,它应该返回一个与传递的搜索查询匹配的博客列表。我得到以下由第 76 行引起的。有什么想法吗?谢谢

第 76 行:

return objectMapper.convertValue(searchEntity, BlogDetailsResponse.class);

IllegalArgumentException:无法从 START_ARRAY 令牌中反序列化 package.dto.BlogDetailsResponse 的实例 在 [来源:未知;行:-1,列:-1] 在 com.cor.devsquareawsservice.services.impl.BlogServiceImpl.searchBlogs(BlogServiceImpl.java:76) ~[classes/:na]

BlogServiceImpl.java

  @Override
  public BlogDetailsResponse searchBlogs(SearchBlogsRequest searchBlogsRequest) {
    try {
      final List<BlogEntity> searchEntity = blogsRepository.searchBlogs(searchBlogsRequest.getSearchBlogsQuery());
      return objectMapper.convertValue(searchEntity, BlogDetailsResponse.class);
    } catch (final Exception exception) {
      log.error(exception);
      throw new DevSquareDynamoDBException(ExceptionConstants.SERVICE_CREATE_BLOG_EXCEPTION_MESSAGE, exception);
    }
  }

BlogsRepository.java

  public List<BlogEntity> searchBlogs(String query) {
    try {
      DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
      scanExpression.addFilterCondition("title", new Condition()
              .withComparisonOperator(ComparisonOperator.CONTAINS)
              .withAttributeValueList(new AttributeValue().withS(query)));
      return dynamoDBMapper.scan(BlogEntity.class, scanExpression);
    } catch (Exception ex) {
      log.error("failed to get blogs > " + query);
    }
    return null;
  }

【问题讨论】:

  • searchEntity 是 BlogEntity 的列表。 BlogDetailsResponse 不是一个列表,是一个对象。请提供有关 BlogDetailsResponse 代码和 BlogEntity 的更多详细信息。

标签: java spring amazon-web-services spring-boot amazon-dynamodb


【解决方案1】:

这解决了我的问题。我需要映射列表并隐藏每个实体。

  public List<BlogDetailsResponse> searchBlogs(SearchBlogsRequest searchBlogsRequest) {
    try {
      return blogsRepository.searchBlogs(searchBlogsRequest.getSearchBlogsQuery()).stream().map(e -> objectMapper
              .convertValue(e, BlogDetailsResponse.class))
              .collect(Collectors.toList());
    } catch (final Exception exception) {
      log.error(exception);
      throw new DevSquareDynamoDBException(ExceptionConstants.SERVICE_SEARCH_BLOGS_EXCEPTION_MESSAGE, exception);
    }
  }

【讨论】:

    猜你喜欢
    • 2016-08-22
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 2021-11-08
    • 2019-02-26
    • 2018-07-03
    相关资源
    最近更新 更多