Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

java - Netbeans with JAXB Random ClassCastException ..cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor

I have downloaded the Soap messages from a SOAP Service and trying to mock the Soap service by returning the downloaded messages. the following code shows how I am Unmarshalling the Soap message into the required Response

    public static DataClientType unmarshallFile(String fileName) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(ClientSampleSoapResponseData.class.getResourceAsStream(fileName));
    xsr.nextTag(); // Advance to Envelope tag
    xsr.nextTag(); // Advance to Header
    xsr.nextTag(); // Advance to Body tag
    xsr.nextTag(); // Advance to getClientByAccountResponse
    xsr.nextTag(); // Advance to content of getClientByAccountResponse

    JAXBContext jc = JAXBContext.newInstance(GetClientByAccountResponse.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    JAXBElement<GetClientByAccountResponse> je = unmarshaller.unmarshal(xsr, GetClientByAccountResponse.class);

    return je.getValue().getClientDataContract();
}

However I keep getting this ClassCastExeption which happens randomly. After a number of test iterations, it begins to happen. Sometimes a clean and build fixes it but sometimes it doesn't work.

java.lang.ClassCastException: com.x.X.X.X.GetClientByAccountResponse$JaxbAccessorF_clientDataContract cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.instanciate(OptimizedAccessorFactory.java:188)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:180)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:256)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:90)

I have tried other online suggestions such as reverting to old jaxb versions and using endorsed folders in the maven compiler configuration but it still happens

Any ideas on what could be causing it and possible solutions?

Thank u

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solved with the following code

@BeforeClass
public static  void init(){ 
    System.setProperty( "com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize", "true");
}

@AfterClass
public static void revert(){ 
    System.getProperties().remove("com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize");
}    

Parameter can also be set at JVM using

-Dcom.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize=true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...