[英]Error connecting Java App to WCF web service
我已经在Eclipse中编写了一个简单的Java控制台应用程序,该应用程序引用了用C#编写的WCF Web服务。 在我的PC上本地托管WCF服务,我无法使用Java客户端连接到该服务。
我创建WCF服务所采取的步骤如下
创建具有以下端点的“ Service1.svc”:
字符串IService1.Hello(字符串sName){返回“ Hello” + sName +“!” ; }
该服务的Web配置如下:
<system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding" /> </basicHttpBinding> </bindings> <client> <endpoint binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="WcfService1.IService1" name="BasicHttpEndpoint" /> </client> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/>
我修改了属性,因此该服务将始终使用端口8888。
我已经使用C#控制台应用程序测试了该服务。
要创建Java客户端,我做了以下工作:
下载并安装与Metro(Web服务器)捆绑在一起的Glassfish 3.0.1(应用程序服务器)
使用我的jdk的“ bin”目录中的“ wsimport”工具为Java客户端应用生成服务参考代码。 我运行以创建服务参考的.bat文件
将上述步骤2中的代码复制到Eclipse中的新Java应用程序中。
在控制台应用程序中创建一个新的Java类,该类将按如下所示调用Web服务
`
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import org.tempuri.Service1;
import org.tempuri.IService1;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
try {
Service1 service = new Service1(new URL("http://localhost:8888/Service1.svc?wsdl"), new QName("http://tempuri.org", "Service1"));
IService1 port = service.getBasicHttpBindingIService1();
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8888/Service1.svc");
String sFileName = port.hello("Cal");
System.out.println(sFileName);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.printStackTrace();
}
}
}
`
尝试运行我的应用程序时出现的错误如下:
Exception in thread "main" javax.xml.ws.WebServiceException: {http://tempuri.org}Service1 is not a valid service. Valid services are: {http://tempuri.org/}Service1
at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:237)
at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:182)
at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:106)
at javax.xml.ws.Service.(Unknown Source)
at org.tempuri.Service1.(Service1.java:42)
at Main.main(Main.java:17)
感谢您的协助。 谢谢。
更改:
new QName("http://tempuri.org", "Service1")
至:
new QName("http://tempuri.org/", "Service1")
注意org之后的多余“ /”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.