当前位置: 首页 > 知识库问答 >
问题:

解封期间JAXb未填充对象

金令
2023-03-14

同样,在导出时没有错误。问题是getCityWeatherbyZipresponse.getCityWeatherbyZipResult的值为null。我知道文档正在返回正确的结果,因为结果打印如下:

结果打印输出:

<?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

测试Web服务:http://wsf.cdyne.com/weatherws/weather.asmx

@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());
    }
}
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;
        }
    }
}
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")));
    }
}
@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;
    }
}

子umarshal对象:

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;
    }
}

共有1个答案

濮阳安澜
2023-03-14

@xmlrootelement注释上指定namespace属性时,它只适用于该元素。

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

您的XML文档指定默认命名空间。这意味着没有另一个显式命名空间映射的所有元素也是http://ws.cdyne.com/weatherws/命名空间的一部分。

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

您希望在包级别指定名称空间映射,以便它适用于所有元素映射。这是在一个名为package-info的特殊类上使用包级别@xmlschema注释完成的。

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

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

有关更多信息

我在博客上写了更多关于JAXB和命名空间限定的内容:

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

属性的默认元素与XML不匹配。对于下面的属性,预期的元素名称将是GetCityWeatherByZipResult,因此需要使用@xmlElement注释重写默认值。

@XmlElement(name="GetCityWeatherByZIPResult")
public GetCityWeatherByZIPResult getGetCityWeatherByZIPResult() {
    return GetCityWeatherByZIPResult;
}
 类似资料:
  • 我对JAXB很陌生,在解封一般对象时遇到了麻烦。问题是我需要能够封送和解封任何对象(java.lang.object)。我成功地进行了封送处理,但是当我运行解封处理时,响应中得到的是一个“ElementNSimpl”对象,而不是我自己的对象。 这是涉及的bean: message.java somebean.java jaxb.index的内容: 生成的xml很好()但是在解封后计算“msg.ge

  • 问题内容: 我正在编写一个使用许多不同组件的程序,因此我开始对其使用GridBagLayout。效果很好,但是当我运行程序时,我得到了: 当前布局图 应该将最右边的列表对象直接与其余对象对齐,并填充屏幕的整个宽度。不知道为什么不是。这是相关代码的简短副本(为简化起见,我决定将它们全部放在一个类中): 我包括了所有的NoteBox类,因为它是发生故障的类,并且我认为它是最相关的。 问题答案: 正如M

  • 我有如下所示的XML: 我有一个ObjectList类,它如下所示: 和如下所示的对象类: 当我尝试使用以下代码将xml解封到and对象中时: 我得到以下错误: UnMarshalException:意外元素(URI:“”,本地:“ObjectList”)。需要的元素为<{}Object>、<{}ObjectList> 编辑:我刚刚注意到两个问题我将我的ObjectList对象的XmlRootEl

  • 我正在尝试使用JAXB进行封送处理。 我的输出是这样的: ...但我需要这样的输出: 如果取消对代码的注释,则会得到。没有它,我可以编译,但我不能得到所需的精确输出。 我的豆子长这样: 适配器类

  • 我有这些课程: 用户 地址 输出 我如何解决这个问题?我是否可以使用一些JAXB注释?怎么做?还是需要创建某种类型的XmlAdapter?(我试过这个,但没有成功……)

  • 如何告诉JAXB将给定的XML完全解封到对象中?