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

Source: Internet
Author: User

Http://blog.csdn.net/downmoon/article/details/5834773

I recently started to learn about WCF. I wrote the simplest helloworld and wanted to implement communication through the Java client. I did not expect that it took me two days (not work, of course) to complete the entire process. I wrote this article for my reference:

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.

First, let me introduce axis2, its official address: http://ws.apache.org/axis2/

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!

Special thanks to yycxbjl
This article: http://www.cnblogs.com/yycxbjl/archive/2010/06/14/1758063.html

My entire Java configuration environment:

1. Install JDK and configure Environment Variables

 
Java_home = D:/2000/Java/jdkupdate (note that the version number is not included so that only the file is copied during later upgrade without modifying the environment variable .)
Path = % java_home %/bin;
Classpath =.; % java_home %/LIB; % java_home %/lib/tools. Jar

2. Download and install eclipse3.6
Is the familiar sun godHelios
(Decompress the package)
Decompress the package to: D:/2000/Java/eclipse.

3. Download tomcat6.0.29
(The green version is used in this article, so decompression is complete)
Decompress the package to: D:/2000/Java/tomcat (note that the version number is not included so that only the file is copied during later upgrade without modifying the environment variable .)
Because Tomcat is installed without the need to configure Tomcat environment variables, you can check the information of previous versions.
You need to set environment variables such as tomcat_home and Catalina.

4. Slave
Http://ws.apache.org/axis2/download/1_4_1/download.cgi
Download axis2-1.4.1-bin.zip

Decompress the package to: D:/2000/Java/axis2. Do not download axis2 1.5.1 for any reason.
(
According to yycxbjl
Said:
"No backport-util-concurrent-3.1.jar in the latest version 1.5.0 and 1.5.1,
Work und: An error occurred while generating a Java file from the WSDL file using the axis2-eclipse-codegen-wizard plug-in, so it is not used .)

5. Download axis2-eclipse-codegen-wizard and axis2-eclipse-service-archiver-wizard for version 1.4.1

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

(However, after decompression, It is shown as 1.3.0. decompress the package to: D:/2000/Java/Eclipse/dropins (decompress the package together with the root directory ). You can also use the traditional link method for deployment.

Copy D:/2000/Java/axis2/lib/backport-util-concurrent-3.1.jar with geronimo-stax-api_1.0_spec-1.0.1.jar
To: D:/2000/Java/Eclipse/dropins/axis2_codegen_wizard_1.3.0/lib,
Edit: D:/2000/Java/Eclipse/dropins/axis2_codegen_wizard_1.3.0/plugin. xml.
Add under Runtime
<Library name = "Library/backport-util-concurrent-3.1.jar">
<Export name = "*"/>
</Library>
<Library name = "Library/geronimo-stax-api_1.0_spec-1.0.1">
<Export name = "*"/>
</Library>

Rename axis2_codegen_wizard_1.3.0 to axis2_codegen_wizard_1.4.1. (This is the key to the entire deployment! I have spent a whole day here !)

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:

In
Getwcfinfobyjavaclient
In the project, create a package named org. tempuri, which is consistent with the custom namespace. The generated two class files
Add the servicecallbackhandler. Java and servicestub. java files and add them to the jrese1.6 library.
D: Reference all jar files under/2000/Java/axis2/lib.

Note that there is a special file, that isAxis2_codegen_wizard_1.4.1/lib/axis2-1.4.1.jar, need to be manually copied to D:/2000/Java/axis2/lib and added to the project's JRE library, this is the second card shell of the invitation month, after 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:/2000/Java/axis2/lib/axis2-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:

This is what we want.

For other articles on interaction between Java and. net, see:

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: Use eclipse and Java SE 6 to create an independent Web Services application, Part 1:
Web services server application (this link must 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, I have asked many people and asked questions in the forum. However, due to the different environments, I had to find a similar environment and I had to explore the practices myself, fortunately, there is a little more
Java basics. 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.
Without it, you don't have to go to other sites to ask for answers. The server, development environment, and plug-in configurations represented by ECLIPSE, tomcat, and axis are helpless for beginners! Because you don't know a jar
Yes
No. It 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 plainprint?
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Configuration>
  3. <System. Web>
  4. <Compilation DEBUG = "true"/>
  5. </System. Web>
  6. <System. servicemodel>
  7. <Services>
  8. <Service name = "sample. WCF. hellotimeforjava. Service" behaviorconfiguration = "myservicetypebehaviors">
  9. <Endpoint address = "hellotimeservice" binding = "wshttpbinding" Contract = "sample. WCF. hellotimeforjava. iservice">
  10. <Identity>
  11. <DNS value = "localhost"/>
  12. </Identity>
  13. </Endpoint>
  14. <Endpoint address = "mex" binding = "mexhttpbinding" Contract = "imetadataexchange"/>
  15. <Host>
  16. <Baseaddresses>
  17. <Add baseaddress = "http: // localhost: 8000/"/>
  18. </Baseaddresses>
  19. </Host>
  20. </Service>
  21. </Services>
  22. <Behaviors>
  23. <Servicebehaviors>
  24. <Behavior name = "myservicetypebehaviors">
  25. <Servicemetadata httpgetenabled = "true" httpgeturl = ""/>
  26. <Servicedebug includeexceptiondetailinfaults = "false"/>
  27. </Behavior>
  28. </Servicebehaviors>
  29. </Behaviors>
  30. </System. servicemodel>
  31. </Configuration>

Related Links: Java and WCF interaction (2): the WCF client calls Java Web servicejava to interact with WCF (1) supplement: Use wsimport to generate the Java client code of the WSDL Invitation month Note: The copyright of this article is jointly owned by the invitation month and csdn. For more information, see the source.


Helping others is the same as self-help!
3w@live.cn

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.