WebService服务端——CXF和Spring的整合

一、在Spring开发环境的基础上导入Jar包:
- cxf.jar
- commons-logging.jar
- neethi.jar
- XmlSchema.jar
- wsdl4j.jar

二、创建WebService接口
package com.demo;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
	public String sayHi(@WebParam(name = "text") String text);
}

三、编写实现类
package com.demo;

import javax.jws.WebService;

@WebService(endpointInterface = "com.demo.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
	public String sayHi(String text){
		return "Hello "+text;
	}
}

四、配置application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
    default-autowire="byName"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
						http://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"/>
 	
 	<jaxws:endpoint id="helloWorld"
	              implementor="com.demo.HelloWorldImpl"
	              address="/helloWorld" />
	
</beans>

五、web.xml中加入如下配置
	<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>/webservice/*</url-pattern>
	</servlet-mapping>
	<context-param>

六、启动Tomcat,访问 http://localhost:8080/YHPC/webservice/helloWorld?wsdl查看是否配置成功。

猜你喜欢

转载自lihao.iteye.com/blog/1851578