gpt4 book ai didi

java - 如何将存储库填充器与 spring 数据 -jpa 一起使用?

转载 作者:行者123 更新时间:2023-12-04 05:22:26 24 4
gpt4 key购买 nike

我正在使用 Spring Data JPA 并且我正在尝试使用存储库填充器来填充数据 - 但我得到了格式错误的 json 的异常。

我想可能是data.json文件的位置。我已将其放在 META-INF 文件夹中,并按如下方式配置测试用例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:META-INF/integration-test-context.xml","classpath:META-INF/sample-data.xml"})
public class JpaCompanyRepositoryIntegrationTest{

//Test cases here

}

我的示例-data.xml 是:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository.xsd">

<repository:jackson-populator location="classpath:data.json" />

</beans>

和 data.json 包含
[{"_class" : "com.some.Class",
"id" : "id1",
"firstname" : "Admin",
"lastname" : "admin"
},
{
"_class" : "com.some.Class",
"id" : "id2",
"firstname" : "user",
"lastname" : "user"
}]

我收到以下异常:
Caused by: java.lang.RuntimeException: org.codehaus.jackson.JsonParseException: Unexpected character ('c' (code 99)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.ByteArrayInputStream@61adabf3; line: 1, column: 2]
at org.springframework.data.repository.init.ResourceReaderRepositoryPopulator.readObjectFrom(ResourceReaderRepositoryPopulator.java:145)
at org.springframework.data.repository.init.ResourceReaderRepositoryPopulator.populate(ResourceReaderRepositoryPopulator.java:115)
at org.springframework.data.repository.init.AbstractRepositoryPopulatorFactoryBean.onApplicationEvent(AbstractRepositoryPopulatorFactoryBean.java:93)
at org.springframework.data.repository.init.AbstractRepositoryPopulatorFactoryBean.onApplicationEvent(AbstractRepositoryPopulatorFactoryBean.java:34)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:934)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
... 24 more
Caused by: org.codehaus.jackson.JsonParseException: Unexpected character ('c' (code 99)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.ByteArrayInputStream@61adabf3; line: 1, column: 2]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1432)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:306)
at org.codehaus.jackson.impl.Utf8StreamParser._handleUnexpectedValue(Utf8StreamParser.java:2084)
at org.codehaus.jackson.impl.Utf8StreamParser._nextTokenNotInObject(Utf8StreamParser.java:600)
at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:486)
at org.codehaus.jackson.map.ObjectReader._initForReading(ObjectReader.java:638)
at org.codehaus.jackson.map.ObjectReader._bindAsTree(ObjectReader.java:597)
at org.codehaus.jackson.map.ObjectReader._bindAndCloseAsTree(ObjectReader.java:621)
at org.codehaus.jackson.map.ObjectReader.readTree(ObjectReader.java:381)
at org.springframework.data.repository.init.JacksonResourceReader.readFrom(JacksonResourceReader.java:80)
at org.springframework.data.repository.init.ResourceReaderRepositoryPopulator.readObjectFrom(ResourceReaderRepositoryPopulator.java:143)
... 36 more

因此,我进行了更多调试,结果发现配置正在存储库填充器定义中查找“locations”属性——而 xml 需要“location”属性。我正在使用 Spring 数据 1.4.0.RELEASE 和 jackson 1.9.2。我是否混淆了依赖项?

最佳答案

这是当前版本的 Spring Data Commons 中的一个错误(有关详细信息,请参阅 DATACMNS-227)。这是蜜蜂fixed recently但由于修复需要更改命名空间,因此它只会进入 Spring Data Commons 1.5 即将到来的里程碑。

关于java - 如何将存储库填充器与 spring 数据 -jpa 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13565466/

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