Java JAXb在解组期间未填充对象
在这一点上我有点不知所措。我决不是SOAP/JAXb专家,但是,我正在尝试创建一个通用类,它将为任何服务封送/调用/解组。我使用天气服务wsdl作为起点来证明这个概念 我终于让编组、调用和解编能够无误地执行,但是,没有填充响应对象。有人能帮我找出我做错了什么吗?我也在寻找一个很好的解释,如果可能的话,这样我可以从这个经验中学习 同样,执行时没有错误。问题是GetCityWeatherBySipResponse.GetCityWeatherBySipResult的值为null。我知道文档返回了正确的结果,因为结果打印输出如下: 结果打印输出:Java JAXb在解组期间未填充对象,java,jaxb,unmarshalling,Java,Jaxb,Unmarshalling,在这一点上我有点不知所措。我决不是SOAP/JAXb专家,但是,我正在尝试创建一个通用类,它将为任何服务封送/调用/解组。我使用天气服务wsdl作为起点来证明这个概念 我终于让编组、调用和解编能够无误地执行,但是,没有填充响应对象。有人能帮我找出我做错了什么吗?我也在寻找一个很好的解释,如果可能的话,这样我可以从这个经验中学习 同样,执行时没有错误。问题是GetCityWeatherBySipResponse.GetCityWeatherBySipResult的值为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
Soap服务类:
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;
}
}
}
用于泛化调用的基本框架类(可用于所有SOAP调用):
子umarshal对象:
@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;
}
}
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;
}
}
您当前的映射
在@XmlRootElement
注释上指定名称空间
属性时,它仅适用于该元素
@XmlRootElement(name = "GetCityWeatherByZIPResponse",
namespace = "http://ws.cdyne.com/WeatherWS/")
public class GetCityWeatherByZIPResponse {
您的XML文档
XML文档指定一个默认名称空间。这意味着没有另一个显式名称空间映射的所有元素也是http://ws.cdyne.com/WeatherWS/
名称空间
<?xml version="1.0" encoding="UTF-8"?><GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/">
<GetCityWeatherByZIPResult>
<Success>true</Success>
了解更多信息
我在我的博客上写了更多关于JAXB和名称空间限定的内容:
更新 默认元素名称 属性的默认元素与XML不匹配。对于下面的属性,预期的元素名称将是
GetCityWeatherBySipResult
,因此您需要使用@XmlElement
注释覆盖默认值
@XmlElement(name="GetCityWeatherByZIPResult")
public GetCityWeatherByZIPResult getGetCityWeatherByZIPResult() {
return GetCityWeatherByZIPResult;
}
调试提示
如果在解组时遇到问题,请填充对象模型并进行封送处理,以查看基于当前映射的预期XML。我尝试了您指出的方法,并将package-info.java文件添加到com.pgi.qa.weathertools.domain,但仍然得到相同的结果@XmlSchema(namespace=”“,elementFormDefault=XmlNsForm.QUALIFIED)包com.pgi.qa.weathertools.domain;导入javax.xml.bind.annotation.XmlNsForm;导入javax.xml.bind.annotation.XmlSchema@Mordimer-我已经用一些信息更新了我的答案,这些信息应该会有所帮助。我现在离答案越来越近了。您提到的最后一个技巧(默认元素名)实际上得到了第一个主类填充。现在,GetCityWeatherBySipResult中的属性只是默认值(null、0、false)。我想知道这是不是同样的问题。我现在将深入研究这些问题,看看这是否解决了我的问题,并将结果发布在这里。是的,在子响应对象的所有getter中添加@xmlement行修复了问题的最后一部分。我感谢你的帮助和解释。这是一篇非常快速和有见地的文章。
<?xml version="1.0" encoding="UTF-8"?><GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/">
<GetCityWeatherByZIPResult>
<Success>true</Success>
@XmlSchema(
namespace = "http://ws.cdyne.com/WeatherWS/",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
@XmlElement(name="GetCityWeatherByZIPResult")
public GetCityWeatherByZIPResult getGetCityWeatherByZIPResult() {
return GetCityWeatherByZIPResult;
}