问题
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.
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
}
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>
and data.json contains
[{"_class" : "com.some.Class",
"id" : "id1",
"firstname" : "Admin",
"lastname" : "admin"
},
{
"_class" : "com.some.Class",
"id" : "id2",
"firstname" : "user",
"lastname" : "user"
}]
I get the following exception:
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
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?
回答1:
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.
回答2:
I think you cannot use _
directly as JSON key (_class
).
Check the List of extensions for non-standard JSON
来源:https://stackoverflow.com/questions/13565466/how-to-use-repository-populator-with-spring-data-jpa