java - 在访问器(getter)方法上使用转换器时出现异常

标签 java eclipselink moxy

我正在尝试使用自定义转换器为此SupplierOrderCharge 对象创建某种聚合,我将在其中汇总列表中的所有值。

如果我删除变压器注释,下面列出的异常就会消失。 这是有效的配置吗?我可以为这样的 getter 方法放置一个转换器吗?或者这可能是 MOXy 中的一个错误?

这是我的代码:

@XmlElement
@XmlTransformation
@XmlReadTransformer(transformerClass = GeneralAttributeMarkerTransformer.class)
@XmlWriteTransformers({ @XmlWriteTransformer(xmlPath = "ORDER_HEADER/SHIPPING_INFO/SHIPMENT_CODE/text()", transformerClass = ShippingInfoTransformer.class),
@XmlWriteTransformer(xmlPath = "ORDER_HEADER/SHIPPING_INFO/SHIPPING_COST_EX_VAT/text()", transformerClass = ShippingInfoTransformer.class),
@XmlWriteTransformer(xmlPath = "ORDER_HEADER/SHIPPING_INFO/SHIPPING_COST_VAT/text()", transformerClass = ShippingInfoTransformer.class),
@XmlWriteTransformer(xmlPath = "ORDER_HEADER/SHIPPING_INFO/SHIPPING_COST_INC_VAT/text()", transformerClass = ShippingInfoTransformer.class), })
@Transient
protected SupplierOrderCharge getShippingInfo() {
    for (final SupplierOrderCharge soCharge : getOrderCharges()) {
        if (EntryType.SHIPPING.equals(soCharge.getEntryType())) {
            return soCharge;
        }
    }
    return null;
}

我得到这个异常:

javax.xml.bind.JAXBException: 
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [] or [getShippingInfo] is not defined in the object [com.debijenkorf.data.model.supplier.SupplierOrder].
Internal Exception: java.lang.NoSuchMethodException: com.debijenkorf.data.model.supplier.SupplierOrder.(com.debijenkorf.data.model.supplier.SupplierOrderCharge)
Mapping: org.eclipse.persistence.oxm.mappings.XMLTransformationMapping[shippingInfo]
Descriptor: XMLDescriptor(com.debijenkorf.data.model.supplier.SupplierOrder --> [DatabaseTable(SALES_ORDER)])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException

- with linked exception:
[Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [] or [getShippingInfo] is not defined in the object [com.debijenkorf.data.model.supplier.SupplierOrder].
Internal Exception: java.lang.NoSuchMethodException: com.debijenkorf.data.model.supplier.SupplierOrder.(com.debijenkorf.data.model.supplier.SupplierOrderCharge)
Mapping: org.eclipse.persistence.oxm.mappings.XMLTransformationMapping[shippingInfo]
Descriptor: XMLDescriptor(com.debijenkorf.data.model.supplier.SupplierOrder --> [DatabaseTable(SALES_ORDER)])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException
]
at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:771)
at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:711)
at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:157)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:134)
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 javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:128)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:249)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
at com.debijenkorf.services.core.JaxbMoxyMarUn.marshal(JaxbMoxyMarUn.java:48)
at com.debijenkorf.services.fc4.JaxbMarUnFC1Test.testMarshal(JaxbMarUnFC1Test.java:36)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [] or [getShippingInfo] is not defined in the object [com.debijenkorf.data.model.supplier.SupplierOrder].
Internal Exception: java.lang.NoSuchMethodException: com.debijenkorf.data.model.supplier.SupplierOrder.(com.debijenkorf.data.model.supplier.SupplierOrderCharge)
Mapping: org.eclipse.persistence.oxm.mappings.XMLTransformationMapping[shippingInfo]
Descriptor: XMLDescriptor(com.debijenkorf.data.model.supplier.SupplierOrder --> [DatabaseTable(SALES_ORDER)])

Runtime Exceptions: 
---------------------------------------------------------

java.lang.NullPointerException

at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:638)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:574)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:533)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:777)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:735)
at org.eclipse.persistence.oxm.XMLContext$XMLContextState.<init>(XMLContext.java:857)
at org.eclipse.persistence.oxm.XMLContext$XMLContextState.<init>(XMLContext.java:832)
at org.eclipse.persistence.oxm.XMLContext.<init>(XMLContext.java:193)
at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:797)
at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:769)
... 37 more

最佳答案

这里可能有一个错误。您介意在下面的链接中输入一个,并且可以提供一个示例来演示您遇到的问题吗?

我在下面添加了一些示例代码,可以正确地为我创建一个 XmlTransformationMapping。我已完成以下操作:

  • 删除了 @XmlElement 注释。
  • 公开了 getShippingInfo() 方法。
  • 添加了 setShippingInfo(SupplierOrderCharge s) 方法。
package forum13075432;

import java.util.List;
import org.eclipse.persistence.oxm.annotations.*;

public class Root {

    @XmlTransformation
    @XmlReadTransformer(transformerClass = GeneralAttributeMarkerTransformer.class)
    @XmlWriteTransformers({
            @XmlWriteTransformer(xmlPath = "ORDER_HEADER/SHIPPING_INFO/SHIPMENT_CODE/text()", transformerClass = ShippingInfoTransformer.class),
            @XmlWriteTransformer(xmlPath = "ORDER_HEADER/SHIPPING_INFO/SHIPPING_COST_EX_VAT/text()", transformerClass = ShippingInfoTransformer.class),
            @XmlWriteTransformer(xmlPath = "ORDER_HEADER/SHIPPING_INFO/SHIPPING_COST_VAT/text()", transformerClass = ShippingInfoTransformer.class),
            @XmlWriteTransformer(xmlPath = "ORDER_HEADER/SHIPPING_INFO/SHIPPING_COST_INC_VAT/text()", transformerClass = ShippingInfoTransformer.class), })
    public SupplierOrderCharge getShippingInfo() {
        for (final SupplierOrderCharge soCharge : getOrderCharges()) {
            if (EntryType.SHIPPING.equals(soCharge.getEntryType())) {
                return soCharge;
            }
        }
        return null;
    }

    public void setShippingInfo(SupplierOrderCharge s) {
    }

    private List<SupplierOrderCharge> getOrderCharges() {
        // TODO Auto-generated method stub
        return null;
    }

}

关于java - 在访问器(getter)方法上使用转换器时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13075432/

相关文章:

java - jetty 服务器错误

java - switch 语句的效率,Java

java - JAXB/MOXy : Wrong namespace for element

java - junit 的异常 JPA derby 配置?

java - 为什么我不能将这个简单的对象映射到 Java/Jersey 中的 XML 文本?

java - EclipseLink MOXy 是否适合大得离谱的 XML 文件?

java - 更改 ActionBarActivity 中的 fragment

Java正则表达式捕获特定字符串后的字符串

java - 确定哪个实体导致了乐观锁异常

java - JAXB:解码异构数组