Java 整合mongodb报错问题 Error creating bean with name ‘publicPageService‘: Unsatisfied dependency expre

在工作中,整合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的时候能避免此类问题。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值