I recently started to understand WCF and wrote the simplest Helloworld. I want 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 for 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 .)
Path = % JAVA_HOME % \ bin;
Classpath =.; % JAVA_HOME % \ lib \ tools. jar
2. Download and install Eclipse3.6Is 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. SlaveHttp://ws.apache.org/axis2/download/1_4_1/download.cgiDownload 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: "the latest version of 1.5.0 and 1.5.1 has no backport-util-concurrent-3.1.jar and cannot be solved: using the axis2-eclipse-codegen-wizard plug-in, errors cannot continue when generating java files from the wsdl file, 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 and 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 New Java project 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. The generated two class files, ServiceCallbackHandler. java and ServiceStub. add the java file and add references to all jar files under d: \ 2000 \ Java \ Axis2 \ lib in jreSE1.6.
Note that there is a special file, that isAxis2_Codegen_Wizard_1.4.1 \ lib \ axis2-1.4.1.jar, need to manually copy to d: \ 2000 \ java \ axis2 \ lib and add to the project's jre library, this is the second part 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: This is just a simple demo, but I have asked many people and asked questions in the forum. However, due to the different use environments, I did not find a similar environment, so I had to explore and practice it myself, 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. Eclipse \ tomcat \ axis 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:
<? 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>
</Endpoint>
<Endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange"/>
<Host>
<BaseAddresses>
<Add baseAddress = "http: // localhost: 8000/"/>
</BaseAddresses>
</Host>
</Service>
</Services>
<Behaviors>
<ServiceBehaviors>
<Behavior name = "MyServiceTypeBehaviors">
<ServiceMetadata httpGetEnabled = "True" httpGetUrl = ""/>
<ServiceDebug includeExceptionDetailInFaults = "False"/>
</Behavior>
</ServiceBehaviors>
</Behaviors>
</System. serviceModel>
</Configuration>
Related links:
Java and WCF interaction (I) supplement: Use WSImport to generate the Java client code of WSDL
Java and WCF interaction (2): the WCF client calls Java web service