Make WebService interface calls using CXF

Source: Internet
Author: User
Tags wsdl

First, the service side

Build Javaweb project, go to the official website to download the required CXF Interface release jar package, import into the project. Website address: http://cxf.apache.org/download.html

1. Establishing the Calling interface

 Package Com.ymx.cxf.server; Import Javax.jws.WebService; @WebService  Public Interface userservice {    User getUser (String name);}
View Code

2. Implement the interface

 Package Com.ymx.cxf.server; Import Javax.jws.WebService; @WebService  Public class Implements userservice {    @Override    public  User getUser (String name) {        new  User ();        User.setname (name);        User.setage (a);         return user;    }}
View Code

3. Configure Web. xml

<?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" 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>cxfserver</ display-name> <!--cxf service start a servlet--<servlet> <servlet-name>cxfservlet</servlet-name&gt      ; <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-nam E>cxfservlet</servlet-name> &LT;URL-PATTERN&GT;/CXF/*</url-pattern> </servlet-mapping> <context-param> <PARAM-NAME>CONTEXTCONFIGLOCATION&L T;/param-name> <param-value>/WEB-INF/spring-cxf.xml</param-value> </context-param> <  Listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.html</welcome-file> </ Welcome-file-list></web-app>
View Code

4. Configure Spring-cxf.xml, publish interface

<beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp//Cxf.apache.org/jaxwshttp//cxf.apache.org/schemas/jaxws.xsd "><!--introduce CXF configuration file, lower version also need to introduce the other two files--<ImportResource= "Classpath:meta-inf/cxf/cxf.xml"/> <!--configuration Note: Implementor is the specific implementation of the interface class--<jaxws:endpoi NT Implementor= "Com.ymx.cxf.server.UserServiceImpl" address= "/user" ></jaxws:endpoint> </beans>
View Code

5. Use Tomcat to publish the project to access the address of the published CXF interface

http://10.50.10.52:8080/cxfserver/cxf/user?wsdl

Cxfserver publishes the project name, CXF is the value configured in the Url-pattern in Web. XML, user is the address configured in Spring-cxf.xml.

Second, the client

1. Enter the bin directory under the extracted CXF file and generate the client code using the Wsdl2java command.

  

-p Specifies the namespace of its WSDL, which is the package name to generate the code;-d Specifies the directory where the code is to be generated;-client generates the code for the client to test the Web service; 2. Import the generated client code into the project and call the service-side code as follows:
 Packagecom.ymx.cxf.client;ImportJavax.xml.namespace.QName;/*** This class is generated by Apache CXF 2.5.9 * 2017-05-20t10:27:29.544+08:00 * generated source version:2.5.9 * /c6>*/ Public Final classuserservice_userserviceimplport_client {Private Static FinalQName service_name =NewQName ("http://server.cxf.ymx.com/", "Userserviceimplservice");  Public Static voidMain (String args[])throwsException {userserviceimplservice ss=NewUserserviceimplservice (userserviceimplservice.wsdl_location, service_name); UserService Port=Ss.getuserserviceimplport (); String name= "Test"; User result=Port.getuser (name); System.out.println ("getuser.result=" + result.getname () + ":" +result.getage ()); }}
View Code

Make WebService interface calls using CXF

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.