WebService Summary (i)--use CXF to advertise and invoke WebService (do not use spring)

Source: Internet
Author: User
Tags wsdl

CXF and Axis2 are two of the more popular WebService frameworks, and then I'll write a few blogs about how to use both of these frameworks.

First of all, first introduce the use of CXF.


CXF announced WebService there are many ways. Here I introduce three kinds:

1, do not use spring. Cxf himself announced WebService

2, do not use SPRING,CXF manual release WebService

3. Use Spring + CXF to publish WebService


This blog provides an example of the first approach--not using SPRING,CXF to proactively publish WebService.


Service side:

The folder structure is as follows:


Ihelloworldserver Code:

<span style= "Font-family:microsoft yahei;font-size:18px;" >package Com.test.server;public Interface Ihelloworldserver {public string SayHello (String username);} </span>

Helloworldserverimp Code:
<span style= "Font-family:microsoft yahei;font-size:18px;" >package Com.test.server;public class Helloworldserverimp implements Ihelloworldserver {@Overridepublic String SayHello (String username) {return username+ ": HelloWorld";}} </span>
These two are the most simple Java classes.


Webservlet Code:

<span style= "Font-family:microsoft yahei;font-size:18px;" >package Com.test.server;import Javax.servlet.servletconfig;import Org.apache.cxf.bus;import Org.apache.cxf.busfactory;import Org.apache.cxf.frontend.serverfactorybean;import Org.apache.cxf.transport.servlet.cxfnonspringservlet;public class Webservlet extends Cxfnonspringservlet {//private        Static final String Service_suffix = "";p rivate static final long serialversionuid = 1L;                  @Override protected void Loadbus (ServletConfig servletconfig) {Super.loadbus (servletconfig);          Bus bus = Getbus ();          Busfactory.setdefaultbus (bus);  Helloworldserverimp HelloWorld = new Helloworldserverimp ();//Implementation class Serverfactorybean Serverfactorybean = new Serverfactorybean (); Server Factory Serverfactorybean.setserviceclass (ihelloworldserver.class);//Interface Class Serverfactorybean.setaddres S ("/helloworld");         Service Request Path Serverfactorybean.setservicebean (HelloWorld); Serverfactorybean.create (); }}</span>
This servlet is used to advertise webservice when the program is deployed. The container will invoke this method on its own initiative.


Web. xml file

<span style= "Font-family:microsoft yahei;font-size:18px;" ><?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "Webapp_ ID "version=" 3.0 "><display-name>cxf_demo</display-name><welcome-file-list><welcome-file >index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file> Index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file> Default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> <display-name>cxf_demo</display-name><servlet><servlet-name>cxfservlet</ Servlet-name><servlet-class>com.test.server.webservlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name >cxfservlet</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping></ Web-app></span>

By posting the above items into Tomcat, he will voluntarily announce the webservice. Enter the URL: http://localhost:8080/cxf_demo_noSpring_1/ws, you can see the published good WebService. Input URL: Http://localhost:8080/cxf_demo_noSpring_1/ws/helloWorld?

Wsdl. View the WSDL file to display correctly. Description WebService announced success.


Client

Helloworldclient Code:

<span style= "Font-family:microsoft yahei;font-size:18px;" >package Com.test.client;import Org.apache.cxf.jaxws.endpoint.dynamic.jaxwsdynamicclientfactory;public Class helloworldclient {public static void main (string[] args) {jaxwsdynamicclientfactory DCF = Jaxwsdynamicclientfactory.newinstance (); Org.apache.cxf.endpoint.Client Client = Dcf.createclient ("http:// Localhost:8080/cxf_demo_nospring_1/ws/helloworld?

WSDL "); Object[] Objects;try {objects = Client.invoke ("SayHello", "Haitao");//Output call result System.out.println (objects[0].tostring ( ));} catch (Exception e) {e.printstacktrace ();}}} </span>


After run, the results are as follows:



At this point, the first approach is complete, and the next blog I will continue to introduce other methods.




WebService Summary (i)--use CXF to advertise and invoke WebService (do not use spring)

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.