Java interaction with WCF (i): Java Client calls WCF service

Source: Internet
Author: User
Tags wsdl java se

Recently began to understand WCF, wrote the simplest HelloWorld, want to implement communication through the Java client. Did not expect to my foundation, incredibly spent a full two days (of course, outside the work of time, hehe), the whole process of great expense, close-up this article, for the needs of friends reference:

First step: Build a WCF Service

Create a new WCF solution, add three items, Hellotimeservice (class library), Hellotimehost (console program), Hellotimeclient (console program), Project structure

The main code for each project:
Service

Host:

Client:

After the compilation is passed, the test host and client succeed individually:

Keep the host program open so that WCF services can always be called by external programs. Java clients are also included, of course.

Let's look at the generated WSDL

Step Two: Call the WCF-generated WSDL to generate the Java client.

First of all, please allow me to introduce the next AXIS2, its official address: http://ws.apache.org/axis2/

This is a Web service solution for the Java platform. Our most commonly used is the Wsdl2java and java2wsdl two functions.

Another solution for Web service is Xfire, I haven't tried.

About the command line execution Wsdl2java, I also did not execute success! What I'm introducing here is code Generator wizard-eclipse plug-in, a plugin for eclipse. And it is in the reference to a number of solutions before the configuration success!

Special thanks to YYCXBJL for this post: http://www.cnblogs.com/yycxbjl/archive/2010/06/14/1758063.html

My Java entire configuration environment:

1. Install the JDK and configure the environment variables

Java_home=d:\2000\java\jdkupdate (Note that no version number is required to replicate files at a later time without modifying environment variables.) )
Path=%java_home%\bin;
Classpath=.; %java_home%\lib;%java_home%\lib\tools.jar

2. Download and install Eclipse3.6, is familiar with the sun god Helios(decompression can be)
Extract to: D:\2000\Java\Eclipse

3. Download Tomcat6.0.29 (The green version is used in this article, so the decompression is done)
Extract to: D:\2000\Java\tomcat (note that no version number is required to copy files at a later time without modifying environment variables.) )
Since the installation of tomcat6.0 does not have to configure Tomcat environment variables, the previous version can be checked for information.
Environment variables such as Tomcat_home,catalina need to be set

4. download Axis2-1.4.1-bin.zip from http://ws.apache.org/axis2/download/1_4_1/download.cgi
Extract to: D:\2000\Java\Axis2, note do not download Axis2 1.5.1, the reason ( according to YYCXBJL said: "The latest version of 1.5.0 and 1.5.1 did not Backport-util-concurrent-3.1.jar, unable to resolve: using the Axis2-eclipse-codegen-wizard plug-in, generated from the WSDL file Java file error can not continue the problem, it is not used. )

5. Download 1.4.1 version of Axis2-eclipse-codegen-wizard and Axis2-eclipse-service-archiver-wizard

Http://ws.apache.org/axis2/tools/1_0/eclipse/wsdl2java-plugin.html#operation1

(But after the decompression inside the display is 1.3.0, extract to: D:\2000\java\Eclipse\dropins (even the root directory in the compression package extracted together). Can also be deployed using the traditional link method.

Copy D:\2000\Java\Axis2\lib\backport-util-concurrent-3.1.jar and Geronimo-stax-api_1.0_spec-1.0.1.jar
To: D:\2000\Java\Eclipse\dropins\Axis2_Codegen_Wizard_1.3.0\lib, and edit: D:\2000\Java\Eclipse\dropins\Axis2_Codegen_ Wizard_1.3.0\plugin.xml
Add at runtime

      <Libraryname= "Lib/backport-util-concurrent-3.1.jar">         <Exportname="*"/>      </Library>      <Libraryname= "lib/geronimo-stax-api_1.0_spec-1.0.1">         <Exportname="*"/>      </Library>

Rename axis2_codegen_wizard_1.3.0 to axis2_codegen_wizard_1.4.1, which is the key to the entire deployment! It's been a whole day of inviting months! )

Create a new Java project named Getwcfinfobyjavaclient and add a new wizard to the new project, File-new

At this point, we can see the long-awaited wizard interface:

In this step, build a package in the new Getwcfinfobyjavaclient project, named Org.tempuri, consistent with the previously customized namespace. Add the generated two class files, Servicecallbackhandler.java and Servicestub.java files, and add references to all the jar files under D:\2000\Java\Axis2\lib in the jreSE1.6 library.

Note that there is a special file, Axis2_codegen_wizard_1.4.1\lib\axis2-1.4.1.jar, that needs to be manually copied to D:\2000\java\axis2\lib and added to the project's JRE library. This is the second to invite the month to jam the place, pondering a long time, cautious of caution.

The structure of the project should be as follows:

Eclipse's namespace hint is stronger than vs! Very fond of ctrl+1. Oh. It would be nice if Microsoft could do that.

The hell is, this time the library is added to complete, check the code is correct, actually compiled error, hint:

Access restriction:the type Axisfault isn't accessible due to restriction on required library D:\2000\Java\Axis2\lib\axi S2-kernel-1.4.1.jar

This under my dumbfounded, so hurriedly Google, these two days google.com always no reason to strike, really pours, finally found a foreigner article, said is as follows, can solve:
* Go to the Build Path settings in the project properties. Java Compiler, Windows-Preferences
* Remove the JRE System Library
* Add another JRE with a "perfect match"
* Clean and build your project again. It worked for me.

I tried, my default JRE environment was JDK5, and I switched to the JRE system library

Finally compiled successfully! See the Dawn,

At this point, the final job is to call the stub in main to get the service result we want, the simple method, the test class just generated the method slightly modified, you can run. As follows:

This is the result we want.

For additional articles on Java and. Net interaction, refer to:

Microsoft Example:

Java/.net interoperability with the microsoft.com Web Service
Http://msdn.microsoft.com/en-us/library/ms953968.aspx

Interoperability Testing

Http://msdn.microsoft.com/en-us/library/aa480510.aspx

IBM Example: Creating a stand-alone Web Services application using Eclipse and Java SE 6, part 1th: Web Services server-side application (the link needs to be registered first)

Https://www6.software.ibm.com/developerworks/cn/education/webservices/ws-eclipse-javase1/section2.html

Summary : Although this is just a simple demo, but I asked a lot of people, and in the forum questions, but because of the different use of the environment, eventually did not find a similar environment, had to explore their own practice, fortunately there is a little bit of Java Foundation, Other online forums like Javaeye provide answers too old, this is the Java beginner's distress, this point of view, Microsoft's development environment is commendable anyway, at least it does not have to run to other sites to ask the answer. Eclipse\tomcat\axis and other representative of the server and development environment, plug-in configuration is very frustrating for beginners! Because you do not know if a jar is well compatible with the other thousands of jars. Do not know whether Java experts are referring to a clear way, thank you very much! And again thank YYCXB

The next article describes how WCF clients invoke Java Web Service, which is much simpler. Please pay attention.

Attached: WCF configuration file:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <system.web>    <compilationDebug= "true"/>  </system.web>  <System.ServiceModel>    <Services>      <Servicename= "Sample.WCF.HelloTimeForJava.Service"behaviorconfiguration= "Myservicetypebehaviors">        <EndpointAddress= "Hellotimeservice"binding= "Wshttpbinding"Contract= "Sample.WCF.HelloTimeForJava.IService">          <Identity>            <DNSvalue= "localhost"/>          </Identity>        </Endpoint>        <EndpointAddress= "Mex"binding= "mexHttpBinding"Contract= "IMetadataExchange"/>        <Host>          <baseaddresses>            <Addbaseaddress= "Http://localhost:8000/"/>          </baseaddresses>        </Host>      </Service>    </Services>    <Behaviors>      <servicebehaviors>        <Behaviorname= "Myservicetypebehaviors">          <Servicemetadatahttpgetenabled= "True"Httpgeturl=""/>          <ServicedebugIncludeexceptiondetailinfaults= "False"/>        </Behavior>      </servicebehaviors>    </Behaviors>  </System.ServiceModel></Configuration>

RELATED links:

Java interaction with WCF (i) Supplement: Java Client code that generates WSDL with Wsimport
Http://www.cnblogs.com/downmoon/archive/2010/08/29/1811689.htmlJava interacting with WCF (ii): WCF client calls Java Web Service
Http://www.cnblogs.com/downmoon/archive/2010/08/25/1807982.html

This article transferred from: http://www.cnblogs.com/downmoon/archive/2010/08/24/1807161.html

Java interaction with WCF (i): Java Client calls WCF service

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.