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

使用doze-core 7.0.0映射XMLGregorianCalendar遇InaccessibleObjectException求助

解决Dozer映射XMLGregorianCalendar在JDK17下的访问错误

方案一:修正JVM参数

错误核心是java.xml模块未将com.sun.org.apache.xerces.internal.jaxp.datatype包开放给未命名模块,你现有的JVM参数仅开放了父包,需补充子包的开放配置:

在Tomcat的JVM参数中添加:

--add-opens=java.xml/com.sun.org.apache.xerces.internal.jaxp.datatype=ALL-UNNAMED

完整参数可合并为:

--add-opens=java.base/java.lang=ALL-UNNAMED 
--add-opens=java.base/java.io=ALL-UNNAMED 
--add-opens=java.base/java.util=ALL-UNNAMED 
--add-opens=java.base/java.util.concurrent=ALL-UNNAMED 
--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED 
--add-opens=java.xml/com.sun.org.apache.xerces.internal.jaxp=ALL-UNNAMED 
--add-opens=java.xml/com.sun.org.apache.xerces.internal.jaxp.datatype=ALL-UNNAMED 
--add-opens=java.base/java.lang.reflect=ALL-UNNAMED

方案二:自定义Dozer转换器(不依赖JVM参数)

若不想修改JVM参数,可通过自定义转换器绕过Dozer的反射属性复制逻辑,直接处理XMLGregorianCalendar的转换:

  1. 实现Dozer转换器类:
import com.github.dozermapper.core.DozerConverter;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class XMLGregorianCalendarConverter extends DozerConverter<XMLGregorianCalendar, XMLGregorianCalendar> {

    public XMLGregorianCalendarConverter() {
        super(XMLGregorianCalendar.class, XMLGregorianCalendar.class);
    }

    @Override
    public XMLGregorianCalendar convertTo(XMLGregorianCalendar source, XMLGregorianCalendar destination) {
        if (source == null) {
            return null;
        }
        try {
            DatatypeFactory factory = DatatypeFactory.newInstance();
            return factory.newXMLGregorianCalendar(
                source.getYear(),
                source.getMonth(),
                source.getDay(),
                source.getHour(),
                source.getMinute(),
                source.getSecond(),
                source.getMillisecond(),
                source.getTimezone()
            );
        } catch (Exception e) {
            throw new RuntimeException("转换XMLGregorianCalendar失败", e);
        }
    }

    @Override
    public XMLGregorianCalendar convertFrom(XMLGregorianCalendar source, XMLGregorianCalendar destination) {
        return convertTo(source, destination);
    }
}
  1. 注册转换器:
  • 若使用XML配置,在Dozer映射文件中添加:
<configuration>
  <custom-converters>
    <converter type="com.yourpackage.XMLGregorianCalendarConverter"/>
  </custom-converters>
</configuration>
  • 若使用注解,在需要转换的字段上添加@Converter(type = XMLGregorianCalendarConverter.class),或通过配置类全局注册转换器。

方案三:升级Dozer版本

检查Dozer最新版本(如7.5.x及以上)是否已针对JDK模块系统优化了XMLGregorianCalendar的转换逻辑,避免直接反射访问内部类方法。升级后可能无需额外配置即可解决问题。

内容的提问来源于stack exchange,提问作者cs_zineb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 22:47:07