java.lang.IllegalArgumentException: can't serialize class example.Domain

Spring JIRA | Robert Stiller | 6 years ago
tip
Your exception is missing from the Samebug knowledge base.
Here are the best solutions we found on the Internet.
Click on the to mark the helpful solution and get rewards for you help.
  1. 0

    I can't persist enums with Spring-Data-Mongo. I've got the following error-message: {code} java.lang.IllegalArgumentException: can't serialize class example.Domain at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205) at org.bson.BSONEncoder.putObject(BSONEncoder.java:121) at org.bson.BSONEncoder.putObject(BSONEncoder.java:67) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:215) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:180) at com.mongodb.DBCollection.insert(DBCollection.java:72) at com.mongodb.DBCollection.save(DBCollection.java:537) at com.mongodb.DBCollection.save(DBCollection.java:517) at org.springframework.data.document.mongodb.MongoTemplate$10.doInCollection(MongoTemplate.java:708) at org.springframework.data.document.mongodb.MongoTemplate.execute(MongoTemplate.java:280) at org.springframework.data.document.mongodb.MongoTemplate.saveDBObject(MongoTemplate.java:705) at org.springframework.data.document.mongodb.MongoTemplate.doSave(MongoTemplate.java:605) at org.springframework.data.document.mongodb.MongoTemplate.save(MongoTemplate.java:595) at org.springframework.data.document.mongodb.repository.SimpleMongoRepository.save(SimpleMongoRepository.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:355) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:336) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy21.save(Unknown Source) at de.idealo.fe.dwh.service.SiteServiceTest.testSiteService_Alias(SiteServiceTest.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) {code} The classes: {code} @Document(collection = "sites") public class Site { ... protected Domain domain; ... } {code} {code} public enum Domain { MY_DOMAIN_DE(Long.valueOf(1), "my.domain.de"), MY_DOMAIN_COM(Long.valueOf(1), "my.domain.com"); protected Long id; protected String url; private Domain(Long newId, String newUrl) { id = newId; url = newUrl; } public Long getId() { return id; } public String getUrl() { return url; } } {code} I think the best way to store java-enums is to save the enum as String (using the name() method). When reading the enum field the MappingConverter should lookup the parameter-type (class) of the setter method of the according field and use the valueOf() method of the enum passing the stored string to it - if the parameter-type is an enum.

    Spring JIRA | 6 years ago | Robert Stiller
    java.lang.IllegalArgumentException: can't serialize class example.Domain
  2. 0

    I can't persist enums with Spring-Data-Mongo. I've got the following error-message: {code} java.lang.IllegalArgumentException: can't serialize class example.Domain at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205) at org.bson.BSONEncoder.putObject(BSONEncoder.java:121) at org.bson.BSONEncoder.putObject(BSONEncoder.java:67) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:215) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:180) at com.mongodb.DBCollection.insert(DBCollection.java:72) at com.mongodb.DBCollection.save(DBCollection.java:537) at com.mongodb.DBCollection.save(DBCollection.java:517) at org.springframework.data.document.mongodb.MongoTemplate$10.doInCollection(MongoTemplate.java:708) at org.springframework.data.document.mongodb.MongoTemplate.execute(MongoTemplate.java:280) at org.springframework.data.document.mongodb.MongoTemplate.saveDBObject(MongoTemplate.java:705) at org.springframework.data.document.mongodb.MongoTemplate.doSave(MongoTemplate.java:605) at org.springframework.data.document.mongodb.MongoTemplate.save(MongoTemplate.java:595) at org.springframework.data.document.mongodb.repository.SimpleMongoRepository.save(SimpleMongoRepository.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:355) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:336) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy21.save(Unknown Source) at de.idealo.fe.dwh.service.SiteServiceTest.testSiteService_Alias(SiteServiceTest.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) {code} The classes: {code} @Document(collection = "sites") public class Site { ... protected Domain domain; ... } {code} {code} public enum Domain { MY_DOMAIN_DE(Long.valueOf(1), "my.domain.de"), MY_DOMAIN_COM(Long.valueOf(1), "my.domain.com"); protected Long id; protected String url; private Domain(Long newId, String newUrl) { id = newId; url = newUrl; } public Long getId() { return id; } public String getUrl() { return url; } } {code} I think the best way to store java-enums is to save the enum as String (using the name() method). When reading the enum field the MappingConverter should lookup the parameter-type (class) of the setter method of the according field and use the valueOf() method of the enum passing the stored string to it - if the parameter-type is an enum.

    Spring JIRA | 6 years ago | Robert Stiller
    java.lang.IllegalArgumentException: can't serialize class example.Domain
  3. 0

    Hi, i got the following exception when trying to save a java.util.Locale property: (Code is under the stacktrace) {code} java.lang.IllegalArgumentException: can't serialize class java.util.Locale at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205) at org.bson.BSONEncoder.putObject(BSONEncoder.java:121) at org.bson.BSONEncoder.putObject(BSONEncoder.java:67) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:215) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:180) at com.mongodb.DBCollection.insert(DBCollection.java:72) at com.mongodb.DBCollection.save(DBCollection.java:537) at com.mongodb.DBCollection.save(DBCollection.java:517) at org.springframework.data.document.mongodb.MongoTemplate$10.doInCollection(MongoTemplate.java:710) at org.springframework.data.document.mongodb.MongoTemplate.execute(MongoTemplate.java:282) at org.springframework.data.document.mongodb.MongoTemplate.saveDBObject(MongoTemplate.java:707) at org.springframework.data.document.mongodb.MongoTemplate.doSave(MongoTemplate.java:607) at org.springframework.data.document.mongodb.MongoTemplate.save(MongoTemplate.java:597) at org.springframework.data.document.mongodb.repository.SimpleMongoRepository.save(SimpleMongoRepository.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:355) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:336) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy16.save(Unknown Source) at example.repo.RepositoryTest.test(RepositoryTest.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) {code} {code} package example.repo; import java.io.Serializable; import java.util.HashMap; import java.util.Locale; import java.util.Map; import org.springframework.data.annotation.Id; import org.springframework.data.document.mongodb.mapping.Document; import com.google.code.morphia.annotations.Entity; @Document(collection = "examples") @Entity public class Example implements Serializable { private static final long serialVersionUID = 2827176530940493389L; @Id protected String id; protected Locale country; protected Map<Locale, String> names = new HashMap<Locale, String>(); public Locale getCountry() { return country; } public String getId() { return id; } public Map<Locale, String> getNames() { return names; } public void setCountry(Locale newCountry) { country = newCountry; } public void setId(String newId) { id = newId; } public void setNames(Map<Locale, String> newNames) { names = newNames; } } {code} {code} package example.repo; import org.springframework.data.document.mongodb.repository.MongoRepository; import org.springframework.data.document.mongodb.repository.QueryDslPredicateExecutor; public interface ExampleService extends MongoRepository<Example, String>, QueryDslPredicateExecutor<Example> { } {code} {code} package example.repo; import java.util.Locale; import javax.inject.Inject; import junit.framework.Assert; import junit.framework.TestCase; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/testApplicationContext.xml") public class RepositoryTest extends TestCase { @Inject protected MongoTemplate template; @Inject protected ExampleService exampleService; public ExampleService getExampleService() { return exampleService; } public MongoTemplate getTemplate() { return template; } public void setExampleService(ExampleService newExampleService) { exampleService = newExampleService; } public void setTemplate(MongoTemplate newTemplate) { template = newTemplate; } @Before @Override public void setUp() throws Exception { template.dropCollection("examples"); template.createCollection("examples"); super.setUp(); } @After @Override public void tearDown() throws Exception { template.dropCollection("examples"); super.tearDown(); } @Test public void test() { Example example = new Example(); example.setCountry(Locale.GERMANY); example.getNames().put(Locale.GERMANY, "Deutschland"); example.getNames().put(Locale.US, "Germany"); example = exampleService.save(example); Assert.assertEquals(example, exampleService.findAll().get(0)); } } {code} {code} <?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:context="http://www.springframework.org/schema/context" xmlns:spring-data="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <spring-data:mongo host="127.0.0.1" port="27017" id="mongo"/> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg value="testdb"/> <constructor-arg> <spring-data:mapping-converter/> </constructor-arg> </bean> <spring-data:repositories base-package="example.repo" mongo-template-ref="mongoTemplate"> </spring-data:repositories> <context:annotation-config/> </beans> {code} {code} package example.repo; import java.util.Locale; import org.springframework.core.convert.converter.Converter; public class LocaleToStringConverter implements Converter<Locale, String> { public String convert(Locale source) { return source.toString(); } } {code} {code} package example.repo; import java.util.Locale; import org.springframework.core.convert.converter.Converter; import org.springframework.util.StringUtils; public class StringToLocaleConverter implements Converter<String, Locale> { public Locale convert(String source) { return StringUtils.parseLocaleString(source); } } {code} {code} <?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:context="http://www.springframework.org/schema/context" xmlns:spring-data="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <spring-data:mongo host="127.0.0.1" port="27017" id="mongo"/> <bean id="mappingContext" class="org.springframework.data.document.mongodb.mapping.MongoMappingContext"/> <bean id="mongoConverter" class="org.springframework.data.document.mongodb.convert.MappingMongoConverter"> <constructor-arg ref="mappingContext"/> <property name="converters"> <set> <bean class="example.repo.LocaleToStringConverter"/> <bean class="example.repo.StringToLocaleConverter"/> </set> </property> </bean> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg value="testdb"/> <constructor-arg ref="mongoConverter"/> </bean> <spring-data:repositories base-package="example.repo" mongo-template-ref="mongoTemplate"> </spring-data:repositories> <context:annotation-config/> </beans> {code} I guess the converters are not used correctly since there is a "native" support for java.util.Locale objects if using the Converter-Framework of Spring. (org.springframework.core.convert.support.StringToLocaleConverter). Another exception occures when reading a map with a Locale as key-type: (Code is under the stacktrace) To save the map i had to have to register an own Converter (see code) - it seems that the converters are not correctly used at this point too. {code} java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Locale at example.repo.RepositoryTest.test(RepositoryTest.java:70) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) {code} {code} package example.repo; import java.util.Locale; import org.springframework.core.convert.converter.Converter; import org.springframework.data.mapping.MappingBeanHelper; import org.springframework.util.StringUtils; public class StringToLocaleConverter implements Converter<String, Locale> { public Locale convert(String source) { return StringUtils.parseLocaleString(source); } public void init() { MappingBeanHelper.getConversionService().addConverter(this); MappingBeanHelper.getConversionService().addConverterFactory(new StringToLocaleConverterFactory()); } } {code} {code} package example.repo; import java.util.Locale; import org.springframework.core.convert.converter.Converter; import org.springframework.data.mapping.MappingBeanHelper; public class LocaleToStringConverter implements Converter<Locale, String> { public String convert(Locale source) { return source.toString(); } public void init() { MappingBeanHelper.getConversionService().addConverter(this); } } {code} {code} package example.repo; import java.io.Serializable; import java.util.HashMap; import java.util.Locale; import java.util.Map; import org.springframework.data.annotation.Id; import org.springframework.data.document.mongodb.mapping.Document; import com.google.code.morphia.annotations.Entity; @Document(collection = "examples") @Entity public class Example implements Serializable { private static final long serialVersionUID = 2827176530940493389L; @Id protected String id; // protected Locale country; protected Map<Locale, String> names = new HashMap<Locale, String>(); // public Locale getCountry() { // return country; // } @Override public boolean equals(Object obj) { Example ex = (Example) obj; return id.equals(ex.id) && names.equals(ex.names); } public String getId() { return id; } public Map<Locale, String> getNames() { return names; } // public void setCountry(Locale newCountry) { // country = newCountry; // } public void setId(String newId) { id = newId; } public void setNames(Map<Locale, String> newNames) { names = newNames; } } {code} {code} <?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:context="http://www.springframework.org/schema/context" xmlns:spring-data="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <spring-data:mongo host="127.0.0.1" port="27017" id="mongo"/> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg value="testdb"/> <constructor-arg> <spring-data:mapping-converter/> </constructor-arg> </bean> <spring-data:repositories base-package="example.repo" mongo-template-ref="mongoTemplate"> </spring-data:repositories> <bean class="example.repo.LocaleToStringConverter" init-method="init" depends-on="mongoTemplate"/> <bean class="example.repo.StringToLocaleConverter" init-method="init" depends-on="mongoTemplate"/> <context:annotation-config/> </beans> {code} {code} package example.repo; import java.util.Locale; import java.util.Map; import javax.inject.Inject; import junit.framework.Assert; import junit.framework.TestCase; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/testApplicationContext.xml") public class RepositoryTest extends TestCase { @Inject protected MongoTemplate template; @Inject protected ExampleService exampleService; public ExampleService getExampleService() { return exampleService; } public MongoTemplate getTemplate() { return template; } public void setExampleService(ExampleService newExampleService) { exampleService = newExampleService; } public void setTemplate(MongoTemplate newTemplate) { template = newTemplate; } @Before @Override public void setUp() throws Exception { template.dropCollection("examples"); template.createCollection("examples"); super.setUp(); } @After @Override public void tearDown() throws Exception { template.dropCollection("examples"); super.tearDown(); } @Test public void test() { Example example = new Example(); // example.setCountry(Locale.GERMANY); example.getNames().put(Locale.GERMANY, "Deutschland"); example.getNames().put(Locale.US, "Germany"); example = exampleService.save(example); for (Map.Entry<Locale, String> entry : exampleService.findAll().get(0).getNames().entrySet()) { System.out.println(entry.getKey().getCountry()); System.out.println(entry.getValue()); } Assert.assertEquals(example, exampleService.findAll().get(0)); } } {code} {code} If i hadn't registered my own converter the following exception were thrown (even with "native" Converter Support from Spring-Converter-Framework for java.util.Locale): org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from 'java.util.Locale' to 'java.lang.String' at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:181) at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:133) at org.springframework.data.document.mongodb.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:511) at org.springframework.data.document.mongodb.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:480) at org.springframework.data.document.mongodb.convert.MappingMongoConverter$4.doWithPersistentProperty(MappingMongoConverter.java:372) at org.springframework.data.mapping.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:189) at org.springframework.data.document.mongodb.convert.MappingMongoConverter.write(MappingMongoConverter.java:358) at org.springframework.data.document.mongodb.convert.MappingMongoConverter.write(MappingMongoConverter.java:323) at org.springframework.data.document.mongodb.MongoTemplate.doSave(MongoTemplate.java:604) at org.springframework.data.document.mongodb.MongoTemplate.save(MongoTemplate.java:597) at org.springframework.data.document.mongodb.repository.SimpleMongoRepository.save(SimpleMongoRepository.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:355) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:336) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy16.save(Unknown Source) at example.repo.RepositoryTest.test(RepositoryTest.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) {code} I hope these information help you a little bit when trying to fix this.

    Spring JIRA | 6 years ago | Robert Stiller
    java.lang.IllegalArgumentException: can't serialize class java.util.Locale
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Hi, i got the following exception when trying to save a java.util.Locale property: (Code is under the stacktrace) {code} java.lang.IllegalArgumentException: can't serialize class java.util.Locale at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205) at org.bson.BSONEncoder.putObject(BSONEncoder.java:121) at org.bson.BSONEncoder.putObject(BSONEncoder.java:67) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:215) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:180) at com.mongodb.DBCollection.insert(DBCollection.java:72) at com.mongodb.DBCollection.save(DBCollection.java:537) at com.mongodb.DBCollection.save(DBCollection.java:517) at org.springframework.data.document.mongodb.MongoTemplate$10.doInCollection(MongoTemplate.java:710) at org.springframework.data.document.mongodb.MongoTemplate.execute(MongoTemplate.java:282) at org.springframework.data.document.mongodb.MongoTemplate.saveDBObject(MongoTemplate.java:707) at org.springframework.data.document.mongodb.MongoTemplate.doSave(MongoTemplate.java:607) at org.springframework.data.document.mongodb.MongoTemplate.save(MongoTemplate.java:597) at org.springframework.data.document.mongodb.repository.SimpleMongoRepository.save(SimpleMongoRepository.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:355) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:336) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy16.save(Unknown Source) at example.repo.RepositoryTest.test(RepositoryTest.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) {code} {code} package example.repo; import java.io.Serializable; import java.util.HashMap; import java.util.Locale; import java.util.Map; import org.springframework.data.annotation.Id; import org.springframework.data.document.mongodb.mapping.Document; import com.google.code.morphia.annotations.Entity; @Document(collection = "examples") @Entity public class Example implements Serializable { private static final long serialVersionUID = 2827176530940493389L; @Id protected String id; protected Locale country; protected Map<Locale, String> names = new HashMap<Locale, String>(); public Locale getCountry() { return country; } public String getId() { return id; } public Map<Locale, String> getNames() { return names; } public void setCountry(Locale newCountry) { country = newCountry; } public void setId(String newId) { id = newId; } public void setNames(Map<Locale, String> newNames) { names = newNames; } } {code} {code} package example.repo; import org.springframework.data.document.mongodb.repository.MongoRepository; import org.springframework.data.document.mongodb.repository.QueryDslPredicateExecutor; public interface ExampleService extends MongoRepository<Example, String>, QueryDslPredicateExecutor<Example> { } {code} {code} package example.repo; import java.util.Locale; import javax.inject.Inject; import junit.framework.Assert; import junit.framework.TestCase; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/testApplicationContext.xml") public class RepositoryTest extends TestCase { @Inject protected MongoTemplate template; @Inject protected ExampleService exampleService; public ExampleService getExampleService() { return exampleService; } public MongoTemplate getTemplate() { return template; } public void setExampleService(ExampleService newExampleService) { exampleService = newExampleService; } public void setTemplate(MongoTemplate newTemplate) { template = newTemplate; } @Before @Override public void setUp() throws Exception { template.dropCollection("examples"); template.createCollection("examples"); super.setUp(); } @After @Override public void tearDown() throws Exception { template.dropCollection("examples"); super.tearDown(); } @Test public void test() { Example example = new Example(); example.setCountry(Locale.GERMANY); example.getNames().put(Locale.GERMANY, "Deutschland"); example.getNames().put(Locale.US, "Germany"); example = exampleService.save(example); Assert.assertEquals(example, exampleService.findAll().get(0)); } } {code} {code} <?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:context="http://www.springframework.org/schema/context" xmlns:spring-data="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <spring-data:mongo host="127.0.0.1" port="27017" id="mongo"/> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg value="testdb"/> <constructor-arg> <spring-data:mapping-converter/> </constructor-arg> </bean> <spring-data:repositories base-package="example.repo" mongo-template-ref="mongoTemplate"> </spring-data:repositories> <context:annotation-config/> </beans> {code} {code} package example.repo; import java.util.Locale; import org.springframework.core.convert.converter.Converter; public class LocaleToStringConverter implements Converter<Locale, String> { public String convert(Locale source) { return source.toString(); } } {code} {code} package example.repo; import java.util.Locale; import org.springframework.core.convert.converter.Converter; import org.springframework.util.StringUtils; public class StringToLocaleConverter implements Converter<String, Locale> { public Locale convert(String source) { return StringUtils.parseLocaleString(source); } } {code} {code} <?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:context="http://www.springframework.org/schema/context" xmlns:spring-data="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <spring-data:mongo host="127.0.0.1" port="27017" id="mongo"/> <bean id="mappingContext" class="org.springframework.data.document.mongodb.mapping.MongoMappingContext"/> <bean id="mongoConverter" class="org.springframework.data.document.mongodb.convert.MappingMongoConverter"> <constructor-arg ref="mappingContext"/> <property name="converters"> <set> <bean class="example.repo.LocaleToStringConverter"/> <bean class="example.repo.StringToLocaleConverter"/> </set> </property> </bean> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg value="testdb"/> <constructor-arg ref="mongoConverter"/> </bean> <spring-data:repositories base-package="example.repo" mongo-template-ref="mongoTemplate"> </spring-data:repositories> <context:annotation-config/> </beans> {code} I guess the converters are not used correctly since there is a "native" support for java.util.Locale objects if using the Converter-Framework of Spring. (org.springframework.core.convert.support.StringToLocaleConverter). Another exception occures when reading a map with a Locale as key-type: (Code is under the stacktrace) To save the map i had to have to register an own Converter (see code) - it seems that the converters are not correctly used at this point too. {code} java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Locale at example.repo.RepositoryTest.test(RepositoryTest.java:70) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) {code} {code} package example.repo; import java.util.Locale; import org.springframework.core.convert.converter.Converter; import org.springframework.data.mapping.MappingBeanHelper; import org.springframework.util.StringUtils; public class StringToLocaleConverter implements Converter<String, Locale> { public Locale convert(String source) { return StringUtils.parseLocaleString(source); } public void init() { MappingBeanHelper.getConversionService().addConverter(this); MappingBeanHelper.getConversionService().addConverterFactory(new StringToLocaleConverterFactory()); } } {code} {code} package example.repo; import java.util.Locale; import org.springframework.core.convert.converter.Converter; import org.springframework.data.mapping.MappingBeanHelper; public class LocaleToStringConverter implements Converter<Locale, String> { public String convert(Locale source) { return source.toString(); } public void init() { MappingBeanHelper.getConversionService().addConverter(this); } } {code} {code} package example.repo; import java.io.Serializable; import java.util.HashMap; import java.util.Locale; import java.util.Map; import org.springframework.data.annotation.Id; import org.springframework.data.document.mongodb.mapping.Document; import com.google.code.morphia.annotations.Entity; @Document(collection = "examples") @Entity public class Example implements Serializable { private static final long serialVersionUID = 2827176530940493389L; @Id protected String id; // protected Locale country; protected Map<Locale, String> names = new HashMap<Locale, String>(); // public Locale getCountry() { // return country; // } @Override public boolean equals(Object obj) { Example ex = (Example) obj; return id.equals(ex.id) && names.equals(ex.names); } public String getId() { return id; } public Map<Locale, String> getNames() { return names; } // public void setCountry(Locale newCountry) { // country = newCountry; // } public void setId(String newId) { id = newId; } public void setNames(Map<Locale, String> newNames) { names = newNames; } } {code} {code} <?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:context="http://www.springframework.org/schema/context" xmlns:spring-data="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <spring-data:mongo host="127.0.0.1" port="27017" id="mongo"/> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg value="testdb"/> <constructor-arg> <spring-data:mapping-converter/> </constructor-arg> </bean> <spring-data:repositories base-package="example.repo" mongo-template-ref="mongoTemplate"> </spring-data:repositories> <bean class="example.repo.LocaleToStringConverter" init-method="init" depends-on="mongoTemplate"/> <bean class="example.repo.StringToLocaleConverter" init-method="init" depends-on="mongoTemplate"/> <context:annotation-config/> </beans> {code} {code} package example.repo; import java.util.Locale; import java.util.Map; import javax.inject.Inject; import junit.framework.Assert; import junit.framework.TestCase; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/testApplicationContext.xml") public class RepositoryTest extends TestCase { @Inject protected MongoTemplate template; @Inject protected ExampleService exampleService; public ExampleService getExampleService() { return exampleService; } public MongoTemplate getTemplate() { return template; } public void setExampleService(ExampleService newExampleService) { exampleService = newExampleService; } public void setTemplate(MongoTemplate newTemplate) { template = newTemplate; } @Before @Override public void setUp() throws Exception { template.dropCollection("examples"); template.createCollection("examples"); super.setUp(); } @After @Override public void tearDown() throws Exception { template.dropCollection("examples"); super.tearDown(); } @Test public void test() { Example example = new Example(); // example.setCountry(Locale.GERMANY); example.getNames().put(Locale.GERMANY, "Deutschland"); example.getNames().put(Locale.US, "Germany"); example = exampleService.save(example); for (Map.Entry<Locale, String> entry : exampleService.findAll().get(0).getNames().entrySet()) { System.out.println(entry.getKey().getCountry()); System.out.println(entry.getValue()); } Assert.assertEquals(example, exampleService.findAll().get(0)); } } {code} {code} If i hadn't registered my own converter the following exception were thrown (even with "native" Converter Support from Spring-Converter-Framework for java.util.Locale): org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from 'java.util.Locale' to 'java.lang.String' at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:181) at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:133) at org.springframework.data.document.mongodb.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:511) at org.springframework.data.document.mongodb.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:480) at org.springframework.data.document.mongodb.convert.MappingMongoConverter$4.doWithPersistentProperty(MappingMongoConverter.java:372) at org.springframework.data.mapping.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:189) at org.springframework.data.document.mongodb.convert.MappingMongoConverter.write(MappingMongoConverter.java:358) at org.springframework.data.document.mongodb.convert.MappingMongoConverter.write(MappingMongoConverter.java:323) at org.springframework.data.document.mongodb.MongoTemplate.doSave(MongoTemplate.java:604) at org.springframework.data.document.mongodb.MongoTemplate.save(MongoTemplate.java:597) at org.springframework.data.document.mongodb.repository.SimpleMongoRepository.save(SimpleMongoRepository.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:355) at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:336) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy16.save(Unknown Source) at example.repo.RepositoryTest.test(RepositoryTest.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) {code} I hope these information help you a little bit when trying to fix this.

    Spring JIRA | 6 years ago | Robert Stiller
    java.lang.IllegalArgumentException: can't serialize class java.util.Locale
  6. 0

    MongoDb: fields stored in the db can't start with '$'

    GitHub | 5 years ago | tiagoboldt
    java.lang.IllegalArgumentException: fields stored in the db can't start with '$' (Bad Key: '$outer')

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.IllegalArgumentException

      can't serialize class example.Domain

      at org.bson.BSONEncoder._putObjectField()
    2. MongoDB Java Driver
      DBCollection.save
      1. org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205)
      2. org.bson.BSONEncoder.putObject(BSONEncoder.java:121)
      3. org.bson.BSONEncoder.putObject(BSONEncoder.java:67)
      4. com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:215)
      5. com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:180)
      6. com.mongodb.DBCollection.insert(DBCollection.java:72)
      7. com.mongodb.DBCollection.save(DBCollection.java:537)
      8. com.mongodb.DBCollection.save(DBCollection.java:517)
      8 frames
    3. org.springframework.data
      SimpleMongoRepository.save
      1. org.springframework.data.document.mongodb.MongoTemplate$10.doInCollection(MongoTemplate.java:708)
      2. org.springframework.data.document.mongodb.MongoTemplate.execute(MongoTemplate.java:280)
      3. org.springframework.data.document.mongodb.MongoTemplate.saveDBObject(MongoTemplate.java:705)
      4. org.springframework.data.document.mongodb.MongoTemplate.doSave(MongoTemplate.java:605)
      5. org.springframework.data.document.mongodb.MongoTemplate.save(MongoTemplate.java:595)
      6. org.springframework.data.document.mongodb.repository.SimpleMongoRepository.save(SimpleMongoRepository.java:68)
      6 frames
    4. Java RT
      Method.invoke
      1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      4. java.lang.reflect.Method.invoke(Method.java:616)
      4 frames
    5. Spring Data Core
      RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke
      1. org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:355)
      2. org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:336)
      2 frames
    6. Spring AOP
      JdkDynamicAopProxy.invoke
      1. org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      2. org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      2 frames
    7. Unknown
      $Proxy21.save
      1. $Proxy21.save(Unknown Source)
      1 frame
    8. de.idealo.fe
      SiteServiceTest.testSiteService_Alias
      1. de.idealo.fe.dwh.service.SiteServiceTest.testSiteService_Alias(SiteServiceTest.java:46)
      1 frame
    9. Java RT
      Method.invoke
      1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      4. java.lang.reflect.Method.invoke(Method.java:616)
      4 frames
    10. JUnit
      RunBefores.evaluate
      1. org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
      2. org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      3. org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
      4. org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
      5. org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
      5 frames
    11. Spring TestContext
      RunBeforeTestMethodCallbacks.evaluate
      1. org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
      1 frame
    12. JUnit
      RunAfters.evaluate
      1. org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
      1 frame
    13. Spring TestContext
      SpringJUnit4ClassRunner.runChild
      1. org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
      2. org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      3. org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
      3 frames
    14. JUnit
      ParentRunner$2.evaluate
      1. org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      2. org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
      3. org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
      4. org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
      5. org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
      6. org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
      6 frames
    15. Spring TestContext
      RunAfterTestClassCallbacks.evaluate
      1. org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      2. org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      2 frames
    16. JUnit
      ParentRunner.run
      1. org.junit.runners.ParentRunner.run(ParentRunner.java:236)
      1 frame
    17. Spring TestContext
      SpringJUnit4ClassRunner.run
      1. org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
      1 frame
    18. JUnit4 Runner
      JUnit4TestReference.run
      1. org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
      1 frame
    19. JUnit3 Runner
      RemoteTestRunner.main
      1. org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      2. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      3. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      4. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      5. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      5 frames