首页 > 解决方案 > spring boot 版本升级后如何使用 Converter 修复异常

问题描述

我最近从 Spring Boot 版本 1.5.7 升级到了 2.1.4。升级后,我遇到了转换器异常,其中@Query 注释与弹簧数据一起使用。下面的异常堆栈跟踪。

您能否建议是否应更改任何依赖版本以解决此问题?还是需要编写客户转换器?

Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.HashMap<?, ?>] to type [@org.springframework.data.jpa.repository.Query com.example.Stage.XXXX]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194)
    at org.springframework.core.convert.support.CollectionToCollectionConverter.convert(CollectionToCollectionConverter.java:89)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
    ... 150 more

贴出的代码:

@RepositoryRestResource
public interface FileStagedEventRepository extends JpaRepository<FileStagedEvent, Long> {

@Query(value = "select new map(id as id, fileName as fileName, recType as recType) FROM FileStagedEvent where id = :id", nativeQuery = true)
    public Map<String, String> findById(@Param("id") Long id);  
}

select new map部分似乎是问题所在。我不确定这个地图功能是如何工作的。

标签: spring-bootspring-data

解决方案


推荐阅读