小编V. *_*sov的帖子

如何有选择地升级Spring Boot中的依赖项?(示例案例:Spring Data)

我使用Spring Boot的Spring Data JPA启动程序(1.4.1).它包含Spring Data JPA 1.10.3.但是,我需要使用@DomainEvents此版本弹簧数据中尚不存在的注释.当我尝试添加最新版本的Spring Data JPA时,我的应用程序运行时会出错.

我的pom例子:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/>
</parent>

<dependencies>
        ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        ...
</dependencies>
Run Code Online (Sandbox Code Playgroud)

当我尝试添加最新版本的Spring Data JPA时:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.11.6.RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

当我启动我的应用程序时,我得到了erorrs.像这样的错误:

Caused by: java.lang.NoSuchMethodException: org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.<init>()
  at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_121]
  at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_121]
  at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
  ... 53 common frames omitted
Run Code Online (Sandbox Code Playgroud)

如何使用较新版本的Spring Data JPA?我需要@DomainEvents在我的应用程序中.谢谢!

spring dependency-management maven spring-data-jpa spring-boot

22
推荐指数
1
解决办法
3093
查看次数

来自 kotlin 的自定义 json 序列化 Java 原语,作者:Jackson

在我的项目中有 java 和 kotlin 模块。在某些 kotlin 模块中,我需要自定义序列化。所有长值都应序列化为字符串。我用杰克逊。

例子:

科特林

data class KotlinRecord(val id: Long)
Run Code Online (Sandbox Code Playgroud)

爪哇

public class JavaRecord {
    private Long id;

    public Long getId() {
        return id;
    }

    public JavaRecord setId(Long id) {
        this.id = id;
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)

当在java模块中配置 ObjectMapper并序列化值时,如下所示:

 ObjectMapper mapper = new ObjectMapper()
            .registerModule(new KotlinModule())
            .registerModule(new JavaTimeModule())
            .registerModule(
                    new SimpleModule()
                            .addSerializer(Long.class, LongToStringJSONSerializer.ofObject())
                            .addSerializer(long.class, LongToStringJSONSerializer.ofPrimitive())
            );

JavaRecord javaRecord = new JavaRecord().setId(Long.MAX_VALUE);
KotlinRecord kotlinRecord = new KotlinRecord(Long.MAX_VALUE);

System.out.println("Java record:   " + mapper.writeValueAsString(javaRecord));
System.out.println("Kotlin record: " + …
Run Code Online (Sandbox Code Playgroud)

java primitive jackson kotlin json-serialization

3
推荐指数
1
解决办法
4042
查看次数

无法使用 spring boot gradle 插件执行依赖管理的 junit5 测试

在我基于 gradle 和 kotlin 的项目中,我有一些子模块。为它模块构建配置(build.gradle.kts):

plugins {
    java
    kotlin("jvm")
    id("org.springframework.boot") version "2.1.5.RELEASE"
    id("io.spring.dependency-management") version "1.0.7.RELEASE"
}

dependencies {

    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31")
    compile("org.springframework.boot:spring-boot-starter-web")

    testCompile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31")
    testCompile("org.jetbrains.kotlin:kotlin-stdlib:1.3.31")
    testCompile("org.jetbrains.kotlin:kotlin-reflect:1.3.31")

    testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
}
Run Code Online (Sandbox Code Playgroud)

我尝试在这个模块中运行一些简单的测试

简单类:

class Hello {
    fun hello(): String {
        return "hello"
    }
}
Run Code Online (Sandbox Code Playgroud)

对其进行简单测试:

class HelloTest {
    @Test
    fun test() {
        Assertions.assertEquals(Hello().hello(), "hello")
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行测试时,出现错误:

Jun 12, 2019 2:58:31 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;
    at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.createAbortedExecutionPredicate(OpenTest4JAndJUnit4AwareThrowableCollector.java:40)
    at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.<clinit>(OpenTest4JAndJUnit4AwareThrowableCollector.java:30)
    at org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector(JupiterThrowableCollectorFactory.java:34)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:68)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) …
Run Code Online (Sandbox Code Playgroud)

gradle kotlin spring-boot spring-boot-gradle-plugin junit5

3
推荐指数
1
解决办法
1094
查看次数