Announcement Announcement Module
Collapse
No announcement yet.
Unmarshalling Multiple Classes with Same Root Element Name Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unmarshalling Multiple Classes with Same Root Element Name

    I have two classes annotated for jaxb that share the same root element name. When using the RestTemplate to unmarshall each type separately, it appears the Jaxb2RootElementHttpMessageConverter ignores the responseType class and tries to unmarshall the xml response as one of its choosing. As a result, the unmarshaller isn't choosing the correct one, as both classes are registered with the jaxbcontext, and leads to a ClassCastException. Is this a bug?

    Code:
    public abstract class ResponseBase {
    
    }
    
    @XmlRootElement(name="response")
    public class ResponseA extends ResponseBase {
    
    }
    
    @XmlRootElement(name="response")
    public class ResponseB extends ResponseBase {
    
    }
    
    @Test
    public void test() {
        RestTemplate rest = new RestTemplate();
        ResponseA response = rest.getForObject("url", ResponseA.class);
    
        // causes ClassCastException
        ResponseB response = rest.getForObject("url2", ResponseB.class);
    
    }
Working...
X