【发布时间】:2010-10-19 14:53:26
【问题描述】:
编辑:问题在于ResponseMessage 类中的[MessageHeader] 属性; Metro/JAX-WS 似乎无法处理这些属性。将它们更改为 [MessageBodyMember] 解决了这个问题。
正如标题所说,我需要获取一些 Java 1.5 代码来调用 WCF Web 服务。我已经下载并使用 Metro 生成 Java 代理类,但它们没有生成我所期望的,我相信这是因为 WCF 服务生成的 WSDL。
我的 WCF 类如下所示(为简洁起见,省略了完整代码):
public class TestService : IService
{
public TestResponse DoTest(TestRequest request)
{
TestResponse response = new TestResponse();
// actual testing code...
response.Result = ResponseResult.Success;
return response;
}
}
public class TestResponse : ResponseMessage
{
public bool TestSucceeded { get; set; }
}
public class ResponseMessage
{
[MessageHeader]
public ResponseResult Result { get; set; }
[MessageHeader]
public string ResponseDesc { get; set; }
[MessageHeader]
public Guid ErrorIdentifier { get; set; }
}
public enum ResponseResult
{
Success,
Error,
Empty,
}
生成的 WSDL(当我浏览到 http://localhost/TestService?wsdl=wsdl0 时)如下所示:
<xsd:element name="TestResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="TestSucceeded" type="xsd:boolean" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ErrorIdentifier" type="q1:guid" xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/" />
<xsd:simpleType name="ResponseResult">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Error" />
<xsd:enumeration value="Success" />
<xsd:enumeration value="EmptyResult" />
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="ResponseResult" nillable="true" type="tns:ResponseResult" />
<xsd:element name="Result" type="tns:ResponseResult" />
<xsd:element name="ResultDesc" nillable="true" type="xsd:string" />
...
<xs:element name="guid" nillable="true" type="tns:guid" />
<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}" />
</xs:restriction>
</xs:simpleType>
我立即发现此 WSDL 存在问题:TestResponse 不包含从 ResponseMessage 继承的属性。由于此服务一直在 Visual Studio 中有效,我以前从未对此提出过质疑,但也许这会导致我的问题?
无论如何,当我在服务上运行 Metro 的 wsimport.bat 时,会生成以下错误消息:
[WARNING] src-resolve.4.2: Error resolving component 'q1:guid'
并且TestResponse 的输出Java 版本缺少ResponseMessage 的任何属性。
我稍微修改了 WSDL 并将 ErrorIdentifier 更改为 xsd:string,这使得有关解析 GUID 类型的消息消失了,但我仍然没有得到任何 ResponseMessage 的属性。
最后,我更改了 WSDL 以将来自 ResponseMessage 的 3 个属性包含在 TestResponse 中,当然最终结果是生成的 .java 文件包含它们。但是,当我实际从 Java 调用 WCF 服务时,这 3 个属性始终是 null。
除了自己编写代理类之外有什么建议吗?
【问题讨论】:
-
你能帮我找到方法、下载和使用 Metro 吗?