首页 > 解决方案 > org.springframework.data.mongodb.core.mapping.Document 无法引用

问题描述

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.7.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'eclipse'
    id 'java-library'
}

group = 'io.sample'
version = '3.0.0-SNAPSHOT'
sourceCompatibility = '14'
targetCompatibility = '14'

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

repositories {
    mavenLocal()
    mavenCentral()
}

ext {
    set('springCloudVersion', "Hoxton.SR4")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway' 
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    // implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

    developmentOnly 'org.springframework.boot:spring-boot-devtools'

    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'io.projectreactor:reactor-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

package com.mastering.spring.reactive.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Stock {

    private String code;

    private String name;

    private String description;

    public Stock() {
    }

    public Stock(String code, String name, String description) {
        this.name = name;
        this.code = code;
        this.description = description;
    }

    @Id
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Stock person = (Stock) o;

        if (name != null ? !name.equals(person.name) : person.name != null) {
            return false;
        }
        if (code != null ? !code.equals(person.code) : person.code != null) {
            return false;
        }
        return description != null ? description.equals(person.description) : person.description == null;

    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (code != null ? code.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Person{" + "id='" + name + '\'' + ", firstname='" + code + '\'' + ", lastname='" + description + '\''
                + '}';
    }
}

我从https://github.com/PacktPublishing/Working-with-Data-and-Cloud-in-Spring-5.0/blob/master/Section%204/src/main/java/com/mastering/spring/reactive复制/model/Stock.java

错误 在此处输入图像描述

如何解决?

标签: javaspringeclipsegradlespring-data-jpa

解决方案


// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

... 取决于org.springframework.data:spring-data-mongodb,因此应该知道该类。尝试下载依赖项:./gradlew build --refresh-dependencies. 默认build.gradle条目较少;IDEA可能有更好的 Gradle 集成。


推荐阅读