You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JDK8升级至11后ModelMapper映射异常问题求助

问题描述

将JDK版本从8升级至11,并把ModelMapper插件更新至2.3.9(尝试2.4.0也无效)后,仍出现ModelMapper映射异常。本地环境中getAll接口运行正常,但服务器环境下触发该异常。

异常信息

Exception Message: org.modelmapper.MappingException: ModelMapper mapping errors:1) Error mapping
at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:380)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:81)
at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:573)
at org.modelmapper.ModelMapper.map(ModelMapper.java:406)
at rest.translator.impl.MongoDbDocumentTranslatorImpl.toResponse(MongoDbDocumentTranslatorImpl.java:214)
at rest.translator.impl.MongoDbDocumentTranslatorImpl.translate(MongoDbDocumentTranslatorImpl.java:200)
at rest.translator.impl.UserInformationTranslatorImpl.translate(UserInformationTranslatorImpl.java:38)
at rest.controller.UserController.lambda$getAll$1(UserController.java:106)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)...

项目依赖配置(Gradle)

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
    compile 'org.springframework.boot:spring-boot-starter-security'
    compile 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.0.M4'
    compile 'org.springframework.boot:spring-boot-starter-log4j2'
    compile 'org.springframework.security:spring-security-oauth2-resource-server'
    compile 'org.springframework.security:spring-security-oauth2-jose'
    compile 'org.springframework.security:spring-security-config'
    compile 'org.springframework.retry:spring-retry'
    compile 'org.springframework.boot:spring-boot-starter-cache'
    compile 'org.springframework.boot:spring-boot-starter-data-redis'
    compile('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    compile 'io.micrometer:micrometer-registry-prometheus'

    compile 'com.nimbusds:oauth2-oidc-sdk:7.1.1'

    compile 'io.springfox:springfox-swagger2:2.9.2'
    compile 'io.springfox:springfox-swagger-ui:2.9.2'
    compile 'io.springfox:springfox-bean-validators:2.9.2'
    compile "gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.22.1"
    compile "gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.8"
    compile 'org.apache.commons:commons-lang3:3.0'
    compile 'commons-io:commons-io:2.6'
    compile 'org.aspectj:aspectjweaver'
    compile 'org.modelmapper:modelmapper:2.3.9'
    compile 'com.amazonaws:aws-java-sdk-s3:1.11.797'
    compile 'com.amazonaws:aws-java-sdk-core:1.11.797'
    compile 'org.assertj:assertj-core:3.6.2'
    runtime 'org.springframework.boot:spring-boot-starter-tomcat'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'org.springframework.security:spring-security-test'
    testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc'
    testCompile 'de.bwaldvogel:mongo-java-server:1.16.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
    testCompile group: 'io.findify', name: 's3mock_2.12', version: '0.2.6'
    compile 'io.cucumber:cucumber-java:4.5.3'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.4.2'
    compileOnly "org.springframework.boot:spring-boot-configuration-processor"
    // jdk 11 migration dependencies
    implementation "javax.xml.bind:jaxb-api:2.3.1"
    implementation "com.sun.xml.bind:jaxb-core:2.3.0.1"
    implementation "com.sun.xml.bind:jaxb-impl:2.3.1"
    implementation "com.sun.activation:javax.activation:1.2.0"
    implementation("javax.annotation:javax.annotation-api:1.3.2")
    compile group: 'org.ow2.asm', name: 'asm', version: '7.1'
    testCompile("javax.xml.ws:jaxws-api:2.3.1")
    implementation 'org.springframework.boot:spring-boot-test'
    implementation 'org.springframework.boot:spring-boot-test-autoconfigure'
    // end of jdk 11 migration dependencies
}
解决方案
  • 启用详细日志定位具体错误
    当前异常仅提示映射错误,没有具体字段/类信息。可以在代码中捕获异常并打印详细错误:

    try {
        modelMapper.map(source, targetClass);
    } catch (MappingException e) {
        e.getErrors().forEach(error -> System.err.println(error.getMessage()));
        throw e;
    }
    

    或在log4j2配置中添加ModelMapper的DEBUG日志,服务器环境下查看具体失败的映射项。

  • 调整JDK 11反射权限配置
    JDK 11反射限制更严格,若映射类包含私有字段或内部类,需配置ModelMapper的反射策略:

    ModelMapper modelMapper = new ModelMapper();
    modelMapper.getConfiguration()
        .setFieldMatchingEnabled(true)
        .setFieldAccessLevel(AccessLevel.PRIVATE);
    

    若使用模块系统,需在module-info.java中开放反射权限:

    opens your.package.to.map to org.modelmapper;
    
  • 排查服务器类加载与依赖冲突
    本地和服务器类加载器差异可能导致类找不到或版本冲突:

    1. 用gradle dependencyInsight --dependency org.modelmapper:modelmapper检查依赖树,排除冲突版本
    2. 确认服务器环境依赖包版本与本地完全一致
  • 显式配置映射规则
    自动映射可能因字段名、类型不匹配(如JDK8/11日期类型差异)失败,显式定义映射:

    modelMapper.typeMap(SourceClass.class, TargetClass.class)
        .addMapping(SourceClass::getCreateTime, TargetClass::setCreateTime);
    

    针对日期类型添加转换器:

    Converter<LocalDateTime, Date> localDateTimeToDate = context -> 
        Date.from(context.getSource().atZone(ZoneId.systemDefault()).toInstant());
    modelMapper.addConverter(localDateTimeToDate);
    
  • 升级ModelMapper至最新稳定版
    尝试升级到2.5.1(或更高稳定版),新版本修复了更多JDK11兼容问题,修改Gradle依赖:

    compile 'org.modelmapper:modelmapper:2.5.1'
    
  • 调整服务器JVM参数
    若因模块反射限制报错,添加JVM参数:

    --add-opens java.base/java.lang=ALL-UNNAMED
    --add-opens java.base/java.util=ALL-UNNAMED
    

    根据具体报错的模块调整参数。

内容的提问来源于stack exchange,提问作者hemanth reddy m

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:45:02