Calling. NET WCF in Java is actually quite simple.

Source: Internet
Author: User

A few days ago with one of our mail service providers docking, the two sides need to make some communication, the other side is the Java team, and as the. NET team we can only publish out is a WCF basicbinding, think not

To the problem, the other side do not know how to call this basic, maybe they have a bit of food, a little awkward, I have to study, in fact, as long as the knowledge of the published WSDL, what language is very simple.

One: Case

For convenience, I do not intentionally write what code, the VS inside the WCF service template created, the details are as follows:

1     //Note: Using the rename command on the Refactor menu, you can change the class name "Service1" in Code, SVC, and configuration files at the same time. 2     //Note: In order to start the WCF test client to test this service, select Service1.svc or Service1.svc.cs in Solution Explorer and start debugging. 3      Public classService1:iservice14     {5          Public stringGetData (intvalue)6         {7             return string. Format ("You entered: {0}", value);8         }9 Ten          Publiccompositetype getdatausingdatacontract (compositetype composite) One         { A             if(Composite = =NULL) -             { -                 Throw NewArgumentNullException ("Composite"); the             } -             if(composite. Boolvalue) -             { -Composite. StringValue + ="Suffix"; +             } -             returnComposite; +         } A}

Then F5 run, you can see such a WCF test client, there is a service address, there are two method contracts provided.

Two: Java calls

1. Using the Web service client

In fact, in Eclipse's new project there is a Web service Client template, which can transform the WSDL into a proxy Java code, a bit like the service reference in C #, is not,

Below is a step-by-step operation:

"1" Create a new Java project, or Dynamic Web project is possible, this test only creates a new Myjavatest project.

2 Then use the template File of the WebService Client and the other =>web Services = Web Service Client.

"3" Enter our WCF service address in the service definition option: http://localhost:54248/Service1.svc?wsdl, be sure to note

behind the Svc Add a WSDL, so that the Java proxy to find Kazakhstan, and then there is a "thermometer" in the lower left corner, transferred to the start client mode

OK, then continue to click on the a step, details such as:

"4" means that the generated code will be put into which directory, this is nothing to say.

"5" Then click Finish, and now you'll find that you have a few more automated Java classes, convenience and a spell for Visual Studio.

OK, the automation classes are out, next I just need to create a new portal program class, is it possible to finalize the ...

1 Package COM.DATAMIP;2 3 import java.rmi.RemoteException;4 5 import Org.tempuri.IService1Proxy;6 7  Public classProgram {8 9      Public Static voidMain (string[] args) throws RemoteException {Ten  OneIservice1proxy proxy =Newiservice1proxy (); A  -String result = Proxy.getdata (12345); -  theSystem. out. println (result); -     } -  -}

But to vomit groove is, Iservice1proxy incredibly is a class, I thought is interface, thinking fixed bar ~ ~ ~

2. Using the Wsimport gadget

In fact, Java JDK has a lot of practical gadgets, there is a wsimport.exe, it can be used to generate automated proxy classes based on WSDL, and. NET svcutil functionality

is the same.

Since the JDK path is configured in the environment variable, you can see the parameters required by Wsimport in CMD.

C:\users\hxc>wsimport missing Wsdl_uri usage: wsimport [options]<WSDL_URI>\ [Options] includes:-B <path> designation jaxws/JAXB binding files or additional modes (each<path> must have its own-b)-b<jaxboption>Pass this option to the JAXB mode compiler-catalog <file>Specifies that the catalog file used to resolve external entity references supports the TR9401, Xcatalog, and OASIS XML directory formats. -D <directory>specify where to place the generated output file-encoding <encoding>Specifies the character encoding used by the source file-extension Allow Vendor Extensions-features are not specified by specification. Using extensions may cause applications to be non-portable or to interoperate with other implementations-Help Display Assistance-httpproxy:8080)  -keep Keep the generated files-P <pkg>Specifying target Packages-quiet hidden wsimport output-S <directory>specify where to place the generated source files-target <version>generated code by the given JAXWS specification version defaults to2.2, the accepted value is2.0,2.1And2.2For example,2.0will be for JAXWS2.0specification generation of compatible code-verbose output message about what the compiler is doing-version output release information-wsdllocation <location>@WebServiceClient. Wsdllocation values-clientjar <jarfile>creates a jar file of the generated Artifact and the WSDL metadata required to invoke the Web service. -Generatejws Generating stub JWS implementation files-impldestdir <directory>specify where to generate JWS implementation files-implservicename <name>The local part of the service name generated by the JWS implementation-implportname <name>The local part \ Extension of the port name of the generated JWS implementation:-xadditionalheaders Map header is not bound to a request or response message that is not bound to a Java method parameter-xauthfile files used to transmit authorization information in the following format: http://Username:[email protected]/stock?wsdl-Xdebug output Debug Information-xno-addressing-databinding allow Endpointreferencetype to Java bindings-Xnocompile does not compile the generated Java files-xdisableauthenticator Disabled by jax-the validator that WS RI uses will ignore the-xauthfile option (if set)-xdisablesslhostnameverification Disable SSL host name validation when extracting WSDL \ Example: Wsimport stock.wsdl -B Stock.xml-b stock.xjb Wsimport-D generated http://example.org/stock?wsdlC:\Users\hxc>

This one, probably know the following 4 parameters to say.

"1" URL: You need to generate a data source for the proxy class, which is the WSDL file

"2"-D: The path that the generated proxy file needs to be stored

"3"-P: Generates the package name of the proxy class.

"4"-keep: Save the generated file name

Next we'll try Cmd:

C:\users\hxc>wsimport http://localhost:54248/Service1.svc?wsdl-d c:\\2\\-P com.datamip.proxy-keep parsing wsdl ... Generating code ... Compiling code ... C:\users\hxc>

OK, next you can copy the proxy class from folder 2 to project, such as:

Seems to be not perfect, then the next thing to do is coding verification ~ ~ ~

Finally there is a kind of stupid method, that is, through the Fiddler Crawl WebService communication parameters, and then through the HttpPost simulation submitted, it is also possible ... All right, this article is about

Here, hope is helpful to you.

Calling. NET WCF in Java is actually quite simple.

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.