Basic cxf tutorial --- getting started example

Source: Internet
Author: User

Cxf Entry Example

Another example of helloworld is the old-fashioned helloworld. It is also the foundation and must be mastered. Let's take a look at helloworldwebservice, which is very simple.

The required jar package is as follows:

1. helloworldservice server code

Package COM. hoo. service; import javax. JWS. webparam; import javax. JWS. webService; import javax. JWS. soap. soapbinding; import javax. JWS. soap. soapbinding. style;/*** class: cxf WebService server helloworld example ** @ author e4200003458 * @ createdate 4:56:58 * @ file helloworldservice. java * @ package COM. hoo. service * @ project cxf_demo01 */@ WebService @ soapbinding (style = style. RPC) public class helloworldservice {Public String sayhello (@ webparam (name = "name") string name) {return name + "say: Hello World ";}}

It should be noted that the above server code uses the new feature annotation of JDK 5, which is very useful.
Note that if you do not add the @ soapbinding (style = style. RPC) annotation, an exception will occur:
Com. Sun. xml. Internal. ws. model. runtimemodelerexception: runtime modeler Error
In addition, if the JDK version is 1.6, the above exception also occurs.
Solutions:
1> first use the apt command to compile the main program and then generate some java files
2> Upgrade JDK to JDK or later

2. Publish helloworldservice with the following code:

Package COM. hoo. service. deploy; import javax. XML. WS. endpoint; import COM. hoo. service. helloworldservice;/*** class: released cxf WebService ** @ author e4200003458 * @ createdate 5:01:05 * @ file deployhelloworldservice. java * @ package COM. hoo. service. deploy * @ project cxf_demo01 */public class deployhelloworldservice {/*** function: publish WebService * @ author e4200003458 */public static void deployservice () {system. out. println ("service start ...... "); helloworldservice service = new helloworldservice (); string address =" http: // localhost: 9000/helloworld "; endpoint. publish (address, service);} public static void main (string ARGs []) throws interruptedexception {// publish WebService deployservice (); system. out. println ("Server Ready ...... "); thread. sleep (1000*120); system. out. println ("system exiting... "); // wait for 60 seconds after hibernation and exit system. exit (0 );}}

If no exception is found after running the main function above. The basic information is as follows:

Service start ......
23:29:15 org. Apache. cxf. Service. Factory. reflectionservicefactorybean buildservicefromclass
Information: creating service {http://service.hoo.com/?helloworldserviceservice from class com. Hoo. Service. helloworldservice
23:29:16 org. Apache. cxf. endpoint. serverimpl initdestination
Information: Setting the server's publish address to be http: // localhost: 9000/helloworld
23:29:16 org. Eclipse. Jetty. util. log. slf4jlog info
Information: jetty-7.2.2.v20101205
23:29:16 org. Eclipse. Jetty. util. log. slf4jlog info
Information: started selectchannelconnector @ localhost: 9000
Server Ready ......

Then you request in webbrowser:

Http: // localhost: 9000/helloworld? The XML content is displayed in the WSDL.

3. Customize the client to call the WebService interface. The method signature and parameter information in this interface can be seen from the content in the WSDL. The Code is as follows:

package com.hoo.service;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface IHelloWorldService {    public String sayHello(@WebParam(name = "name") String name);}

4. Compile the WebService code called by the client

Package COM. hoo. client; import Org. apache. cxf. jaxws. jaxwsproxyfactorybean; import COM. hoo. service. ihelloworldservice; public class helloworldserviceclient {public static void main (string ARGs []) {// call WebService jaxwsproxyfactorybean factory = new jaxwsproxyfactorybean (); factory. setserviceclass (ihelloworldservice. class); factory. setaddress ("http: // localhost: 9000/helloworld"); ihelloworldservice service = (ihelloworldservice) factory. create (); system. out. println ("[Result]" + service. sayhello ("e4200003458 "));}}

After running the above program, the result is as follows:

23:30:24 org. Apache. cxf. Service. Factory. reflectionservicefactorybean buildservicefromclass
Information: creating service {http://service.hoo.com/~ihelloworldserviceservice from class com. Hoo. Service. ihelloworldservice
[Result] e42100003458say: Hello World

This tutorial is adapted from: http://www.cnblogs.com/hoojo/category/276011.html

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.