在工作中,整合mongodb报错,在重重排查下,终于找到了mongdb的错误。

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'publicPageService': 
Unsatisfied dependency expressed through field 'baseMongoRepository';
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'publicPageRepository' defined in com.cnabke.service.reverse.repository.PublicPageRepository defined in @EnableMongoRepositories declared on 
MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: 
Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'mongoTemplate' defined in class path resource 
[org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed 
through method 'mongoTemplate' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'mappingMongoConverter' 
defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: 
Unsatisfied dependency expressed through method 'mappingMongoConverter' parameter 1; nested exception is
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.class]: Invocation of init method failed;
 nested exception is java.lang.StackOverflowError

 这个错误的原因看似是mongoTemplate的问题,但是其实不是,划重点来了,你要注意你得mongodb 库对象

mongodb对象是:

@Data
@Document(collection ="publicPage")
public class PublicPageMo {

    @ApiModelProperty("分组id")
    private String accountGroupingId;
    @ApiModelProperty("行业")
    private String industry;
    @ApiModelProperty("所属账号 ")
    private String accountId;
    @ApiModelProperty("产品")
    private String product;
    @ApiModelProperty("国家")
    private String country;
    @ApiModelProperty("地区")
    private String region;
    @ApiModelProperty("备注")
    private String desc;
    @ApiModelProperty("头像")
    private String HeadImg;
    @ApiModelProperty("名称")
    private String name;
    @ApiModelProperty("类别")
    private String category;
    @ApiModelProperty("联系方式--ContactEnum")
    private List<KeyValue> ContactList;
    @ApiModelProperty("地址")
    private AddressEnum address;
 
}

  KeyValue 对象

package com.cnabke.core.common.entity;

import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.List;

public class KeyValue<T1,T2> {
    @ApiModelProperty(value = "键")
    private T1 key;
    @ApiModelProperty(value = "值")
    private T2 value;
    private List<KeyValue<T1,T2>> child = new ArrayList<>();
    public KeyValue() {

    }
    public KeyValue(T1 key, T2 value) {
        this.key = key;
        this.value = value;
    }

    public List<KeyValue<T1, T2>> getChild() {
        return child;
    }

    public void setChild(List<KeyValue<T1, T2>> child) {
        this.child = child;
    }

    public T1 getKey() {
        return key;
    }

    public void setKey(T1 key) {
        this.key = key;
    }

    public T2 getValue() {
        return value;
    }

    public void setValue(T2 value) {
        this.value = value;
    }
}

大家都看到了,我KeyValue用的是泛型,但是mongodb不支持泛型的,但是创建不会报错,所以你启动的时候就会报错,你去掉了之后就会发现神奇了,竟然可以启动了。。

这个错误我排查好久,跟一个大佬讨论后一个个把所有的文件都删除了,最后定位到的。。希望大家看到这个以后使用mongodb的时候能避免此类问题。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐