Java 客户端和WCF服务端访问原理和部署步骤(英文) 分享(转)
2011-11-19 13:12
363 查看
http://chaizhixue2005.blog.sohu.com/139840107.htmlWCF’s fundamental communication mechanism is SOAP-based Web services. Because WCF implements Web services technologies defined by the WS-* specifications, other software which is based on SOAP and supports WS-* specifications can communicate with the WCF-based applications.
To build up a cross-platform WCF server, you can use Metro. Metro is a Web Services framework that provides tools and infrastructure to develop Web Services solutions for the end users and middleware developers. It depends on Java programming language. The latest version of Metro is 1.4.
In the development process of SCM Anywhere (a SCM tool, with fully integrated Version Control, Issue Tracking and Build Automation, developed with WCF and METRO/WSIT), we found that METRO is NOT as mature as WCF. There are lots of small issues in METRO/WSIT. Luckily, METRO is an open source project and keeps evolving all the time. Our experience is that if you find some features are not working properly in METRO, keep downloading the latest version from Java.net. Several weeks later, you may discover that the features are working properly.
To implement a Java client to communicate with the WCF server, you can follow the steps below:
1. Download METRO/WSIT from the home page of Metro: https://metro.dev.java.net.
2. Download Eclipse. We use Eclipse + Metro to develop Dynamsoft SCM Anywhere.
3. Install Metro by executing the command:
4. Use the C# project “WcfService1″ (provided in my WCF client and WCF service article) as the WCF server. Go to Property of the WCF project, and set the server port to one that is not occupied by other services. Here we used 8888 for example. In the “web.config” file, change the string “wsHttpBinding” to “basicHttpBinding”.
5. This is the key step. We use the wsimport tool included in Metro to generate the Java client code. Create a file named “service1.xml” and copy the following code to the file:
The parameter “enableAsyncMapping” means generating the asynchronous method communicating to WCF server. Save this file, and execute the following command:
To build up a cross-platform WCF server, you can use Metro. Metro is a Web Services framework that provides tools and infrastructure to develop Web Services solutions for the end users and middleware developers. It depends on Java programming language. The latest version of Metro is 1.4.
In the development process of SCM Anywhere (a SCM tool, with fully integrated Version Control, Issue Tracking and Build Automation, developed with WCF and METRO/WSIT), we found that METRO is NOT as mature as WCF. There are lots of small issues in METRO/WSIT. Luckily, METRO is an open source project and keeps evolving all the time. Our experience is that if you find some features are not working properly in METRO, keep downloading the latest version from Java.net. Several weeks later, you may discover that the features are working properly.
To implement a Java client to communicate with the WCF server, you can follow the steps below:
1. Download METRO/WSIT from the home page of Metro: https://metro.dev.java.net.
2. Download Eclipse. We use Eclipse + Metro to develop Dynamsoft SCM Anywhere.
3. Install Metro by executing the command:
java –jar metro-1_4.jar. The installation folder of Metro contains some documents, tools and samples. You can find the documents in the “docs” folder.
4. Use the C# project “WcfService1″ (provided in my WCF client and WCF service article) as the WCF server. Go to Property of the WCF project, and set the server port to one that is not occupied by other services. Here we used 8888 for example. In the “web.config” file, change the string “wsHttpBinding” to “basicHttpBinding”.
5. This is the key step. We use the wsimport tool included in Metro to generate the Java client code. Create a file named “service1.xml” and copy the following code to the file:
<bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocaption="http://localhost:8888/Service1.svc?wsdl" xmlns="http://java.sun.com/xml/ns/jaxws"> <bindings node="wsdl:definitions"> <enableAsyncMapping>true</enableAsyncMapping> </bindings> </bindings>
The parameter “enableAsyncMapping” means generating the asynchronous method communicating to WCF server. Save this file, and execute the following command:
bin\wsimport -extension -keep -Xnocompile -b TestService.xml http://localhost:8888/Service1.svc?wsdl[/code]
Then you can find two new directories in Metro folder: “org” and “com”. They are the generated Java code.
6. Open Eclipse IDE, create a new Java project named “SimpleWCFClient”, and copy the two new directories “org” and “com” to the “src” folder of the project. Refresh the project, and you can find that some new code files are in the project.
7. Create a test class named “WCFTest” and write the following code to the file:import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.BindingProvider; import org.tempuri.IService1; import org.tempuri.Service1;
public class WCFTest {
public static void main(String[] strArgs)
{
try
{
Service1 service1 = new Service1(new URL("http://localhost:8888/Service1.svc?wsdl"), new QName("http://tempuri.org/", "Service1"));
IService1 port = service1.getBasicHttpBindingIService1();
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8888/Service1.svc");
int input = 100;
String strOutput = port.getData(input);
System.out.println(strOutput);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
8. Four .jar files need to be added to the Java project. You can get these files in the “lib” folder of Metro: webservices-api.jar, webservices-extra.jar, webservices-extra-api.jar, webservices-rt.jar. Then go to Property of the project and add these jars to the project.
9. Compile and run the Java project. If the Eclipse console outputs “You entered: 100″, congratulations, you are successful.
You can download these code from here. When you are familiar with these, you will find it’s very convenient to write a Java application communicating with a WCF client.
Links:
Previous article >>>>: WCF client and WCF service
Next article >>>>: Data types between Java and WCF
WCF & Java Interop series home page: WCF & Java Interop
5 responses so far5 Responses to “Java client and WCF server”
# Anaristoson 04 May 2009 at 1:44 am I have implemented both the VWD Server/Client and the WCF Server/Java Client schemes. The first one works just fine, as I stated before. However, with the second I have run into a big problem. My Java client runs into a “Permission denied: connect.” wall when attempting to access the WSDL. I thought that, since I am running under Vista (6SP1) that perhaps it was UAC that was causing the problem, but that is not it. Nor was it a binding mismatch. I think that because the Java side is coming in through a remote socket that Windows authentication is blocking the access. Perhaps you have some suggestions as to how to get around this?
# Anaristoson 04 May 2009 at 4:56 pm Here is something interesting. I imported the Eclipse project into Netbeans 6.5.1 and it ran perfectly. So the problem is with Eclipse.
# Julisaon 04 Jun 2009 at 9:41 pm It is possible to use javax.xml.ws.Dispatch to communicating with WCF Service?
# NItinon 08 Sep 2009 at 7:55 am How would you create a java client when you have a WCF service running on net tcp binding.
# Ronakon 13 Nov 2009 at 7:24 am Thanks for this really good Tutorial. I am having some issue right now. I am able to generate the proxy classes following your step.
Problem 1: All the strings in Method are JAXBElement
Solution i have found so far is using this bind.xml file instead of yours
Is there any way i can combine both of yours and this one?
Problem 2: I am getting NullPointerException, when execution client code..It occurs in the first line in Main function.
Exception:
java.lang.NullPointerException
at com.sun.xml.ws.model.wsdl.WSDLOperationImpl.freez(WSDLOperationImpl.java:147)
at com.sun.xml.ws.model.wsdl.WSDLPortTypeImpl.freeze(WSDLPortTypeImpl.java:91)
at com.sun.xml.ws.model.wsdl.WSDLBoundPortTypeImpl.freeze(WSDLBoundPortTypeImpl.java:215)
at com.sun.xml.ws.model.wsdl.WSDLModelImpl.freeze(WSDLModelImpl.java:241)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:147)
at com.sun.xml.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:265)
at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:228)
at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:176)
at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:104)
at javax.xml.ws.Service.(Unknown Source)
at org.tempuri.BPAExecutionServerWCFServer.(BPAExecutionServerWCFServer.java:42)
at WCF.WCFTest.main(WCFTest.java:20)
Code:
package WCF;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import org.datacontract.schemas._2004._07.automate.ClientType;
import org.datacontract.schemas._2004._07.automate_messages.OpenSessionRequestMessage;
import org.datacontract.schemas._2004._07.automate_messages.OpenSessionResponseMessage;
import org.tempuri.IBPAExecutionServerHTTPServiceContract;
import org.tempuri.BPAExecutionServerWCFServer;;
public class WCFTest {
public static void main(String[] strArgs)
{
try
{
BPAExecutionServerWCFServer service1 = new BPAExecutionServerWCFServer(new URL(”http://209.223.157.111:7102/BPAExecutionServer/?wsdl”), new QName(”http://tempuri.org/”, “BPAExecutionServerWCFServer”));
IBPAExecutionServerHTTPServiceContract port = service1.getBasicHttpBindingIBPAExecutionServerHTTPServiceContract();
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, “http://209.223.157.111:7102/BPAExecutionServer/?wsdl”);
OpenSessionRequestMessage request = new OpenSessionRequestMessage();
request.setAttemptRegistration(true);
request.setClientType(ClientType.PROCESS_AGENT);
OpenSessionResponseMessage strOutput = port.openSessionRequest(request);
System.out.println(strOutput);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
相关文章推荐
- windows基于Thrift的php客户端访问java的服务端
- 三种客户端访问wcf服务端的方法 C#
- 三种客户端访问wcf服务端的方法 C#
- Java获取访问服务端的客户端IP和MAC地址
- 采用axis2方式Java客户端调用WCF服务端Web Service
- 重温HBaes部署与java客户端访问
- 使用SpringMVC开发RestFul风格接口的java-web项目,tomcat部署并客户端访问
- java网络编程客户端与服务端原理以及用URL解析HTTP协议
- 黑马程序员_Java基础_网络编程_客户端服务端数据传输,交互,客户端请求服务原理,自定义浏览器,URL统一资源定位符
- php SOAP客户端访问 JAVA soap服务端示例
- 在JAVA中部署WEBSERVICE方法与访问步骤
- Java客户端访问服务端
- 分享:Java socket ,多客户端同时与服务端不停的交换数据
- java搭建本地服务器,android客户端访问,tomcat配置详细步骤
- 三种客户端访问wcf服务端的方法 C#
- Java网络编程(客户端和服务端原理)
- WEB-INF是Java的WEB应用的安全目录。所谓安全就是客户端无法访问,只有服务端可以访问的目录。
- tensorflow serving 服务部署与访问(Python + Java)
- java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)
- 客户端(android/ios)与java服务端tomcat的gzip实现