Example of combining spring with Apache CXF

Source: Internet
Author: User
Tags wsdl

The premise of this article is that there is already a spring project, based on how to combine with Apache CXF to develop WebService service and invoke WebService service.

1. Development WebService

1. Introduction of the JAR package

Download the latest Jar package and introduce: \apache-cxf-3.0.1\lib\* (of course, some of them are unnecessary, interested can cut their own).

2. Modify Web. xml

<servlet>        <Servlet-name>Cxf</Servlet-name>        <Servlet-class>Org.apache.cxf.transport.servlet.CXFServlet</Servlet-class>        <Load-on-startup>2</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Cxf</Servlet-name>        <Url-pattern>/services/*</Url-pattern>    </servlet-mapping>

3. Introduce a separate spring configuration file Cxf-servlet.xml

<?XML version= "1.0" encoding= "UTF-8"?><!--Licensed to the Apache software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); You are not a use of this file except in compliance with the License. Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless required by applicable l AW or agreed to writing, software distributed under the License are distributed on a "as is" BASIS, without WARRANT IES or CONDITIONS of any KIND, either express OR implied. See the License for the specific language governing permissions and limitations under the License.  -<!--START Snippet:beans -<Beansxmlns= "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.xsd Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">    <ImportResource= "Classpath:meta-inf/cxf/cxf.xml" />    <ImportResource= "Classpath:meta-inf/cxf/cxf-servlet.xml" />    <Jaxws:endpointID= "UserService"implementor= "Cn.telchina.standard.service.user.UserServiceImpl"Address= "/userservice" /></Beans><!--END Snippet:beans -

In the code above:

Jaxsw:endpoint is the key.

Adding a configuration to Web. xml

4. Development of Service Interfaces and service implementation classes

Interface class:

 Package Cn.telchina.standard.service.user; Import Javax.jws.WebService; /**  @author*/@WebServicepublicinterface  UserService {        publicboolean  updatetheuser (String user);          Public Boolean Updateuserscode (String user);    }

Interface Implementation class:

 PackageCn.telchina.standard.service.user;ImportJavax.jws.WebService; @WebService (Endpointinterface= "Cn.telchina.standard.service.user.UserService") Public classUserserviceimplImplementsUserService {@Override Public Booleanupdatetheuser (String user) {//TODO auto-generated Method Stub        return false; } @Override Public BooleanUpdateuserscode (String user) {//TODO auto-generated Method Stub        return false; }}

At this point, you can publish the program:

Http://localhost:8080/cxfProject/services

http://localhost:8080/cxfProject/services/UserService?wsdl

2. Call WebService

There are three ways to invoke the CXF service:

1. How to configure Spring beans

New configuration file: Client-beans, and configured in Web. Xml.

Client-beans.xml Content

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:OXM= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/OXM"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/OXM http://www.springframework.org/schema/oxm/ Spring-oxm-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">             <jaxws:clientID= "Client"Address= "Http://localhost:8080/cxfProject/services/UserService"ServiceClass= "Cn.telchina.standard.service.user.UserService" /></Beans>

Java code:

 Public Static void invokebyspring () {        //  TODO auto-generated method stub                new  Classpathxmlapplicationcontext (                        new string[] {"Client-beans.xml" });                 = (UserService) Context.getbean ("Client");                 Boolean response = Client.updatetheuser ("Joe");                System.out.println ("Response:" + Response);    }

The way of 2.CXF

 Public Static void Invokeservice () {            new  Jaxwsproxyfactorybean ();              Factory.setserviceclass (UserService. class );              Factory.setaddress ("Http://localhost:8080/cxfProject/services/UserService");               = (UserService) factory.create ();                           Boolean response = Service.updatetheuser ("Joe");            System.out.println ("#############" +response+ "##############");     }

3.RPC mode

 Public Static voidInvokeService2 ()throwsexception{//This is the WebService service deployed with CXF Client Access CXF//Remember, access to the CXF WebService must add namespace, otherwise it will not pass//now another problem, passing the parameters of the past the service side cannot receivejaxwsdynamicclientfactory DCF =jaxwsdynamicclientfactory.newinstance (); Org.apache.cxf.endpoint.Client Client= Dcf.createclient ("http://localhost:8080/cxfProject/services/UserService?wsdl"); //URL is the WSDL address that calls WebServiceQName name=NewQName ("http://user.service.standard.telchina.cn/", "Updatetheuser"); //namespace is a namespace, MethodName is the method nameString xmlstr = "Name"; //paramvalue is a parameter valueObject[] Objects=Client.invoke (NAME,XMLSTR); //calling the Web Service//Output Call ResultSystem.out.println (objects[0].tostring ()); }

Ps:

CXF Class Library and Axis2 class library can not be placed in a project, the person will error:

Invocation of Init method failed; Nested exception is Java.lang.NoSuchFieldError:REFLECTION

Example of combining spring with Apache 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.