Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringDataRESTV3.x中的PersistentEntityResourceHandlerMethodArgumentResolver中找不到自定义转换器_Java_Spring Boot_Spring Data Rest - Fatal编程技术网

Java 在SpringDataRESTV3.x中的PersistentEntityResourceHandlerMethodArgumentResolver中找不到自定义转换器

Java 在SpringDataRESTV3.x中的PersistentEntityResourceHandlerMethodArgumentResolver中找不到自定义转换器,java,spring-boot,spring-data-rest,Java,Spring Boot,Spring Data Rest,从v1升级到Spring Boot v2后,在Spring数据Rest中的PersistentEntityResourceHandlerMethodArgumentResolver中找不到密钥对象的自定义转换器 在配置中添加了转换器(即在Boot v1下运行的旧代码): 现在,在尝试更新实体的过程中,在Boot v2下,转换器被成功调用一次,然后在此处最终失败: org.springframework.core.convert.ConverterNotFoundException: No con

从v1升级到Spring Boot v2后,在Spring数据Rest中的PersistentEntityResourceHandlerMethodArgumentResolver中找不到密钥对象的自定义转换器

在配置中添加了转换器(即在Boot v1下运行的旧代码):

现在,在尝试更新实体的过程中,在Boot v2下,转换器被成功调用一次,然后在此处最终失败:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.example.persistence.AccountKey]
    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.GenericConversionService.convert(GenericConversionService.java:174)
    at org.springframework.data.mapping.model.ConvertingPropertyAccessor.convertIfNecessary(ConvertingPropertyAccessor.java:123)
    at org.springframework.data.mapping.model.ConvertingPropertyAccessor.setProperty(ConvertingPropertyAccessor.java:61)
    at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.lambda$resolveArgument$3(PersistentEntityResourceHandlerMethodArgumentResolver.java:149)
    at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver$$Lambda$1170/0x0000000014b99e20.accept(Unknown Source)
    at java.util.Optional.ifPresent(Optional.java:159)
    at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:146)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:663) [11 skipped]
在BootV1(SpringDataRESTV2.6.x)中,第二部分很好。升级到SpringDataRESTV3.1.x后,它开始失败

似乎在PersistentEntityResourceHandlerMethodArgumentResolver中添加了默认转换服务:

private final ConversionService conversionService = new DefaultConversionService();
在PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument中:

            id.ifPresent(it -> {
                ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(obj),
                        conversionService);
                accessor.setProperty(entity.getRequiredIdProperty(), it);
            });
DefaultConversionService是使用一组硬编码的转换器创建的。我不知道如何添加我自己的转换器。有一个共享实例,我想我可以像下面这样自定义,但上面没有在PersistentEntityResourceHandlerMethodArgumentResolver中使用它:

@Component
public class DefaultConversionServiceInitializer implements InitializingBean {

    @Autowired
    public AccountKeyConverter accountKeyConverter;

    @Override
    public void afterPropertiesSet() {
        DefaultConversionService conversionService = (DefaultConversionService) DefaultConversionService.getSharedInstance();
        conversionService.addConverter(accountKeyConverter);
    }
非常感谢您的帮助

@Component
public class DefaultConversionServiceInitializer implements InitializingBean {

    @Autowired
    public AccountKeyConverter accountKeyConverter;

    @Override
    public void afterPropertiesSet() {
        DefaultConversionService conversionService = (DefaultConversionService) DefaultConversionService.getSharedInstance();
        conversionService.addConverter(accountKeyConverter);
    }