使用jaxb从对象生成xml时出错。 Eclipselink版本2.5.1

时间:2013-11-20 09:02:43

标签: java jaxb eclipselink

将对象转换为xml时出现此异常。这种情况间歇发生,而不是每次都发生。这是由于eclipselink 2.5.1版本中的任何错误吗?

Caused by: javax.xml.bind.JAXBException
 - with linked exception:
[java.lang.NullPointerException]
        at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1021)
        at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:174)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102)
        at sun.reflect.GeneratedMethodAccessor93.invoke(Unknown Source)
        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:250)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:447)
        at cts.emv.bind.JAXBTransformer.transformToXml(JAXBTransformer.java:88)
        ... 10 more
Caused by: java.lang.NullPointerException
        at org.eclipse.persistence.jaxb.JAXBContext$JAXBContextState.updateResolverForFields(JAXBContext.java:1252)
        at org.eclipse.persistence.jaxb.JAXBContext$JAXBContextState.processXMLDescriptor(JAXBContext.java:1235)
        at org.eclipse.persistence.jaxb.JAXBContext$JAXBContextState.processRefClasses(JAXBContext.java:1219)
        at org.eclipse.persistence.jaxb.JAXBContext$JAXBContextState.processXMLDescriptor(JAXBContext.java:1243)
        at org.eclipse.persistence.jaxb.JAXBContext$JAXBContextState.updateNamespaces(JAXBContext.java:1208)
        at org.eclipse.persistence.jaxb.JAXBContext$JAXBContextState.<init>(JAXBContext.java:1135)
        at org.eclipse.persistence.jaxb.JAXBContext$JAXBContextState.<init>(JAXBContext.java:1156)
        at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1051)
        at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1018)
        at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:174)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102)
        at sun.reflect.GeneratedMethodAccessor93.invoke(Unknown Source)
        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:263)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:250)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:447)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:652)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:599)
        at cts.emv.bind.JAXBTransformer.transformToXml(JAXBTransformer.java:87)
        at cts.ws.policy.WSAuthPolicy.getAuthDirectPaymentRequest(WSAuthPolicy.java:308)
        at cts.ws.policy.WSAuthPolicy.getPareResponseMsg(WSAuthPolicy.java:202)
        at cts.ws.policy.WSAuthPolicy.getUnAcknowledgedRequests(WSAuthPolicy.java:324)

相同的代码如下,它在此行失败JAXBContext jc = JAXBContext.newInstance(clazz);

public String transformToXml(Object pObject, boolean validate) throws
XMLProcessingException, XMLValidationException {

StringWriter writer = new StringWriter();
Class clazz = pObject.getClass();
ValidationErrorHandler handler = new ValidationErrorHandler();
try {
    JAXBContext jc = JAXBContext.newInstance(clazz);                                                
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-16");

    if(validate){
        Schema schema = SchemaValidator.getInstance().getSchema(clazz);
        if(schema == null){
            throw new XMLProcessingException(ExceptionSeverity.ERROR, this, "transformToXML", 
                "Error validating object. Schema for " + clazz.getName() + " not found.");
        }

        m.setEventHandler(handler);
        m.setSchema(schema);
    }


    m.marshal(pObject, writer);
}catch(JAXBException je){
    Throwable cause = je.getCause();
    if(cause != null){
        Throwable innerCause = cause.getCause();
        if(innerCause != null && innerCause instanceof MarshalSAXParseException){
            throw new XMLValidationException((Exception)innerCause, ExceptionSeverity.ERROR, this, "transformToXml", handler.getMessages());
    }
    throw new XMLProcessingException(je, ExceptionSeverity.ERROR, this, "transformToXML", 
            "Error transforming Object to XML");
    }
    throw new XMLProcessingException(je, ExceptionSeverity.ERROR, this, "transformToXML", 
            "Error transforming Object to XML");
}

return writer.toString();

Eclipse Persistence Services - 2.3.2.v20111125-r10461

@SuppressWarnings("serial")
@XmlRootElement(name="DirectPaymentConfirmationRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class DirectPaymentConfirmationRequest implements Serializable {

          @XmlElement(name="DirectPaymentReferenceId", nillable=false, required=true)
          private Long mDirectPaymentReferenceId;

          @XmlElement(name="PaymentCardTransactionType", nillable=false, required=true)
          private String mPaymentCardTransactionType;

          @XmlElement(name="Amount", nillable=false, required=true)
          private Integer mAmount;

          @XmlElement(name="MerchantId", nillable=false, required=true)
          private String mMerchantId;

          @XmlElement(name="AddressCheckRequested", nillable=false, required=true)
          private Boolean mAddressCheckRequested;

          @XmlElement(name="AuthorisationTimeStamp" , nillable=false, required=true)
          private Calendar mAuthorisationTimeStamp;

          @XmlElements({
              @XmlElement(name="AcquirerResponse",     type=DirectPaymentAcquirerResponse.class),
              @XmlElement(name="Error", type=Error.class)    
          })
          private Object mResponse;

          @XmlElement(name="CardDetail",required=false)
          private CardDetail mCardDetail;


@SuppressWarnings("serial")
@XmlRootElement(name="AcquirerResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class DirectPaymentAcquirerResponse extends AuthAuthorisationCode implements   Serializable {

          @XmlElement(name="AddressCheckResult", nillable=true, required=true)
          private String mAddressCheckResult;

          @XmlElement(name="CVVCheckResult", nillable=true, required=true)
          private String mCVVCheckResult;

          @XmlElement(name="ProductCode", nillable=true, required=true)
          private String mProductCode;

          @XmlElement(name="TraceId", nillable=true, required=true)
          private String mTraceId;

@SuppressWarnings("serial")
@XmlRootElement(name="Error")
@XmlAccessorType(XmlAccessType.FIELD)
public class Error implements Serializable {

   @XmlElement(name="ErrorCode", nillable=false, required=true)
   private Short mErrorCode;

   @XmlElement(name="ErrorDescription", nillable=false, required=true)
   private String mErrorDescription;

0 个答案:

没有答案