Mongo java.lang.IllegalArgumentException: 无法序列化 class java.lang.Class
Mongo java.lang.IllegalArgumentException: can't serialize class java.lang.Class
我正在使用 Java+Spring+MongoTemplate+MongoDB。如果我想将对象插入到我的 MongoDB 中,则会出现错误:
java.lang.IllegalArgumentException: can't serialize class java.lang.Class
这是我的 mongo-context.xml:
<mongo:mongo id="mongoLocal" host="${local.mongo.host}" port="${local.mongo.port}">
<mongo:options
connections-per-host="700"
threads-allowed-to-block-for-connection-multiplier="100"
connect-timeout="15000"
auto-connect-retry="true"
socket-timeout="60000"
write-number="1"/>
</mongo:mongo>
<mongo:db-factory id="mongoDbFactoryLocal" dbname="${local.mongo.db}" mongo-ref="mongoLocal"/>
<bean id="mongoConverterLocal" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg index="0" ref="mongoDbFactoryLocal" />
<constructor-arg index="1">
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
</constructor-arg>
<property name="mapKeyDotReplacement" value="\+"/>
</bean>
<bean id="mongoTemplateLocal" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactoryLocal"/>
<constructor-arg name="mongoConverter" ref="mongoConverterLocal"/>
</bean>
mongo-context.xml 我导入到 spring-context.xml 并像这样使用它:
<bean id="statusDao" class="com.status.StatusDAOImpl">
</bean>
<bean id="BasicDAO" class="com.status.PreparableDAO">
<property name="daos">
<list>
<ref bean="statusDao"/>
</list>
</property>
<property name="template" ref="mongoTemplateLocal"/>
</bean>
最后 class (SimpleMessage),我试图插入到 Mongo:
public abstract class Message implements Serializable {
@Id
private String id;
public Message() {super();}
public Message(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
和
@Document
public class EmlMessage extends Message {
private WatchEvent.Kind<Path> eventType;
private String emlPath;
public EmlMessage() {
super();
}
public EmlMessage(String id,
WatchEvent.Kind<Path> eventType,
StatusType statusType,
String emlPath) {
super(id, statusType);
this.eventType = eventType;
this.emlPath = emlPath;
}
public EmlMessage(String id,
String eventType,
StatusType statusType,
String emlPath) {
super(id, statusType);
this.emlPath = emlPath;
chooseEventType(eventType);
}
public WatchEvent.Kind<Path> getEventType() {
return eventType;
}
public void setEventType(WatchEvent.Kind<Path> eventType) {
this.eventType = eventType;
}
public void setEventType(String eventType) {
chooseEventType(eventType);
}
private void chooseEventType(String eventType) {
if (eventType.equalsIgnoreCase("ENTRY_CREATE"))
this.eventType = StandardWatchEventKinds.ENTRY_CREATE;
else if (eventType.equalsIgnoreCase("ENTRY_MODIFY"))
this.eventType = StandardWatchEventKinds.ENTRY_MODIFY;
else if (eventType.equalsIgnoreCase("ENTRY_DELETE"))
this.eventType = StandardWatchEventKinds.ENTRY_DELETE;
else
this.eventType = null;
}
@Override
public int hashCode() {
int hash = 1;
hash = hash * 31 + (getId() == null ?
"".hashCode() : getId().hashCode());
hash = hash
+ (arriveDateTime == null ? 0 : arriveDateTime.hashCode());
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof EmlMessage))
return false;
SimpleMessage otherA = (SimpleMessage) obj;
return getId().equals(otherA.getId());
}
}
有什么想法吗?我知道我可以扩展 BasicDBObject,但是如果我实现了 MongoMappingConverter.
为什么我需要这样做
问题出在 java.nio.file.StandardWatchEventKinds 的变量 WatchEvent.Kind 中。此 class 是 public 最终 class StandardWatchEventKinds。我刚切换到字符串。现在看起来像
@Document
public class EmlMessage extends Message {
private String eventType;
private String emlPath;
public EmlMessage() {
super();
}
public EmlMessage(String id,
WatchEvent.Kind<Path> eventType,
StatusType statusType,
String emlPath) {
super(id, statusType);
this.eventType = eventType.name();
this.emlPath = emlPath;
}
public EmlMessage(String id,
String eventType,
StatusType statusType,
String emlPath) {
super(id, statusType);
this.emlPath = emlPath;
this.eventType = eventType;
}
public String getEventType() {
return eventType;
}
public void setEventType(WatchEvent.Kind<Path> eventType) {
this.eventType = eventType.name();
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public String getEmlPath() {
return emlPath;
}
public void setEmlPath(String emlPath) {
this.emlPath = emlPath;
}
我正在使用 Java+Spring+MongoTemplate+MongoDB。如果我想将对象插入到我的 MongoDB 中,则会出现错误:
java.lang.IllegalArgumentException: can't serialize class java.lang.Class
这是我的 mongo-context.xml:
<mongo:mongo id="mongoLocal" host="${local.mongo.host}" port="${local.mongo.port}">
<mongo:options
connections-per-host="700"
threads-allowed-to-block-for-connection-multiplier="100"
connect-timeout="15000"
auto-connect-retry="true"
socket-timeout="60000"
write-number="1"/>
</mongo:mongo>
<mongo:db-factory id="mongoDbFactoryLocal" dbname="${local.mongo.db}" mongo-ref="mongoLocal"/>
<bean id="mongoConverterLocal" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg index="0" ref="mongoDbFactoryLocal" />
<constructor-arg index="1">
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
</constructor-arg>
<property name="mapKeyDotReplacement" value="\+"/>
</bean>
<bean id="mongoTemplateLocal" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactoryLocal"/>
<constructor-arg name="mongoConverter" ref="mongoConverterLocal"/>
</bean>
mongo-context.xml 我导入到 spring-context.xml 并像这样使用它:
<bean id="statusDao" class="com.status.StatusDAOImpl">
</bean>
<bean id="BasicDAO" class="com.status.PreparableDAO">
<property name="daos">
<list>
<ref bean="statusDao"/>
</list>
</property>
<property name="template" ref="mongoTemplateLocal"/>
</bean>
最后 class (SimpleMessage),我试图插入到 Mongo:
public abstract class Message implements Serializable {
@Id
private String id;
public Message() {super();}
public Message(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
和
@Document
public class EmlMessage extends Message {
private WatchEvent.Kind<Path> eventType;
private String emlPath;
public EmlMessage() {
super();
}
public EmlMessage(String id,
WatchEvent.Kind<Path> eventType,
StatusType statusType,
String emlPath) {
super(id, statusType);
this.eventType = eventType;
this.emlPath = emlPath;
}
public EmlMessage(String id,
String eventType,
StatusType statusType,
String emlPath) {
super(id, statusType);
this.emlPath = emlPath;
chooseEventType(eventType);
}
public WatchEvent.Kind<Path> getEventType() {
return eventType;
}
public void setEventType(WatchEvent.Kind<Path> eventType) {
this.eventType = eventType;
}
public void setEventType(String eventType) {
chooseEventType(eventType);
}
private void chooseEventType(String eventType) {
if (eventType.equalsIgnoreCase("ENTRY_CREATE"))
this.eventType = StandardWatchEventKinds.ENTRY_CREATE;
else if (eventType.equalsIgnoreCase("ENTRY_MODIFY"))
this.eventType = StandardWatchEventKinds.ENTRY_MODIFY;
else if (eventType.equalsIgnoreCase("ENTRY_DELETE"))
this.eventType = StandardWatchEventKinds.ENTRY_DELETE;
else
this.eventType = null;
}
@Override
public int hashCode() {
int hash = 1;
hash = hash * 31 + (getId() == null ?
"".hashCode() : getId().hashCode());
hash = hash
+ (arriveDateTime == null ? 0 : arriveDateTime.hashCode());
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof EmlMessage))
return false;
SimpleMessage otherA = (SimpleMessage) obj;
return getId().equals(otherA.getId());
}
}
有什么想法吗?我知道我可以扩展 BasicDBObject,但是如果我实现了 MongoMappingConverter.
为什么我需要这样做问题出在 java.nio.file.StandardWatchEventKinds 的变量 WatchEvent.Kind 中。此 class 是 public 最终 class StandardWatchEventKinds。我刚切换到字符串。现在看起来像
@Document
public class EmlMessage extends Message {
private String eventType;
private String emlPath;
public EmlMessage() {
super();
}
public EmlMessage(String id,
WatchEvent.Kind<Path> eventType,
StatusType statusType,
String emlPath) {
super(id, statusType);
this.eventType = eventType.name();
this.emlPath = emlPath;
}
public EmlMessage(String id,
String eventType,
StatusType statusType,
String emlPath) {
super(id, statusType);
this.emlPath = emlPath;
this.eventType = eventType;
}
public String getEventType() {
return eventType;
}
public void setEventType(WatchEvent.Kind<Path> eventType) {
this.eventType = eventType.name();
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public String getEmlPath() {
return emlPath;
}
public void setEmlPath(String emlPath) {
this.emlPath = emlPath;
}