我有一个包含枚举属性的类。
public class Car{
private UserAction action;
//getters setters
}
我想从/向Mongo db保存/读取它。我正在使用Spring框架。
SpringConfig.xml
<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:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mongo:mongo host="127.0.0.1" port="27017" />
<mongo:db-factory dbname="dbName" />
<bean id="mappingContext"
class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<bean id="defaultMongoTypeMapper"
class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey"><null/></constructor-arg>
</bean>
<bean id="mappingMongoConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
</bean>
<!--CODE BELOW CASES PROBLEMS-->
<mongo:mapping-converter id="mappingConverter">
<mongo:custom-converters>
<mongo:converter>
<bean class="myapp.mongo.converters.UserActionReadConverter"/>
</mongo:converter>
<mongo:converter>
<bean class="myapp.mongo.converters.UserActionWriteConverter"/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
</beans>
UserActionReadConverter.java
package myapp.mongo.converters;
import myapp.model.UserAction;
import com.mongodb.DBObject;
import org.springframework.core.convert.converter.Converter;
public class UserActionReadConverter implements Converter<DBObject, UserAction> {
public UserAction convert(DBObject source) {
String val = (String) source.get("value");
return (val.equals("PROCESSED_BY_USER" ) ? UserAction.PROCESSED_BY_USER : UserAction.UNPROCESSED_BY_USER);
}
}
UserActionWriteConverter.java
package myapp.mongo.converters;
import myapp.model.UserAction;
import org.springframework.core.convert.converter.Converter;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class UserActionWriteConverter implements Converter<UserAction, DBObject> {
public DBObject convert(UserAction userAction) {
DBObject dbo = new BasicDBObject();
dbo.put("value", userAction.toString());
return dbo;
}
}
操作初始化
...
ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
this.operations = (MongoOperations) ctx.getBean(beamName);
...
我不知道如何正确地将转换器映射到config .xml。它返回错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': Invocation of init method failed; nested exception is javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
第二个问题是。我想转换简单的枚举,为此我使用了#34;支持识别器&#34;值。我可以在不使用它的情况下转换枚举,数据库中的数据将存储为{car.userAction:&#34;数据值&#34;而不是{car.value.userAction:&#34;数据值&#34;谢谢。
答案 0 :(得分:0)
缺少验证器库。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.3.Final</version>
</dependency>