从 Java 调用 WCF 服务 [英] Calling a WCF service from Java

查看:47
本文介绍了从 Java 调用 WCF 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于 ResponseMessage 类中的 [MessageHeader] 属性;Metro/JAX-WS 似乎无法处理这些属性.将它们更改为 [MessageBodyMember] 解决了问题.

the issue was with the [MessageHeader] attributes in the ResponseMessage class; Metro/JAX-WS doesn't seem capable of handling these attributes. Changing them to [MessageBodyMember] solved the issue.

正如标题所说,我需要一些 Java 1.5 代码来调用 WCF Web 服务.我已经下载并使用了 Metro 来生成 Java 代理类,但它们没有生成我所期望的,我相信这是因为 WCF 服务生成的 WSDL.

As the title says, I need to get some Java 1.5 code to call a WCF web service. I've downloaded and used Metro to generate Java proxy classes, but they aren't generating what I expect, and I believe this is because of the WSDL that the WCF service generates.

我的 WCF 类如下所示(为简洁起见省略了完整代码):

My WCF classes look like this (full code omitted for brevity):

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) 看起来像这样:

and the resulting WSDL (when I browse to http://localhost/TestService?wsdl=wsdl0) looks like this:

<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 中运行,我以前从未质疑过这一点,但也许这可能会导致我的问题?

Immediately I see an issue with this WSDL: TestResponse does not contain the properties inherited from ResponseMessage. Since this service has always worked in Visual Studio I've never questioned this before, but maybe that could be causing my problem?

无论如何,当我在服务上运行 Metro 的 wsimport.bat 时,会生成以下错误消息:

Anyhow, when I run Metro's wsimport.bat on the service the following error message is generated:

[警告] src-resolve.4.2:解析组件q1:guid"时出错

并且 TestResponse 的输出 Java 版本缺少 ResponseMessage 中的任何属性.

and the outputted Java version of TestResponse lacks any of the properties from ResponseMessage.

我稍微修改了 WSDL 并将 ErrorIdentifier 更改为 xsd:string,这使得有关解析 GUID 类型的消息消失了,但我仍然不不获取任何 ResponseMessage 的属性.

I hacked the WSDL a bit and changed ErrorIdentifier to be typed as xsd:string, which makes the message about resolving the GUID type go away, but I still don't get any of ResponseMessage's properties.

最后,我修改了 WSDL 以在 TestResponse 中包含来自 ResponseMessage 的 3 个属性,当然最终结果是生成的 .java 文件包含它们.但是,当我实际从 Java 调用 WCF 服务时,这 3 个属性总是 null.

Finally, I altered the WSDL to include the 3 properties from ResponseMessage in TestResponse, and of course the end result is that the generated .java file contains them. However, when I actually call the WCF service from Java, those 3 properties are always null.

除了自己编写代理类之外,还有什么建议吗?

Any advice, apart from writing the proxy classes myself?

推荐答案

有些 #%$^@!在没有告诉任何人的情况下,将 ResponseMessage 属性上的 [MessageBodyMember] 属性更改为 [MessageHeader].我将它们改回 [MessageBodyMember],重新生成代理类,一切正常.

Some #%$^@! changed the [MessageBodyMember] attributes on the properties of ResponseMessage to [MessageHeader] without telling anyone. I changed them back to [MessageBodyMember], regenerated the proxy classes and everything works correctly.

完成集成后,我将进行 CVS diffs 以找到负责人,然后他们将忍受我的愤怒.

Onced I've finished the integration I'll be doing CVS diffs to find the person responsible, and then they shall SUFFER MY WRATH.

这篇关于从 Java 调用 WCF 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆