Integration of cxf implementation and spring to publish WebService

Source: Internet
Author: User

Upgrade the project framework in these days .......................... you must learn how to publish WebService ............................. integration with spring is essential.

1 ........................................ create a web project springcxf

2 ................................. import the required rack package (I tried to find the minimum configuration, at least need the rack package as follows, you can directly copy the Lib package under the apache-cxf-2.6.1 to the project lib directory, OK, huh, huh)

3 ............................ the next step is web. xml configuration to filter spring through servlet, so as to find the released Interface

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" 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_2_5.xsd" id = "webapp_id" version = "2.5"> <! -- Configuration file to be loaded by spring --> <context-param> <param-Name> contextconfiglocation </param-Name> <param-value> classpath: applicationContext-server.xml </param-value> </context-param> <listener-class> Org. springframework. web. context. contextloaderlistener </listener-class> </listener> <! -- Cxf service starts servlet --> <servlet-Name> cxfservlet </servlet-Name> <servlet-class> Org. apache. cxf. transport. servlet. cxfservlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-Name> cxfservlet </ servlet-Name> <URL-pattern>/services/* </url-pattern> </servlet-mapping> <welcome-file-List> <welcome-File> index. JSP </welcome-File> </welcome-file-List> </Web-app>

4 ..................................... create a new applicationContext-server.xml file in the src directory for spring ing and add integration configuration with cxf

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://cxf.apache.org/jaxws      http://cxf.apache.org/schemas/jaxws.xsd"><import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /></beans>

5 .................................. write interface classes and add cxf annotations

package com;import javax.jws.WebParam;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;@WebService@SOAPBinding(style = Style.RPC)public interface IComplexUserService {public String getUserByName(@WebParam(name = "username") String username);public void setUser(String username);}

6 .................................. write the interface implementation class with the cxf Annotation

package com;import javax.jws.WebParam;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;@WebService@SOAPBinding(style = Style.RPC)@SuppressWarnings("deprecation")public class ComplexUserService implements IComplexUserService {public String getUserByName(@WebParam(name ="username")String username) {return "Hello:"+username;}public void setUser(String username) {System.out.println(username);}}

7 ...... configure the spring ing file,

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://cxf.apache.org/jaxws      http://cxf.apache.org/schemas/jaxws.xsd"><import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><bean id="userServiceBean" class="com.ComplexUserService" /><jaxws:server id="userService" serviceClass="com.IComplexUserService"address="/userService"><jaxws:serviceBean><ref bean="userServiceBean" /></jaxws:serviceBean></jaxws:server></beans>

8 ............................ start the server for publishing and deployment, and enter http: // localhost: 8080/springcxf/services/userservice in the browser? The following result is displayed:

9 ........................... the release is successful.

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.