4

我正在尝试使用推土机映射将一种类型转换为另一种类型。我已经定义了一些像这样的bean:

public class OneBean extends TwoBean {

    private static final long serialVersionUID = 1L;

    public OneBean() {
    }

    private Short idOne;

   public final Short getIdOne() {
        return idOne;
    }

    public final void setIdOne(Short idOne) {
        this.idOne = idOne;
    }
}


public class TwoBean extends ThreeBean{

    private static final long serialVersionUID = 1L;

    public TwoBean() {
    }

    private Short idTwo;

   public final Short getIdTwo() {
        return idTwo;
    }

    public final void setIdTwo(Short idTwo) {
        this.idTwo = idTwo;
    }
}
public class ThreeBean  {

    private static final long serialVersionUID = 1L;

    public ThreeBean() {
    }

    private Short idThree;

    public final Short getIdThree() {
        return idThree;
    }

    public final void setIdThree(Short idThree) {
        this.idThree = idThree;
    }
}

public class OneTypeBean extends TwoTypeBean {

    private static final long serialVersionUID = 1L;


    public OneTypeBean() {
    }

    private Short idOne;

    public final Short getIdOne() {
        return idOne;
    }

    public final void setIdOne(Short idOne) {
        this.idOne = idOne;
    }
}


public class TwoTypeBean {

    private static final long serialVersionUID = 1L;

    public TwoTypeBean() {
    }

    private Short idTwo;
    private Short idThree;

    public final Short getIdTwo() {
        return idTwo;
    }

    public final void setIdTwo(Short idTwo) {
        this.idTwo = idTwo;
    }



    public final Short getIdThree() {
        return idThree;
    }

    public final void setIdThree(Short idThree) {
        this.idThree = idThree;
    }

}

推土机文件是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="
        http://dozer.sourceforge.net 
        http://dozer.sourceforge.net/schema/beanmapping.xsd">

    <mapping>
        <class-a>mypackage.TwoTypeBean</class-a>
        <class-b>mypackage.TwoBean</class-b>
        <field>
            <a>idTwo</a>
            <b>idTwo</b>
        </field>
        <field>
            <a>idThree</a>
            <b>idThree</b>
        </field>
    </mapping>
    <mapping>
        <class-a>mypackage.OneTypeBean</class-a>
        <class-b>mypackage.OneBean</class-b>
        <field>
            <a>idOne</a>
            <b>idOne</b>
        </field>
    </mapping>
</mappings>     

然后,我尝试在 OneTypeBean 对象中转换 OneBean 对象,如下所示:

private OneTypeBean convertBeanToType(TwoBean twoBean) {

    if (twoBean instanceof OneBean) { 
        //OneBean one = (OneBean) twoBean;
        return convert(twoBean, OneTypeBean.class);
    }
}

在我看来一切都很好(可能是层次结构的问题??),但是,我发现了这个错误:

    [#|2012-10-23T12:45:45.542+0000|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=32;_ThreadName=httpSSLWorkerThread-8080-4;|un.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:872)
    at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
    at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
    at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
    at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:264)
    at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
Caused by: org.dozer.MappingException: java.lang.NoSuchMethodException: [B.<init>()
    at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:82)
    at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:261)
    at org.dozer.factory.ConstructionStrategies$ByConstructor.create(ConstructionStrategies.java:245)
    at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:65)
    at org.dozer.MappingProcessor.mapCustomObject(MappingProcessor.java:477)
    at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:434)
    at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:330)
    at org.dozer.MappingProcessor.mapField(MappingProcessor.java:276)
    at org.dozer.MappingProcessor.map(MappingProcessor.java:245)
    at org.dozer.MappingProcessor.processSuperTypeMapping(MappingProcessor.java:999)
    at org.dozer.MappingProcessor.map(MappingProcessor.java:234)
    at org.dozer.MappingProcessor.map(MappingProcessor.java:187)
    at org.dozer.MappingProcessor.map(MappingProcessor.java:124)
    at org.dozer.MappingProcessor.map(MappingProcessor.java:119)
    at org.dozer.DozerBeanMapper.map(DozerBeanMapper.java:111)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:362)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter.doHandle(AnnotationMethodHandlerAdapter.java:349)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter.handleAction(AnnotationMethodHandlerAdapter.java:283)
    at org.springframework.web.portlet.DispatcherPortlet.doActionService(DispatcherPortlet.java:641)
    at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:519)
    at org.springframework.web.portlet.FrameworkPortlet.processAction(FrameworkPortlet.java:460)
    at com.sun.portal.portletcontainer.appengine.PortletAppEngineServlet.service(PortletAppEngineServlet.java:264)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:333)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at com.sun.portal.portletcontainer.appengine.PortletAppEngineFilter.doFilter(PortletAppEngineFilter.java:105)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:871)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:723)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:679)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:594)
    at com.sun.portal.portletcontainer.impl.PortletContainer.invokePAE(PortletContainer.java:819)
    at com.sun.portal.portletcontainer.impl.PortletContainer.invokePAE(PortletContainer.java:694)
    at com.sun.portal.portletcontainer.impl.PortletContainer.executeAction(PortletContainer.java:339)
    at com.liferay.portal.portletcontainer.WindowInvoker.invokeAction(WindowInvoker.java:267)
    at com.liferay.portlet.InvokerPortletImpl.processAction(InvokerPortletImpl.java:357)
    at com.liferay.portal.action.LayoutAction.processPortletRequest(LayoutAction.java:591)
    at com.liferay.portal.action.LayoutAction.processLayout(LayoutAction.java:419)
    ... 96 more
Caused by: java.lang.NoSuchMethodException: [B.<init>()
    at java.lang.Class.getConstructor0(Class.java:2706)
    at java.lang.Class.getDeclaredConstructor(Class.java:1985)
    at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:257)
    ... 146 more

|#]

我不知道我还能做什么,我找不到错误。任何帮助..?谢谢!

编辑:

正如我在http://dozer.sourceforge.net/documentation/baseattributes.html中看到的,也许我应该把我的推土机换成这样的东西......

仍然没有工作,但正在进行中......

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="
        http://dozer.sourceforge.net 
        http://dozer.sourceforge.net/schema/beanmapping.xsd">

    <mapping>
        <class-a>mypackage.TwoTypeBean</class-a>
        <class-b>mypackage.ThreeBean</class-b>
        <field>
            <a>idThree</a>
            <b>idThree</b>
        </field>
    </mapping>
    <mapping>
        <class-a>mypackage.OneTypeBean</class-a>
        <class-b>mypackage.OneBean</class-b>
        <field>
            <a>idTwo</a>
            <b>idTwo</b>
        </field>
        <field>
            <a>idOne</a>
            <b>idOne</b>
        </field>
    </mapping>
</mappings> 
4

3 回答 3

3

这里报告了一个类似的问题,也许它可以帮助你: - java.lang.NoSuchMethodException

于 2012-10-23T12:11:39.133 回答
1

答案是我在问题中编辑的那个:

正如我在http://dozer.sourceforge.net/documentation/baseattributes.html中看到的,我应该把我的推土机换成这样的东西......

 <?xml version="1.0" encoding="UTF-8"?>
 <mappings xmlns="http://dozer.sourceforge.net"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
    http://dozer.sourceforge.net 
    http://dozer.sourceforge.net/schema/beanmapping.xsd">

<mapping>
    <class-a>mypackage.TwoTypeBean</class-a>
    <class-b>mypackage.ThreeBean</class-b>
    <field>
        <a>idThree</a>
        <b>idThree</b>
    </field>
</mapping>
<mapping>
    <class-a>mypackage.OneTypeBean</class-a>
    <class-b>mypackage.OneBean</class-b>
    <field>
        <a>idTwo</a>
        <b>idTwo</b>
    </field>
    <field>
        <a>idOne</a>
        <b>idOne</b>
    </field>
</mapping>

于 2012-10-24T11:32:59.077 回答
0

将 Dozer bean 映射器与 SOAP Web 服务一起使用时,我遇到了同样的问题。将文件附加到 SOAP 请求时出现问题。因为我只使用 bean mapper 来重定向请求,并且源对象可以被修改并没有打扰我,所以我通过声明类型转换器将原始引用作为副本返回(当前如果在 Dozer 中指定的转换器它优先于其他构造方法 - 我必须检查内部推土机逻辑)。

映射文件:

 <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://dozer.sourceforge.net
              http://dozer.sourceforge.net/schema/beanmapping.xsd">
        <configuration>
            <custom-converters> 
                <converter type="mypackage.DataHandlerCopyRefConverter">
                    <class-a>javax.activation.DataHandler</class-a>
                    <class-b>javax.activation.DataHandler</class-b>
                </converter>
            </custom-converters>
        </configuration>
    </mappings>  

转换器来源:

public class DataHandlerCopyRefConverter implements CustomConverter {

    @Override
    public Object convert(Object existingDestinationFieldValue,
            Object sourceFieldValue, Class<?> destinationClass,
            Class<?> sourceClass) {
        return sourceFieldValue;
    }
}

警告!DataHandler 是临时资源,不利于克隆和序列化。

于 2014-11-07T23:07:40.927 回答