java.lang.IllegalArgumentException: Invalid BSON field name google_web-**-beauty.m
at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:532)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:198)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:141)
at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
...
版本:
org.mongodb
mongodb-driver
3.6.3
java.lang.IllegalArgumentException 异常,说明该字段就不合法,没法插入到mongoDB中
@Override
public void writeName(final String name) {
notNull("name", name);
if (state != State.NAME) {
throwInvalidState("WriteName", State.NAME);
}
if (!fieldNameValidatorStack.peek().validate(name)) {
throw new IllegalArgumentException(format("Invalid BSON field name %s", name));
}
doWriteName(name);
context.name = name;
state = State.VALUE;
}
package org.bson;
public interface FieldNameValidator {
boolean validate(String fieldName);
FieldNameValidator getValidatorForField(String fieldName);
}
换该字段名称,或者干脆剔除。
如果这个字段是你自定义的,那还好说,但是,假如是不确定的,比如我报错的是该Java对象里面有个 Map
那么解决方法只能是先判断字段名称是否合法,不合法的话就剔除该字段。
public class Document implements Map, Serializable, Bson
public void insert(MongoCollection coll, Document Document) {
try {
coll.insertOne(Document);
} catch (java.lang.IllegalArgumentException e) {
LOGGER.error("{}",e);
Document newDocument = removeTheIllegalKeyForStore(Document);
coll.insertOne(newDocument);
}
}
public static Document removeTheIllegalKeyForStore(Document document){
/*
1、 new document
*/
Document newDocument = new Document();
for (Map.Entry entry :
document.entrySet()) {
Object entryValue = entry.getValue();
if(null == entryValue){
continue;
}
Class> clazz = entryValue.getClass();
/*
2、 if the entryValue class is Map,
validate the Map key
*/
if (Map.class.isAssignableFrom(clazz)) {
Map
public static Object removeTheIllegalKeyForStore(Class clazz,Object instance) {
/*
1. find the field which type is Map
*/
List mapFieldList = Lists.newArrayList();
Field[] fields = clazz.getDeclaredFields();
if(null != fields){
for (Field field :
fields) {
Class> fieldClazz = field.getType();
if(Map.class.isAssignableFrom(fieldClazz) &&
ParameterizedTypeImpl.class.isAssignableFrom(field.getGenericType().getClass())){
ParameterizedTypeImpl parameterizedTypeImpl = (ParameterizedTypeImpl) field.getGenericType();
Type keyType = parameterizedTypeImpl.getActualTypeArguments()[0];
if(keyType.getTypeName().equalsIgnoreCase("java.lang.String")){
System.out.println(field.getName());
mapFieldList.add(field);
}
}
}
}
/*
2. validate the map key whether it can be store
*/
for (Field mapField :
mapFieldList) {
try {
mapField.setAccessible(true);
Map map = (Map) mapField.get(instance);
Set illegalKeys = Sets.newHashSet();
for (String key :
map.keySet()) {
boolean isValidate = new CollectibleDocumentFieldNameValidator().validate(key);
if(!isValidate){
illegalKeys.add(key);
}
}
for (String illegalKey :
illegalKeys) {
LOGGER.error("remove illegalKey -> {}",illegalKey);
map.remove(illegalKey);
}
mapField.set(instance,map);
} catch (IllegalAccessException e) {
LOGGER.error("{}",e);
}
}
return instance;
}