Web Services CXF Framework Release 2

Source: Internet
Author: User
Tags wsdl

Original works, permission to reprint, please be sure to use hyperlinks in the form of the original source of the article, the author information and my statement. Otherwise, the legal liability will be investigated. Eternal の_☆ Address: http://blog.csdn.net/chenghui0317/article/details/9320053

First, the introduction of CXF

Apache CXF is an open-source WebService framework that CXF greatly simplifies the creation of webservice, while inheriting the xfire tradition of seamlessly integrating with spring's natural nature. The CXF framework is an SOA application development framework based on servlet technology that requires support from the JDK and servlet containers in addition to the CXF application itself, in order to properly apply enterprise applications developed based on the CXF application framework.

Ii. conditions of preparation for CXF

Required JAR Packages:

Xmlbeans-2.4.0.jar

Wss4j-1.5.9.jar

Jetty-server-7.1.6.v20100715.jar

Jetty-util-7.1.6.v20100715.jar

Geronimo-ws-metadata_2.0_spec-1.1.3.jar

Geronimo-activation_1.1_spec-1.1.jar

Geronimo-servlet_3.0_spec-1.0.jar

Velocity-1.6.4.jar

Jaxb-xjc-2.2.1.1.jar

Xml-resolver-1.2.jar

Wsdl4j-1.6.2.jar

Cxf-2.3.0.jar

Xmlschema-1.4.7.jar

Jaxb-api-2.2.1.jar

Jaxb-impl-2.2.1.1.jar

Neethi-2.0.4.jar

Geronimo-annotation_1.0_spec-1.1.1.jar

Geronimo-stax-api_1.0_spec-1.0.1.jar

: http://download.csdn.net/detail/ch656409110/5748183 (Take the jar package you need)

Third, create WebService server 1, first put the jar package into the Lib directory 2, configure the core servlet of the CXF framework in Web. xml
1 <!--CXF -  2 <servlet>    3     <Servlet-name>Cxfservlet</Servlet-name>    4     <Servlet-class>Org.apache.cxf.transport.servlet.CXFServlet</Servlet-class>    5     <Load-on-startup>1</Load-on-startup>    6 </servlet>    7 <servlet-mapping>    8     <Servlet-name>Cxfservlet</Servlet-name>    9     <Url-pattern>/services/*</Url-pattern>    Ten </servlet-mapping>  

3. Import the XML in Applicationcontext.xml and publish the WebService service.

1 <?XML version= "0" encoding= "UTF-8"?>  2 <Beans3     xmlns= "Http://www.springframework.org/schema/beans"  4 Xmlns:xsi= "Http://www.worg/2001/XMLSchema-instance"  5 xmlns:p= "http://www.springframework.org/schema/p"  6 Xmlns:tx= "Http://www.springframework.org/schema/tx"  7 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"  8 Xmlns:jaxws= "Http://cxf.apache.org/jaxws"    9 Xmlns:jaxrs= "Http://cxf.apache.org/jaxrs"    Ten xsi:schemalocation= "Http://www.springframework.org/schema/beans One http://www.springframework.org/schema/beans/spring-beans-xsd A Http://www.springframework.org/schema/tx - http://www.springframework.org/schema/tx/spring-tx-xsd - HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP the http://www.springframework.org/schema/aop/spring-aop-xsd - Http://cxf.apache.org/jaxws - http://cxf.apache.org/schemas/jaxws.xsd - Http://cxf.apache.org/jaxrs + http://cxf.apache.org/schemas/jaxrs.xsd -                         "   + Default-autowire= "ByName"   A                         >   at     <ImportResource= "Classpath:meta-inf/cxf/cxf.xml" />   -     <ImportResource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml" />   -     <ImportResource= "Classpath:meta-inf/cxf/cxf-servlet.xml" />   -        -     <!--<jaxws:endpoint id= "Facelookwebservice" address= "/facelookwebservice" implementor= " Com.facelook.webservice.server.FacelookWebServiceImpl "></jaxws:endpoint> -   -     <!--do not know why, the WebService configuration here, can only be implemented with beans, otherwise injected service is empty. But before there was a project, I could not think of the solution.  -   in     <BeanID= "Facelookwebservice"class= "Com.facelook.webservice.server.FacelookWebServiceImpl"/>    -     <Jaxws:endpointID= "FacelookWebService1"Address= "/facelookwebservice"Implementorclass= "Com.facelook.webservice.server.FacelookWebServiceImpl">   to         <Jaxws:implementorref= "Facelookwebservice"/>     +     </Jaxws:endpoint>   - </Beans>  
View Code

4, define WebService interface Facelookwebservice and implement class Facelookwebserviceimpl.

1 @WebService2 Public interface Facelookwebservice {3       4     /** 5 * Get photo album information according to the conditions passed6 * Format specification for XML7*<?XML version=\ "0\" encoding=\ "utf-8\"?> 8*<Facelook> 9*<condition> Ten*<name></name>  One*<Description></Description>  A*<PageNo></PageNo>  -*<pagesize></pagesize>  -*</condition>  the*</Facelook>  - * Here the Webparam must be specified, otherwise the call will return null - * @return -      */   + Public string Getalbumlist (@WebParam (name= "Xmlstr") string xmlstr);  - }   +    A    at @WebService - //This can be followed by a configuration that does not write a comment, as in the ApplicationContext configuration (servicename= "Facelookwebservice", endpointinterface= " Com.facelook.webservice.server.FacelookWebService ") - Public class Facelookwebserviceimpl implements facelookwebservice{ -    - @Autowired - private Albumservice Albumservice;  in        - @Override to Public string Getalbumlist (String xmlstr) { + try { -List<Album>albumlist = Getalbumpage (XMLSTR);  the Jsonarray Jsonarray = Jsonarray.fromobject (albumlist);  * return jsonarray.tostring ();  $ } catch (Exception e) {Panax Notoginseng E.printstacktrace ();  -         }   the return null;  +     }   A}
View Code

This way, basically it will be.

5, Save the Code, publish the project, start Tomact.

In the Address bar, enter: http://localhost:8080/house/services/houseWebService?wsdl to see the details of the published service side.

Shown below:

This means that the WebService server CXF released has been successful.

6, through the client call server WebService.

Client Access to axis:

public static void Main (string[] args) throws Serviceexception, RemoteException, malformedurlexception {String xmlst r = "<?xml version=\" 0\ "encoding=\" utf-8\ "?>" + "<facelook>" + "< Condition> "+" <name> home </name> "+" &LT;DESCRIPTION&GT;&L T;/description> "+" <pageno></pageno> "+" <pagesize&              Gt;</pagesize> "+" </condition> "+" </facelook> ";        Service service = new service ();        Call Call Service.createcall ();        Call.settargetendpointaddress ("http://localhost:8080/facelook/services/facelookWebService?wsdl");        QName QName = new QName ("http://server.webservice.facelook.com/", "getalbumlist");        Call.setoperationname (QName);        Call.setusesoapaction (TRUE);        The following two lines must be added, otherwise receive on the server side is not received.Call.addparameter ("Xmlstr", xmltype.xsd_string, parametermode.in);        Call.setreturntype (xmltype.xsd_string);        String result = (string) call.invoke (new object[] {xmlstr});            SYSTEM.OUT.PRINTLN (result);      Converts the returned string to the list collection//jsonarray array = jsonarray.fromobject (result);   list<album> list = Jsonarray.tolist (Array,album.class);

CXF Client Access:

public static void Main (string[] args) throws Exception {//This is CXF service with CXF Client Access WebService deployment//Remember, Access CXF The webservice must be added namespace, otherwise pass//Now another problem, pass past the parameters of the service side received jaxwsdynamicclientfactory DCF = JAXWSDYNAMICC          Lientfactory.newinstance (); Org.apache.cxf.endpoint.Client Client = dcf.createclient ("http://localhost:8080/facelook/services/          Facelookwebservice?wsdl ");          The URL is the WSDL address of the calling WebService QName name=new QName ("http://server.webservice.facelook.com/", "getalbumlist");                   Namespace is a namespace, MethodName is the method name String xmlstr = "<?xml version=\" 0\ "encoding=\" utf-8\ "?>" + "<facelook>" + "<condition>" + "<name&gt            Home </name> "+" <description></description> "+" <pageno></pageno> "+" <pagesize></pagesize>"          + "</condition>" + "</facelook>";           Paramvalue is the parameter value object[] Objects=client.invoke (NAME,XMLSTR);  Call the Web service//output call result System.out.println (objects[0].tostring ());   }

In this case, the XML specification passed by the server itself, and then to parse, get parameters, perform the corresponding operation, return the desired results to the calling client.

Web Services CXF Framework Release 2

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.