JAXb does not populate object during unmarshal

I am a little lost at this point. I am by no means a SOAP / JAXb expert, however I am trying to create a generic class that will marshal / call / unmarshal for any service. I am using the wsdl weather service as a starting point for a proof of concept.

Finally I got marshalling, call and unmarshalling to execute without error, however the response object is not populating. Can anyone help in identifying what I am doing wrong? I am also looking for a good explanation of the answer if possible so that I can learn from this experience.

Again, there is no error when skipping. The problem is that the GetCityWeatherByZIPResponse.GetCityWeatherByZIPResult value is null. I know that the document is returning the correct results as the result looks like this:

Printout of results:

<?xml version="1.0" encoding="UTF-8"?><GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/">
    <GetCityWeatherByZIPResult>
        <Success>true</Success>
        <ResponseText>City Found</ResponseText>
        <State>MO</State>
        <City>Saint Charles</City>
        <WeatherStationCity>Farmington</WeatherStationCity>
        <WeatherID>4</WeatherID>
        <Description>Sunny</Description>
        <Temperature>79</Temperature>
        <RelativeHumidity>47</RelativeHumidity>
        <Wind>CALM</Wind>
        <Pressure>30.00S</Pressure>
        <Visibility/>
        <WindChill/>
        <Remarks/>
    </GetCityWeatherByZIPResult>
</GetCityWeatherByZIPResponse>

Response: GetCityWeatherByZIPResult: null

      

Testing the web service: http://wsf.cdyne.com/WeatherWS/Weather.asmx

Initial call (done via JBehave):

@Given("I call the weather soap service")
public void givenICallTheWeatherSoapService() {
    GetCityWeatherByZIP weather = new GetCityWeatherByZIP();
    weather.setZIP("63304");
    try {
        new WeatherTools();
        WeatherSoap weatherSoap = new WeatherSoap();
        GetCityWeatherByZIPResponse response = weatherSoap.getCityWeatherByZip("63304");
        System.out.println("Response: " + response);
    } catch (JAXBException | ParserConfigurationException | SOAPException | IOException e) {
        Assert.fail(e.getMessage());
    }
}

      

Soap service class:

public class WeatherSoap extends PTFSoapClient {

    public WeatherSoap() throws JAXBException, ParserConfigurationException, SOAPException {
        super(PTFApplication.getConfig(Environment.executionEnv.getEnv(), "Weather SOAP endpoint"));
    }

    public GetCityWeatherByZIPResponse getCityWeatherByZip(String zip) throws JAXBException, SOAPException, IOException {
        GetCityWeatherByZIP weatherByZip = new GetCityWeatherByZIP();
        weatherByZip.setZIP(zip);
        try {
            sendRequest(weatherByZip);
            return (GetCityWeatherByZIPResponse) unmarshallResponse(GetCityWeatherByZIPResponse.class);
        } catch (ParserConfigurationException | XMLStreamException e) {
            e.printStackTrace();
            return null;
        }
    }
}

      

Base Framework Class generating the call (used for all SOAP calls):

public class PTFSoapClient {
    private JAXBContext context;
    private Marshaller marshaller;
    private Object object;
    private SOAPMessage message;
    private String endpoint;
    private SOAPMessage response;

    public PTFSoapClient(String endpoint) {
        this.endpoint = endpoint;
    }

    public void toConsole() throws JAXBException, SOAPException, IOException {
        message.writeTo(System.out);
        System.out.print("\n");
    }

    public SOAPMessage sendRequest(Object obj) throws JAXBException, ParserConfigurationException, SOAPException {
        object = obj;
        context = JAXBContext.newInstance(obj.getClass());
        marshaller = context.createMarshaller();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        Document doc = dbf.newDocumentBuilder().newDocument();
        marshaller.marshal(object,doc);
        MessageFactory factory = MessageFactory.newInstance();
        message = factory.createMessage();
        message.getSOAPBody().addDocument(doc);
        message.saveChanges();

        SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
        response = connection.call(message, endpoint);
        connection.close();

        try {
            System.out.println("Response:");
            response.writeTo(System.out);
            System.out.println("");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    public Object unmarshallResponse(Class<?> classname) throws JAXBException, XMLStreamException, SOAPException, IOException {
        Document doc = response.getSOAPBody().extractContentAsDocument();
        try {
            System.out.println("Document: ");
            printDocument(doc, System.out);
            System.out.println("");
        } catch (TransformerException e) {
            e.printStackTrace();
        }

        Unmarshaller unmarshaller = JAXBContext.newInstance(classname).createUnmarshaller();
        return unmarshaller.unmarshal(doc);
    }

    public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        transformer.transform(new DOMSource(doc), 
             new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    }
}

      

Basic non-marching object:

@XmlRootElement(name = "GetCityWeatherByZIPResponse",
                namespace = "http://ws.cdyne.com/WeatherWS/")
public class GetCityWeatherByZIPResponse {
    GetCityWeatherByZIPResult GetCityWeatherByZIPResult;

    public GetCityWeatherByZIPResult getGetCityWeatherByZIPResult() {
        return GetCityWeatherByZIPResult;
    }

    public void setGetCityWeatherByZIPResult(GetCityWeatherByZIPResult GetCityWeatherByZIPResult) {
        this.GetCityWeatherByZIPResult = GetCityWeatherByZIPResult;
    }

    @Override
    public String toString() {
        return "GetCityWeatherByZIPResult: " + GetCityWeatherByZIPResult;
    }
}

      

Sub umarshal object:

public class GetCityWeatherByZIPResult {
    boolean Success;
    String ResponseText;
    String State;
    String City;
    String WeatherStationCity;
    String WeatherID;
    String Description;
    int Temperature;
    int RelativeHumidity;
    String Wind;
    String Pressure;
    String Visibility;
    String WindChill;
    String Remarks;

    public boolean isSuccess() {
        return Success;
    }

    public void setSuccess(boolean success) {
        Success = success;
    }

    public String getResponseText() {
        return ResponseText;
    }

    public void setResponseText(String responseText) {
        ResponseText = responseText;
    }

    public String getState() {
        return State;
    }

    public void setState(String state) {
        State = state;
    }

    public String getCity() {
        return City;
    }

    public void setCity(String city) {
        City = city;
    }

    public String getWeatherStationCity() {
        return WeatherStationCity;
    }

    public void setWeatherStationCity(String weatherStationCity) {
        WeatherStationCity = weatherStationCity;
    }

    public String getWeatherID() {
        return WeatherID;
    }

    public void setWeatherID(String weatherID) {
        WeatherID = weatherID;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public int getTemperature() {
        return Temperature;
    }

    public void setTemperature(int temperature) {
        Temperature = temperature;
    }

    public int getRelativeHumidity() {
        return RelativeHumidity;
    }

    public void setRelativeHumidity(int relativeHumidity) {
        RelativeHumidity = relativeHumidity;
    }

    public String getWind() {
        return Wind;
    }

    public void setWind(String wind) {
        Wind = wind;
    }

    public String getPressure() {
        return Pressure;
    }

    public void setPressure(String pressure) {
        Pressure = pressure;
    }

    public String getVisibility() {
        return Visibility;
    }

    public void setVisibility(String visibility) {
        Visibility = visibility;
    }

    public String getWindChill() {
        return WindChill;
    }

    public void setWindChill(String windChill) {
        WindChill = windChill;
    }

    public String getRemarks() {
        return Remarks;
    }

    public void setRemarks(String remarks) {
        Remarks = remarks;
    }
}

      

+3


source to share


1 answer


Your current mapping

When you specify a property namespace

in an annotation @XmlRootElement

, it only applies to one element.

@XmlRootElement(name = "GetCityWeatherByZIPResponse",
                namespace = "http://ws.cdyne.com/WeatherWS/")
public class GetCityWeatherByZIPResponse {

      

Your XML Document

The XML document specifies the default namespace. This means that all elements without other explicit namespace mapping are also part of the namespace http://ws.cdyne.com/WeatherWS/

.

<?xml version="1.0" encoding="UTF-8"?><GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/">
    <GetCityWeatherByZIPResult>
        <Success>true</Success>

      

Fixing the namespace

You need to specify the package-level namespace mapping so that it applies to all of your item mappings. This is done using a package level annotation @XmlSchema

in a special class called package-info

.

@XmlSchema( 
    namespace = "http://ws.cdyne.com/WeatherWS/", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

      

Additional Information



I wrote more about JAXB and namespace qualifications on my blog:


Update

Default element names

The default elements for your properties don't match your XML. for the property below the expected element name will be getCityWeatherByZIPResult

, so you will need to override the default with the annotation @XmlElement

.

@XmlElement(name="GetCityWeatherByZIPResult")
public GetCityWeatherByZIPResult getGetCityWeatherByZIPResult() {
    return GetCityWeatherByZIPResult;
}

      

Debugging tip

When you run into non-sorting issues, populate your object model and marshal it to see that the expected XML is based on your current mappings.

+2


source







All Articles