Question

I've seen a lot of things about how to unmarshall multiple root elements in a single response. My problem is a little different. The API I need to contact is set up a little strangely. Even if there is an error, the server will return 200, but it will return the error message in XML format like this:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Number></Number>
    <Source></Source>
    <Description></Description>
    <HelpFile/>
    <HelpContext/>
</Error>

However, the expected response is something more like:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Number></Number>
    <LabelImage></LabelImage>
    <Name></Name>
    <Address1/>
    <Address2></Address2>
    <City></City>
    <State></State>
    <Zip></Zip>
    <LabelSequenceNumber></LabelSequenceNumber>
    <DropOff></DropOff>
    <LogMessage/>
</Response>

I'm trying to figure out how I can accept either of these responses from the same request.

I'm using JaxB and RestTemplate to handle these requests and responses. The API I need to talk to is some kind of awful combination of Rest and SOAP, but I don't have any control over that part.

Was it helpful?

Solution

We solved this issue by creating an interface for each of the classes representing a response to extend. I don't know why I didn't think of this before, my understanding of JaxB was non-existant at the time. When receiving the response, it was checked for the word "Error", and if found, was unmarshalled into the ErrorResponse class, otherwise it was unmarshalled into the success response.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top