[英]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.