【发布时间】:2014-03-26 12:19:38
【问题描述】:
我正在学习如何用 Java 实现 Web 服务。按照 O'Reilly 的“学习 Java”一书中的示例,我在 cdyne.com 下载了一个气象服务的 WSDL 文件(参见 http://bit.ly/13moiTh),并使用 wsimport 工具生成了一组类。
第一个问题是,当我生成一个 JAR 文件并在 Eclipse 项目中引用它时,无法识别这些类。我必须使用 wsimport -keep 并将所有源文件单独复制到项目中才能构建。
接下来,为了确保服务可用,我根据书中的代码构建了一个简单的 HTTP POST 客户端。我可以使用该代码成功访问 getCityWeatherByZIP 服务,因此不存在连接或身份验证问题。
最后,我尝试使用自动生成的 Web 服务客户端代码访问 getCityWeatherByZIP 和 getCityForecastByZIP 服务。两者都静默失败,即 isSuccess() 方法返回 false 并且所有响应字段均为空:
Weather service = new Weather();
WeatherSoap weatherSoap = service.getWeatherSoap();
WeatherReturn weather = weatherSoap.getCityWeatherByZIP(ZIP);
if (weather.isSuccess()) {
System.out.format("%s, %s : %s : Temperature: %s, Wind: %s\n",
weather.getCity(), weather.getState(), weather.getDescription(),
weather.getTemperature(), weather.getWind());
}
else {
System.out.println("Failed to obtain weather");
}
在逐步执行代码时,在 getCityWeatherByZIP() 中,我发现了以下 NoSuchMethodException: com.sun.xml.internal.ws.api.message.Packet.setHeaderList(com.sun.xml.internal.ws.api.message.HeaderList)"
这个异常发生在一行中(在 Eclipse 的 Debug 透视图中): “SEIStub.invoke(Object, Method, Object[]) 行:不可用”
很明显,Packet.setHeaderList(HeaderList) 方法是不存在的,非内部类的文档也证实了这一点: https://jax-ws.java.net/nonav/jax-ws-20-fcs/arch/com/sun/xml/ws/api/message/Packet.html
我使用的是最新的 JDK 1.7 和 Eclipse 版本:Kepler Service Release 1,Build id:20130919-0819。
我不知道如何解决这个问题。任何帮助将不胜感激。
【问题讨论】:
-
我解决了这个问题:Eclipse 项目中缺少 package-info.java 文件。一旦我按照这些说明将它添加到包中,一切都开始工作:stackoverflow.com/questions/8405336/…
标签: java web-services weather wsimport