gpt4 book ai didi

java - Spring Data Neo4j : ConversionFailedException: Failed to convert from type java. lang.String 为值键入字节

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:30 26 4
gpt4 key购买 nike

我尝试使用 Spring Data Neo4j 将图像存储为字节数组 (byte[]),并具有以下模型属性:

@GraphProperty(propertyType = byte[].class)
private byte[] profileImage;

保存和读取不起作用:

modelRepository.save(myObject)
myModel = modelRepository.findByNodeId(nodeId)

我收到以下转换异常:

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type byte for value ...
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) ~[spring-core-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.core.convert.support.StringToArrayConverter.convert(StringToArrayConverter.java:63) ~[spring-core-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35) ~[spring-core-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-4.0.7.RELEASE.jar:4.0.7.RELEASE]
Caused by: java.lang.NumberFormatException: For input string: ...
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.7.0_75]
at java.lang.Integer.parseInt(Integer.java:492) ~[na:1.7.0_75]
at java.lang.Byte.parseByte(Byte.java:148) ~[na:1.7.0_75]
at java.lang.Byte.valueOf(Byte.java:204) ~[na:1.7.0_75]
at java.lang.Byte.valueOf(Byte.java:230) ~[na:1.7.0_75]

似乎在内部,byte[] 首先被转换为 String,然后再次转换回 byte[]...

我不太明白那里到底发生了什么。

作为连接类,我在 Spring 应用程序配置 xml 文件中使用这个连接类:

org.springframework.data.neo4j.rest.SpringRestGraphDatabase

你有什么线索吗?

感谢您的帮助。

最佳答案

经过一些调查,我可以通过提供自定义转换器来解决该问题:

public class StringBase64ToByteArrayConverter implements Converter<String, byte[]> {

@Override
public byte[] convert(String source) {
return Base64.decodeBase64(source);
}
}

并使用此转换器注册 ConversionService:

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="my.converter.StringBase64ToByteArrayConverter"/>
</set>
</property>
</bean>

另请参阅 Spring 引用:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#core-convert

关于java - Spring Data Neo4j : ConversionFailedException: Failed to convert from type java. lang.String 为值键入字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28274979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com