【问题标题】:Custom converter to avoid the exception of IllegalAccessException while mapping XMLGregorianCalendarImpl in dozer自定义转换器以避免在推土机中映射 XMLGregorianCalendarImpl 时出现 IllegalAccessException 异常
【发布时间】:2015-06-12 14:32:52
【问题描述】:

最初我遇到以下异常:我使用的是推土机 5.4。我的类路径中有 xerces jar 文件。我是推土机的新手,因此非常感谢任何形式的帮助。

org.dozer.MappingException: java.lang.IllegalAccessException: 类 org.dozer.util.ReflectionUtils 无法使用修饰符“public”访问 org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl 类的成员

我从该站点的其他帖子中了解到,解决上述问题的方法是为 XmlGregorianCalender 编写一个自定义转换器。

以下是自定义转换器的代码。目前正在调用 convertFrom 方法,并且传递的所有值都是 null。

自定义转换器:

 import javax.xml.datatype.XMLGregorianCalendar;
    
    import org.dozer.DozerConverter;
    
    public class XMLGregorianCalendarCustomConvertor extends
    DozerConverter<XMLGregorianCalendar, XMLGregorianCalendar>{
    public XMLGregorianCalendarCustomConvertor() {
        super(XMLGregorianCalendar.class, XMLGregorianCalendar.class);
        // TODO Auto-generated constructor stub
    }

    @Override
    public XMLGregorianCalendar convertTo(XMLGregorianCalendar source,
            XMLGregorianCalendar destination) {
        if (source == null) {
            return null;
            }
        else{
            return source;
        }
    }

    @Override
    public XMLGregorianCalendar convertFrom(XMLGregorianCalendar source,
            XMLGregorianCalendar destination) {
        if(destination == null){
            
            return null;
        }
        else{
            return destination;
        }
    }

}

映射xml

<configuration>
     <custom-converters> 
        <converter type="com.code.user.XMLGregorianCalendarCustomConvertor"  >
             <class-a>javax.xml.datatype.XMLGregorianCalendar</class-a>
              <class-b>javax.xml.datatype.XMLGregorianCalendar</class-b>
         </converter>
    </custom-converters>     
 </configuration>

【问题讨论】:

    标签: dozer


    【解决方案1】:

    通过引用复制它怎么样?如果它适合您,您可以这样做:

    <configuration>
        <copy-by-references>
          <copy-by-reference>
            javax.xml.datatype.XMLGregorianCalendar
          </copy-by-reference>
        </copy-by-references>
      </configuration>
    

    【讨论】:

    • 以上对我不起作用。请您详细说明上述内容或提供其他解决方案吗?
    【解决方案2】:

    问题是类:

    org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl
    不是以下的公共实现:
    javax.xml.datatype.XMLGregorianCalendar

    解决方案是简单地将 is-accessible="true" 属性添加到推土机转换器配置中的目标类,如下所示:

    Mapping.xml

    <configuration>
         <custom-converters> 
            <converter type="com.code.user.XMLGregorianCalendarCustomConvertor"  >
                 <class-a>javax.xml.datatype.XMLGregorianCalendar</class-a>
                  <class-b is-accessible="true">javax.xml.datatype.XMLGregorianCalendar</class-b>
             </converter>
        </custom-converters>     
     </configuration>
    

    希望对大家有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多