Announcement Announcement Module
No announcement yet.
ClassCastException when using @XPathParam and ObjectFactory Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassCastException when using @XPathParam and ObjectFactory

    We've recently run into an exception with unmarshalling when using the Jaxb2Marshaller with contextPath set (using ObjectFactory). This only happens for methods where the Endpoint method uses XPathParam & MarshallingSource (Spring WS 1.5.9):

    @SoapAction(value = WebServiceConstants.SOAPACTIONS.SOME_METHOD)
    public Source getSomeObject(@XPathParam("/fs:someIdRequest") String someId) {
        SomeModelObject obj = someService.getSomeObject(Long.parseLong(someId));
        return new MarshallingSource(marshaller, obj);
    Here's the client method being used:

    public SomeObject getSomeObject(long someId) {
    	final String request = "<fs:someIdRequest xmlns:fs=\"" + WebServiceConstants.SOAPACTIONS.FOUNDATIONS_NAMESPACE + "\">" + someId + "</fs:someIdRequest>";
    	return ((SomeModelObject) webServiceTemplate.sendSourceAndReceive(new StringSource(request), new WebServiceMessageCallback() {
    		public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
    			((SoapMessage) webServiceMessage).setSoapAction(WebServiceConstants.SOAPACTIONS.SOME_METHOD);
    	}, marshallableSourceExtractor));
    The following Exception is thrown by the client method:

    java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to com.somecompany.someapp.model.SomeObject
    Other methods that use @SoapAction without @XPathParam work fine, and when we change the Jaxb2Marshaller to use "classesToBeBound" instead of "contextPath" the above method works fine too. However, we'd like to use ObjectFactory for maintainability purposes.

    Things that I've checked:
    1. The ObjectFactory does have a "create" method for the model object that is throwing the Exception.
    1. The model object does have an XmlRootElement annotation.

    Does anyone have any idea what might be causing the problem? Thanks in advance!
    Last edited by ach; May 2nd, 2011, 10:08 AM.

  • #2
    Ok, so it seems the issue is with the complexity of the model object. The ObjectFactory is being generated such that the "create" method returns a JAXBElement instead of the POJO.

    What are some reasons that might be the case? The xsd type corresponding to the object-in-question extends an abstract type (which extends another abstract type), and contains a sequence of two elements which also extend other abstract types. There are xsd minLength and maxLength restrictions used for some of the string elements.
    Last edited by ach; May 2nd, 2011, 04:00 PM.