Web Service (ii) CXF automatically publishes Web service (No Spring)

Source: Internet
Author: User
Tags soap wsdl

There are two main frameworks for Web service implementation, CXF and axis, the pros and cons of these two frameworks, and we're using the CXF framework in this project, and first look at how remote calls are implemented without the integration of spring.

comparison between Axis and CXF

in the SOA realm, we believe that Web service is the building block of the SOA architecture (building blocks). Both frameworks are developed from existing open source projects. Which of these two frameworks is better?


comparing Axis2 and CXF by a comparison matrix becomes a realistic meaning. The main differences are in the following areas:

The 1.CXF supports Ws-addressing,ws-policy, WS-RM, ws-security, and WS-I Basic profiles. AXIS2 does not support Ws-policy, but promises to support it in the following version.

2. CXF can support spring very well. Axis2 No, this is why we choose CXF to implement Web service functionality in this project, and we use spring to integrate with spring in the background.

3. AXIS2 supports a wider range of data and pairs, such as xmlbeans,jibx,jaxme and Jaxbri, and its custom data-bound adb.

4. Axis2 supports multiple languages-in addition to Java, he supports the C + + version.


CXF Publishing a Web service instance

1. Project structure




2. Interfaces and Implementation classes

Package Com.test.webservice;public interface Ihelloworld {public string SayHello (String username);}

Package Com.test.webservice;public class Helloworldimpl implements Ihelloworld {@Overridepublic string SayHello (string Username) {return username+ "Lilongsheng";}}
3.Servlet Filter

Package Com.test.webservice;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 long serialversionuid = -4585672797532240291l; @Overrideprotected void Loadbus (ServletConfig SC) {      Super.loadbus (SC);      Global configuration Bus bus = Getbus ();        Use global configuration Busfactory.setdefaultbus (bus);      Interface Implementation class Helloworldimpl HelloWorld = new Helloworldimpl ();       Web Server Factory Serverfactorybean Serverfactorybean = new Serverfactorybean ();         Set the Service interface class Serverfactorybean.setserviceclass (Ihelloworld.class);          Service Request Path Serverfactorybean.setaddress ("/helloworld");          Set the Service implementation class Serverfactorybean.setservicebean (HelloWorld);  Serverfactorybean.create (); }}
In this filter set specify which class or interface can be published as Web service access, the interface and implementation classes are set into the service factory bean, call the factory's creation method can create a WebService object.

4.web.xml file Configuration

<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 "> <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>Web_Service_No-Spring</display-name> <servlet > <servlet-name>CXFServlet</servlet-name> <servlet-class>com.test.webservice.webser        Vlet</servlet-class>  <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet -name>cxfservlet</servlet-name> <url-pattern>/ws/*</url-pattern> &LT;/SERVLET-MAPPING&G  T </web-app>

5. The test is successful, enter the following address in the browser: HTTP://LOCALHOST:8082/WEB_SERVICE_NO-SPRING/WS, the following information appears


Description The Publishing Web service interface succeeded.
Browser input: HTTP://LOCALHOST:8082/WEB_SERVICE_NO-SPRING/WS/HELLOWORLD?WSDL, the configuration file will appear

<wsdl:definitions xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" xmlns:wsdl= "http://schemas.xmlsoap.org/wsdl/" xmlns:tns= "http://webservice.test.com/" xmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1= "http// Schemas.xmlsoap.org/soap/http "Name=" Ihelloworld "targetnamespace=" http://webservice.test.com/"&GT;&LT;WSDL: Types><xsd:schema xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" xmlns:tns= "http://webservice.test.com/" attributeformdefault= "Unqualified" elementformdefault= "qualified" targetnamespace= "http://webservice.test.com/" ><xsd:element name= "SayHello" type= "Tns:sayhello"/><xsd:complextype name= "SayHello" ><xsd: Sequence/></xsd:complextype><xsd:element name= "Sayhelloresponse" type= "Tns:sayHelloResponse"/> <xsd:complextype name= "Sayhelloresponse" ><xsd:sequence><xsd:element minOccurs= "0" name= "return" Type= "Xsd:string"/></xsd:sequence></xsd:complextype></xsd:schema></wsdl:types>< Wsdl:message NAMe= "Sayhelloresponse" ><wsdl:part element= "Tns:sayhelloresponse" name= "parameters" ></wsdl:part> 
..............................................................................................................................
..............................................................................................................................

6. Client Test class

public class TestClient {public static void main (string[] args) {          jaxwsdynamicclientfactory DCF = jaxwsdynamicclientf Actory.newinstance ();          Org.apache.cxf.endpoint.Client Client = dcf.createclient ("http://localhost:8082/Web_Service_No-Spring/ws/ Helloworld?wsdl ");          Object[] objects;          try {              objects = Client.invoke ("SayHello");              Output call result              System.out.println (objects[0].tostring ()),          } catch (Exception e) {              e.printstacktrace ();          }       }  }
The automatic publishing of Web service through CXF can be done at this point. This method is released when the container is launched, the entire interface or class can be published, no specific know which method, obviously not flexible enough, the next articledescribes CXF manual configuration classes, methods published as Web Service, which methods you need to publish which methods are flexible.


AXIS2 allows itself to publish Web Service as a standalone application, and provides a number of features and a good model that can be constantly added to new functionality through its own architecture (modular architecture). Some developers think this approach is too cumbersome for their needs. These developers will prefer CXF.

Axis2 appeared earlier, cxf catching speed fast. My advice is: if you need multi-lingual support, you should choose AXIS2. If you need to focus your implementation on Java and want to integrate with spring, CXF is a better choice, especially to embed your Web service in other programs. For example, some classes in a project are published as Web service methods.

Web Service (ii) CXF automatically publishes Web service (No 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.