原创

spring-kafka 从 2.9.9 升级到 3.1.3 中 lambda 表达式中的参数类型不兼容

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I try to upgrade my app from Spring 2.7.12 to 3.2.3 and jdk 11 to jdk 17. the application successfully build but there is some integration issue. and when I upgrade spring-kafka version from 2.9.9 to 3.1.3 I receive this error:

... /KafkaConfig.java:92: error: incompatible types: incompatible parameter types in lambda expression
        return record -> {
               ^

The method is :

@Bean
    public RecordInterceptor<Object, Object> recordInterceptor() {
        return record -> {
            val value = record.value();
            MDC.clear();
            if (value != null) {
                MDC.put(ID, ((EvnValue) value).getMetadata().getId());
            }

            return record;
        };
    }

My build.gradle include this:

 plugins {
    ...
    id 'org.springframework.boot' version '3.2.3'
    id 'io.spring.dependency-management' version '1.1.4'
    id 'java'
...
}

and dependencies.gradle:

    // Spring Boot
    implementation "org.springframework.boot:spring-boot-starter-actuator"
    implementation "org.springframework.boot:spring-boot-starter-aop"
    implementation "org.springframework.boot:spring-boot-starter-cache"

    implementation "org.springframework.boot:spring-boot-starter-security"
    implementation "org.springframework.boot:spring-boot-starter-validation"
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation "org.springframework.boot:spring-boot-starter-web-services"
    implementation "org.springframework.boot:spring-boot-starter-log4j2"

    implementation "org.springframework.ws:spring-ws-core"

    //wsdl to java gen
    implementation(files(genJaxb.classesDir).builtBy(genJaxb))
    implementation 'junit:junit:4.13.2'
    implementation 'org.junit.jupiter:junit-jupiter:5.9.2'
    implementation 'org.testng:testng:7.9.0'
    jaxb 'org.glassfish.jaxb:jaxb-runtime:4.0.4'
    jaxb 'org.glassfish.jaxb:jaxb-xjc:4.0.4'

    //kafka dependencies
    implementation 'org.apache.kafka:kafka-clients:3.6.0'
    implementation 'org.springframework.kafka:spring-kafka:3.1.3'
    implementation 'io.confluent:kafka-avro-serializer:6.2.10'
    implementation 'org.apache.avro:avro:1.11.1'

    testImplementation 'com.tngtech.archunit:archunit-junit5:0.23.1'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }

    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'org.springframework.ws:spring-ws-test'

    testImplementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    testImplementation 'com.h2database:h2:2.1.214'

    testImplementation "org.projectlombok:lombok:1.18.28"
    testAnnotationProcessor("org.projectlombok:lombok:1.18.28")

    integrationTestImplementation "com.github.tomakehurst:wiremock-jre8:2.35.0"
    integrationTestImplementation('org.springframework.kafka:spring-kafka-test:3.1.3')
    integrationTestImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }

if I get back spring-kafka version to 2.9.9 while I already upgrade spring to 3 and jdk to 17 the build get successful but failed in integration test with this error as an example:

java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@5358c8fa testClass = X.tests.kafka.JWTErrorPathTest, locations = [], classes = [X.Application], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true", "server.port=0"], contextCustomizers = [org.springframework.kafka.test.context.EmbeddedKafkaContextCustomizer@2c6c2c15, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@55044b34, [ImportsContextCustomizer@1a02ebe0 key = [@org.springframework.test.context.BootstrapWith(value=org.springframework.boot.test.context.SpringBootTestContextBootstrapper.class), @org.springframework.boot.autoconfigure.EnableAutoConfiguration(exclude={org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class}, excludeName={}), @org.springframework.context.annotation.Import(value={org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar.class}), @org.springframework.boot.test.context.SpringBootTest(args={}, classes={X.Application.class}, properties={}, useMainMethod=NEVER, value={}, webEnvironment=RANDOM_PORT), @org.springframework.boot.autoconfigure.AutoConfigurationPackage(basePackageClasses={}, basePackages={}), @org.springframework.context.annotation.Import(value={org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.class})]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@300b, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@43786627, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@6d001, org.springframework.boot.test.context.SpringBootTestAnnotation@9609e468], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]

I did some changes for lambda expression but still no success. I was wondering someone can help me for this?

正文到此结束
热门推荐
本文目录