我得到了错误,不知道在哪里调试。
ConverterNotFoundException:找不到能够从类型@javax.validation.constraints.NotNull java.util.HashMap<@javax.validation.constraints.NotNull java.lang.String,@javax.validation.constraints.NotNull java.util 转换的转换器。 Date> 键入 java.lang.String
我已经设置了我的应用程序上下文:
<mongo:mapping-converter id="mongoConverter" base-package="com.app">
<mongo:custom-converters>
<mongo:converter>
<bean class="com.app.util.converter.HashMapToStringConverter" />
</mongo:converter>
<mongo:converter>
<bean class="com.app.util.converter.StringToHashMapConverter" />
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
<bean class="org.springframework.data.mongodb.core.MongoTemplate" id="mongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mongoConverter"/>
</bean>
我的转换器之一是:
public class HashMapToStringConverter implements Converter<HashMap<String,Date>, String>{
@Override
public String convert(HashMap<String,Date> map) {
if(map==null){
throw new ConversionFailedException(TypeDescriptor.valueOf(HashMap.class),
TypeDescriptor.valueOf(String.class), map, null);
}
StringBuilder sb = new StringBuilder();
boolean first = true;
for(Map.Entry<String, ?> e: map.entrySet()){
if(!first){
sb.append(",");
first=!first;
}
sb.append(e.getKey());
sb.append("::");
sb.append(e.getValue());
}
return sb.toString();
}
}