Java and WCF interaction (1): Java client calls the WCF Service

Source: Internet
Author: User

Step 1: generate a WCF Service

Create a new WCF solution, add three projects respectively, HelloTimeService (Class Library), HelloTimehost (console Program), HelloTimeClient (console Program), and project structure

Main Code of each project:
Service:

Host:

Client:

After the compilation is successful, the test Host and Client are successful:

Keep the Host Program open so that the WCF Service can always be called by external programs. Of course, it also includes the Java client.

Let's take a look at the generated WSDL

Step 2: Call the WSDL generated by WCF to generate a java client.

This is a java web service solution. The most common features are WSDL2Java and Java2WSDL.

Another solution for Web service is xFire, which I have never tried.

I have not successfully executed WSDL2Java on the command line! Here I will introduce Code Generator Wizard-eclipse Plug-in, an Eclipse Plug-in. The configuration is successful only when many solutions are referenced!

Create a Java projec T, named GetWCFInfoByJavaClient, and add a New Wizard, File-New

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

Create a package named org. tempuri in the newly created GetWCFInfoByJavaClient project, which is consistent with the custom namespace. Add the generated two class files, ServiceCallbackHandler. java and ServiceStub. java, and add references to all jar files under d: 2000JavaAxis2lib in jreSE1.6.

Note that at this time there is a special file, that is, the Axis2_Codegen_Wizard_1.4.1libaxis2-1.4.1.jar, You need to manually copy to d: 2000javaaxis2lib and add to the project's jre library, this is the second place of the Month of invitation, pondering for a long time, be cautious.

The project structure should be as follows:

The eclipse namespace prompt is better than! Ctrl + 1. Haha. How nice it would be if Microsoft could do this!

In this case, the database is added and the check code is correct. The compilation error is prompted:

Access restriction: The type AxisFault is not accessible due to restriction on required library D: 2000JavaAxis2libaxis2-kernel-1.4.1.jar

Now I am dumb, so I have to hurry to google. In the past two days, google.com has always gone on strike for no reason. This is really a single disaster. I finally found a foreigner's article, saying it is as follows, and I can solve it:
* Go to the Build Path settings in the project properties. Windows-> Preferences-> Java Compiler
* Remove the JRE System Library
* Add another JRE with a "perfect match"
* Clean and build your project again. It worked for me.

After trying, my default jre environment is JDK5, so I switched to jre system library.

Finally compiled successfully! Seeing sugon,

At this point, the final task is to call stub in Main to get the desired service result. In a simple method, slightly modify the method of the generated test class to run it. As follows:

Summary: although this is just a simple demo, I have asked many people and asked questions in the forum. However, due to the different use environments, I had to find a similar environment, so I had to find my own practices, fortunately, there is still a little bit of java Foundation. In addition, the answers provided by several online forums such as javaeye are too old. This is the annoyance of java beginners. In this regard, Microsoft's development environment is commendable in any case, at least it does not have to go to other sites to ask for answers. Eclipseomcataxis and other representatives of the server and development environment, plug-in configuration is very helpless for beginners! Because you do not know whether a jar is compatible with thousands of other jar files. I don't know if java experts have a clear explanation. Thank you very much! Thanks again to yycxb

The next article introduces how the WCF client calls java's web service, which is much simpler. Stay tuned.


Appendix: WCF configuration file:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. web>
<Compilation debug = "true"/>
</System. web>
<System. serviceModel>
<Services>
<Service name = "Sample. WCF. HelloTimeForJava. Service" behaviorConfiguration = "MyServiceTypeBehaviors">
<Endpoint address = "HelloTimeService" binding = "wsHttpBinding" contract = "Sample. WCF. HelloTimeForJava. IService">
<Identity>
<Dns value = "localhost"/>
</Identity>

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.