Spring分页和排序存储库-自定义转换器-未找到能够从类型转换为类型的转换器

时间:2019-01-08 19:31:28

标签: java spring type-conversion

我有这样的存储库

public interface ApartmentRepository
    extends PagingAndSortingRepository<Apartment, Long> {

    Page<LandlordPageApartment> findAllByOwnerId(long ownerId, Pageable pageable);

}

当我使用它时,会有一个例外org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [Apartment] to type [LandlordPageApartment]

在这里定义我的转换器

@Component
public class ApartmentToLandlordPageApartmentConverter implements Converter<Apartment, LandlordPageApartment> {

    @Override
    public LandlordPageApartment convert(Apartment apartment) {
        // some conversion
    }

}

转换器注册

@Configuration
@EnableWebMvc
public class AbsWebMvcConfigurer implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(apartmentToLandlordPageApartmentConverter());
    }

    @Bean
    public ApartmentToLandlordPageApartmentConverter
    apartmentToLandlordPageApartmentConverter() {
        return new ApartmentToLandlordPageApartmentConverter();
    }
// ...
}

我做错了什么?

0 个答案:

没有答案