Ii. CXF Entry Example

Source: Internet
Author: User

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;

 

/**

* <B> function: </B> CXF WebService server helloWorld example

 * @author hoojo

* @ CreateDate 2011-3-16 08:29:07 AM

 * @file HelloWorld.java

 * @package com.hoo.service

 * @project CXFWebService

 * @blog http://blog.csdn.net/IBM_hoojo

 * @email hoojo_@126.com

 * @version 1.0

 */

@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 @ 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;/*** <B> function: </B> CXF WebService * @ author hoojo * @ createDate 09:24:33 * @ file DeployHelloWorldService. java * @ package com. hoo. deploy * @ project CXFWebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class DeployHelloWorldService {/** * <B> function: </B> Publish WebService * @ author hoojo */public static void deployService () {System. out. println ("Server start ...... "); HelloWorldService service = new HelloWorldService (); String address =" http: // localhost: 9000/helloWorld "; Endpoint. publish (address, service);} public static void main (String [] args) throws InterruptedException {// publish WebServicedeployService (); System. out. println ("server ready ...... "); Thread. sleep (1000*60); System. out. println ("server exiting"); // The System exits after 60 seconds of hibernation. exit (0 );}}

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

Server start ...... 2011-3-17 10:17:25 org. apache. cxf. service. factory. reflectionServiceFactoryBean buildServiceFromClass information: Creating Service {http://service.hoo.com/?helloworldserviceservice from class com. hoo. service. helloWorldService2011-3-17 10:17:25 org. apache. cxf. endpoint. serverImpl initDestination information: Setting the server's publish address to be http: // localhost: 9000/helloWorld2011-03-17 10:17:25. 375: INFO: jetty-7. 2.2.v201012052011-03-17 10:17:25. 468: INFO: 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;

 

/**

* <B> function: </B> the client calls the interface required by WebService.

 * @author hoojo

* @ CreateDate 2011-3-17 09:00:00 AM

 * @file IHelloWorldService.java

 * @package com.hoo.service

 * @project CXFWebService

 * @blog http://blog.csdn.net/IBM_hoojo

 * @email hoojo_@126.com

 * @version 1.0

 */

@WebService

public 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;

 

/**

* <B> function: </B> CXF WebService client call code

 * @author hoojo

* @ CreateDate 2011-3-16 09:03:49 AM

 * @file HelloWorldServiceClient.java

 * @package com.hoo.client

 * @project CXFWebService

 * @blog http://blog.csdn.net/IBM_hoojo

 * @email hoojo_@126.com

 * @version 1.0

 */

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("hoojo"));

    }

}

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

2011-3-17 10:23:34 org. apache. cxf. service. factory. ReflectionServiceFactoryBean buildServiceFromClass information: Creating Service {found from class com. hoo. service. IHelloWorldService [result] hoojo say: Hello World

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.