Spring-data-rest有时返回带有空关系内容字段的实体

问题描述

我在Kotlin中有一个使用Spring数据仓库Spring Web的项目。我使用Gradle进行编译。

该项目分为三个模块。

  • 实体(Hibernate,JPA)
  • 休息(春季数据休息)
  • Web(Spring Web)

其余模块和Web模块取决于实体。

我有一个实体项目,该项目与其他实体有关系。当我执行“获取项目”请求时,实体会返回给我,但是关系应为空的字段。

该问题已正常解决,在Rest之前先编译Web模块,但已停止工作。

我认为当Rest和Web构建分开进行时,编译整个项目是一个问题,但是我已经做到了,但是没有用。

我不知道为什么会发生这种情况,并且在互联网上也没有发现任何类似的问题,因此,我感谢您的帮助。

分享了我认为可能有用的文件

/build.gradle

buildscript {
    ext.kotlin_version = '1.3.72'
    ext.spring_boot_version = '2.2.2.RELEASE'
    ext.jjwt_version = '0.10.6'
    ext.klockVersion = "1.7.3"
    ext.queryDslVersion = '4.2.1'
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
    }
}


def javaProjects() {
    return subprojects.findAll { new File(it.projectDir,"src").exists() }
}

subprojects {
    repositories {
        jcenter()
        mavenCentral()
    }

    group 'spotlight.app'
    version '0.1.0'

    println project.name

    configure(javaProjects()) {
        apply plugin: "java"
        apply plugin: "java-library"
        apply plugin: "org.jetbrains.kotlin.jvm"
        apply plugin: 'kotlin'
        apply plugin: "kotlin-spring"
        apply plugin: "kotlin-jpa"
        apply plugin: 'io.spring.dependency-management'
        apply plugin: 'org.springframework.boot'

        compileKotlin {
            kotlinoptions.jvmTarget = "1.8"
            kotlinoptions {
                freeCompilerArgs = ['-Xjvm-default=compatibility']
            }

        }
        compileTestKotlin {
            kotlinoptions.jvmTarget = "1.8"
        }

        dependencies {
            implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
            implementation "io.jsonwebtoken:jjwt-api:$jjwt_version"
            implementation "io.jsonwebtoken:jjwt-impl:$jjwt_version"
            implementation "io.jsonwebtoken:jjwt-jackson:$jjwt_version"
            implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
            implementation group: 'com.vladmihalcea',name: 'hibernate-types-52',version: '1.0.0'
            implementation "com.soywiz.korlibs.klock:klock-jvm:1.7.3"
            implementation "org.jetbrains.kotlin:kotlin-reflect"

            implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
            implementation "org.springframework.boot:spring-boot-starter-web"
            implementation "org.springframework.boot:spring-boot-starter-security"
            implementation "org.springframework.boot:spring-boot-starter-webflux"
            implementation 'org.springframework.boot:spring-boot-starter-data-rest:2.3.3.RELEASE'

            implementation "org.postgresql:postgresql:42.1.3"

            implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'

            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-reactor"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"
            implementation "io.projectreactor.kotlin:reactor-kotlin-extensions"

            testImplementation('org.springframework.boot:spring-boot-starter-test') {
                exclude module: 'junit'
                exclude module: 'mockito-core'
            }

            testImplementation('org.junit.jupiter:junit-jupiter:5.5.2')
            testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
            testImplementation('com.ninja-squad:springmockk:2.0.0')
        }

        allOpen {
            annotation("javax.persistence.Entity")
            annotation("javax.persistence.MappedSuperclass")
            annotation("javax.persistence.Embeddable")
        }
    }

}
project(":entities") {
    bootJar {
        enabled = false
    }
    jar {
        enabled = true
    }
}

/entities/build.gradle

apply plugin: 'kotlin-kapt'
apply plugin: 'idea'

idea {
    module {
        def kaptMain = file('$buildDir/build/generated/source/kapt/main')
        sourceDirs += kaptMain
        generatedSourceDirs += kaptMain
    }
}


dependencies {
    api "com.querydsl:querydsl-jpa:${queryDslVersion}"
    kapt "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
}

/web/build.gradle

dependencies {
    implementation project(":entities")
}

/rest/build.gradle

dependencies {
    implementation project(":entities")
}

/rest/config/RestRepositoryConfiguration.kt



@Configuration
class RestRepositoryConfiguration : RepositoryRestConfigurer {
    @Autowired
    private val entityManager: EntityManager? = null

    override fun configureRepositoryRestConfiguration(config: RepositoryRestConfiguration) {
        config.exposeIdsFor(
            *entityManager!!.metamodel.entities.stream().map { e: EntityType<*> -> e.javaType }.collect(
                Collectors.toList()
            ).toTypedArray()
        )
        config.defaultMediaType = MediaType.APPLICATION_JSON;
        config.useHalAsDefaultJsonMediaType(false)
        config.setBasePath("/rest")
    }
}

感谢您的时间

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...