Use cxf to create Web Services

Source: Internet
Author: User

Apache cxf is very simple to create Web Services.

1. Download The cxf release package, http://cxf.apache.org/download.html, here I download is apache-cxf-2.7.0.zip

2. decompress the release package.

3. Create a Java project and introduce the jar packages in the \ apache-cxf-2.7.0 \ lib directory to the project.

4. Compile the serverCode

1) Service Interface

 
Package CN. luxh. app. WS; import javax. JWS. webService; @ webservicepublic interface calculator {/*** interface method for providing the addition Service * @ Param num1 addition 1 * @ Param num2 Addition 2 * @ return */INT add (INT num1, int num2 );}

2) service implementation class

Package CN. luxh. app. WS; import javax. JWS. webService; @ WebService (endpointinterface = "CN. luxh. app. WS. calculator ", servicename =" Calculator ") public class calculatorimpl implements calculator {@ overridepublic int add (INT num1, int num2) {return num1 + num2 ;}}

@ WebService comments on the class, indicating that the method of this class will become the WebService method. AttributeEndpointinterface specifies the service interface. The servicename attribute specifies the service name.

5. Publish services

Package CN. luxh. app. WS; import javax. XML. WS. endpoint; import Org. apache. cxf. jaxws. jaxwsserverfactorybean;/*** publish service ** @ author luxh * 2012-11-5 */public class calculatorserver {/*** publish method 1 * use endpoint to publish service */Private Static void server1 () {system. out. println ("START server1... "); calculatorimpl implementor = new calculatorimpl (); string address =" http: // localhost: 8080/APP/WS/calculator "; endpoint. publish (address, implementor);}/*** release method 2 * use jaxwsserverfactorybean to create a service */Private Static void server2 () {system. out. println ("START server2... "); calculatorimpl implementor = new calculatorimpl (); // create server endpointsjaxwsserverfactorybean svrfactory = new jaxwsserverfactorybean (); svrfactory. setserviceclass (calculator. class); // sets the svrfactory interface for providing services. setaddress ("http: // localhost: 8080/APP/WS/calculator"); // you can specify the svrfactory address to provide services. setservicebean (implementor); // sets the svrfactory class of the service interface. create ();} public static void main (string [] ARGs) throws interruptedexception {server2 (); system. out. println ("Server Ready... "); thread. sleep (5x60*1000); system. out. println ("server exiting... "); system. exit (0 );}}

6. Access http: // localhost: 8080/APP/WS/calculator in a browser? The following content appears in the WSDL, indicating that the service is successfully released.

7. Write client code

1) Use the wsimport command. Go to the command line and switch to the JDK bin directory. The wsimport command is in the bin directory.

2) Run: wsimport-p cn. luxh. App. WS-keep http: // localhost: 8080/APP/WS/calculator? WSDL

-P: Specify the package name-keep: Keep the generated file

3) after the command is executed, the client code is generated in the current directory.

4) create a Java project and test it as a client. Copy the generated client code to the project.

5) test code

Package CN. luxh. app. client; import CN. luxh. app. WS. calculator; import CN. luxh. app. WS. calculatorservice; public class cxfclient {public static void main (string [] ARGs) {calculator = new calculatorservice (). getcalculatorport (); int sum = calculator. add (1, 2); system. out. println ("sum is:" + sum );}}

6) After the server is started, execute the above client code.

 

 

 

 

 

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.