本文介绍了如何将存储库填充器与 spring 数据 -jpa 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Spring Data JPA and I am trying to use the repository populators to fill data - but I get an exception of malformed json.

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

I think it may be the location of the data.json file. I have placed that in the META-INF folder and am configuring for a test case as follows

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

//Test cases here

}

我的 sample-data.xml 是:

my sample-data.xml is:

<?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 包含

and data.json contains

[{"_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"属性.我正在使用弹簧数据 1.4.0.RELEASE 和 jackson 1.9.2.我是否混淆了依赖关系?

So I did a bit more debugging and it turns out that the configuration is looking for the "locations" attribute in the repository populator definition - whereas the xml requires the "location" attribute. I am using spring data 1.4.0.RELEASE and jackson 1.9.2. Am I mixing up the dependencies?

推荐答案

这是当前版本的 Spring Data Commons 中的一个错误(参见 DATACMNS-227 了解详情).它是蜜蜂 最近修复 但由于修复需要更改命名空间使其成为 Spring Data Commons 1.5 即将到来的里程碑.

This has been a bug in the current version of Spring Data Commons (see DATACMNS-227 for details). It's bee fixed recently but as the fix requires a namespace change it will only make it into the upcoming milestone of Spring Data Commons 1.5.

这篇关于如何将存储库填充器与 spring 数据 -jpa 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:39