Xfire WebService Quick Start

Source: Internet
Author: User

Xfire WebService Quick Start  Environment:XFire-1.2.6JDK1.5MyEclipse 6.5tomcat-5.5.27windows XP Professional Simplified Chinese version Software:[Url] refer does not talk about the concept, principle, data discovery, description, binding, and other processes and methods of WebService. Here we only focus on how to quickly develop a general and easy-to-understand Hello world example. The development procedure is as follows:  1. Create a project Open myeclipse 6.5 and create a new WebService project. For example, click Next until the process is complete. After the creation, open the generated web. xml file. You can see that xfire has been configured. <? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.5" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee [url] http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd#/url]
<Servlet>
<Servlet-Name> xfireservlet </servlet-Name>
<Servlet-class> org. codehaus. xfire. Transport. http. xfirepolicableservlet </servlet-class>
<Load-on-startup> 0 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> xfireservlet </servlet-Name>
<URL-pattern>/services/* </url-pattern>
</Servlet-mapping>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app> 2. Create a WebService Create two packages "wstest. Server" and "wstest. Client" to save the server and client programs. Then, create a server program. For example, a service configuration service. xml: <? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://xfire.codehaus.org/config/1.0">

<Service>
<Name> myservice </Name>
<Serviceclass> wstest. server. imyservice </serviceclass>
<Implementationclass>
Wstest. server. myserviceimpl
</Implementationclass>
<Style> wrapped </style>
<Use> literal </use>
<Scope> application </scope>
</Service>
</Beans> the interface and default implementation are also generated, and the rewrite is as follows: Package wstest. server;
// Generated by myeclipse

Public interface imyservice {

Public String sayhello (string user );

} Package wstest. server;
// Generated by myeclipse

Public class myserviceimpl implements imyservice {

Public String sayhello (string user ){
Return "hello," + user;
}

} Now, the server code has been completed.3. Test the server codeTo test the dependencies and Servlet Container tomcat, You need to package and deploy the server to Tomcat and start it. Can be tested. Assume that you have configured the Tomcat server and deployed the WebService server. Now start Tomcat and enter the access address: [url] http: // localhost: 8080/xfire126demo/services/myservice? WSDL [/url], and then go! In this way, the above result indicates that the test is successful.4. Generate client codeVery depressing. Part of the generated client code is included in the server package. It's really rubbish, rubbish !!! However, this is the function of myeclipse, and I cannot change it.5. Client TestThe following describes how to use the client code. Open the generated code: Package wstest. client;

Import java.net. malformedurlexception;
Import java. util. collection;
Import java. util. hashmap;
Import javax. xml. namespace. QNAME;
Import org. codehaus. xfire. xfireruntimeexception;
Import org. codehaus. xfire. Aegis. aegisbindingprovider;
Import org. codehaus. xfire. Annotations. annotationservicefactory;
Import org. codehaus. xfire. Annotations. jsr181.jsr181webannotations;
Import org. codehaus. xfire. Client. xfireproxyfactory;
Import org. codehaus. xfire. jaxb2.jaxbtyperegistry;
Import org. codehaus. xfire. Service. endpoint;
Import org. codehaus. xfire. Service. Service;
Import org. codehaus. xfire. Soap. abstractsoapbinding;
Import org. codehaus. xfire. Transport. transportmanager;

Public class myserviceclient {

Private Static xfireproxyfactory proxyfactory = new xfireproxyfactory ();
Private hashmap endpoints = new hashmap ();
Private Service service0;

Public myserviceclient (){
Create0 ();
Endpoint myserviceporttypelocalendpointep = service0. addendpoint (New QNAME ("http://server.wstest", "myserviceporttypelocalendpoint"), new QNAME ("http://server.wstest", "myserviceporttypelocalbinding"), "xfire. local: // myservice ");
Endpoints. Put (New QNAME ("http://server.wstest", "myserviceporttypelocalendpoint"), myserviceporttypelocalendpointep );
Endpoint myservicehttpportep = service0. addendpoint (New QNAME ("http://server.wstest", "myservicehttpport"), new QNAME ("http://server.wstest", "myservicehttpbinding"), "http: // localhost: 8080/xfire126demo/services/myservice ");
Endpoints. Put (New QNAME ("http://server.wstest", "myservicehttpport"), myservicehttpportep );
}

Public object getendpoint (endpoint ){
Try {
Return proxyfactory. Create (endpoint). getbinding (), (endpoint). geturl ());
} Catch (malformedurlexception e ){
Throw new xfireruntimeexception ("invalid URL", e );
}
}

Public object getendpoint (QNAME name ){
Endpoint endpoint = (endpoint) endpoints. Get (name )));
If (endpoint) = NULL ){
Throw new illegalstateexception ("No such endpoint! ");
}
Return getendpoint (endpoint ));
}

Public Collection getendpoints (){
Return endpoints. Values ();
}

Private void create0 (){
Transportmanager TM = (Org. codehaus. xfire. xfirefactory. newinstance (). getxfire (). gettransportmanager ());
Hashmap props = new hashmap ();
Props. Put ("Annotations. Allow. Interface", true );
Annotationservicefactory ASF = new annotationservicefactory (New jsr181webannotations (), TM, new aegisbindingprovider (New jaxbtyperegistry ()));
ASF. setbindingcreationenabled (false );
Service0 = ASF. Create (wstest. Client. myserviceporttype. Class), props );
{
Abstractsoapbinding soapbinding = ASF. createsoap11binding (service0, new QNAME ("http://server.wstest", "myservicehttpbinding"), "http://schemas.xmlsoap.org/soap/http ");
}
{
Abstractsoapbinding soapbinding = ASF. createsoap11binding (service0, new QNAME ("http://server.wstest", "myserviceporttypelocalbinding"), "urn: xfire: Transport: Local ");
}
}

Public myserviceporttype getmyserviceporttypelocalendpoint (){
Return (myserviceporttype) (this). getendpoint (New QNAME ("http://server.wstest", "myserviceporttypelocalendpoint ")));
}

Public myserviceporttype getmyserviceporttypelocalendpoint (string URL ){
Myserviceporttype Var = getmyserviceporttypelocalendpoint ();
Org. codehaus. xfire. Client. Client. getinstance (VAR). seturl (URL );
Return var;
}

Public myserviceporttype getmyservicehttpport (){
Return (myserviceporttype) (this). getendpoint (New QNAME ("http://server.wstest", "myservicehttpport ")));
}

Public myserviceporttype getmyservicehttpport (string URL ){
Myserviceporttype Var = getmyservicehttpport ();
Org. codehaus. xfire. Client. Client. getinstance (VAR). seturl (URL );
Return var;
}

Public static void main (string [] ARGs ){

Myserviceclient client = new myserviceclient ();

// Create a default service endpoint
Myserviceporttype service = client. getmyservicehttpport ();

// Todo: add custom client code here
//
// Service. yourserviceoperationhere ();

System. Out. println ("test client completed ");
System. Exit (0 );
}

}
I don't know what it means, but from the "Todo" Mark, I know: // todo: add custom client code here
//
// Service. yourserviceoperationhere (); Add the test code here: // todo: add custom client code here
//
// Service. yourserviceoperationhere ();
String hellostring = service. sayhello ("lava ");
System. Out. println (hellostring); after adding two lines of code, you can run it to see the test code. The running result is as follows: Hello, lava
Test client completed can finally breathe a sigh of relief. The complete example is running.6. SummaryI always feel that this development process is not satisfactory. In fact, there are better tools and development methods: compiling WebService, Which is troublesome for client code. It is basically impossible for client code to be written manually, unless you are willing to give amazing time and energy, this will not be worth the candle. The client development of myeclipse is too poor, mainly because the generated client code is messy. The solution is to write the client in a separate project. In fact, as long as the server side is compiled, it is completely possible to generate client code according to the wsdl url in other ways. Here we have to recommend a powerful tool idea8, which comes with the WebService development tool, the plug-in is very powerful and easy to use. In the subsequent sections, I will provide a special introduction. Please stay tuned. Of course, myeclipse is not useless, and myeclipse's server-side debugging tool is quite good. This improves the development efficiency, which is also an exclusive feature of myeclipse. Finally, I would like to warn you that even if WebService supports complex object parameters, it is not recommended because data binding is not so perfect and there are always some shortcomings. For the sake of security, we recommend that you use string as the best parameter.

This article is from the "fuyan" blog. For more information, contact the author!

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.