CXF Spring Hello简单实例
1、首先下载 http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
cxf 开发包
cxf 集成spring 解压包后? 里面包括,spring 包? comms 其他包。
?
拷入jar包
?
?
拷入web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xsi:schemaLocation="http://java.sun.com/xml/ns/javaee??
???? http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
??? <context-param>
??? ??? <param-name>contextConfigLocation</param-name>
??? ??? <param-value>classpath:spring-cxf-server.xml</param-value>
??? </context-param>
??? <listener>
??? ??? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
??? </listener>
??? <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>/*</url-pattern>
??? </servlet-mapping>
??? <welcome-file-list>
??? ??? <welcome-file>index.jsp</welcome-file>
??? </welcome-file-list>
</web-app>
?
?
?
spring-cxf-server.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd?
??? http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
??? <!-- cxf 服务器端,用于创建并发布服务 -->
??? <!-- 一下三个文件位于cxf-2.1.12.jar中 -->
??? <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" />
??? <!-- 当web服务器启动的时候会创建并发布该服务 启动日志信息如下: 信息: Creating Service {http://impl.service.cxf.com/}Helloworld
??? ??? from class com.cxf.service.iface.Helloworld 2010-12-31 14:18:48 org.apache.cxf.endpoint.ServerImpl
??? ??? initDestination 信息: Setting the server's publish address to be /myService -->
??? <jaxws:endpoint id="Helloworld" implementor="com.cxf.service.impl.HelloworldImpl" address="/myService" />
</beans>?
?
?
?
?
接口
package com.cxf.service.iface;?
????
?import javax.jws.WebParam;
import javax.jws.WebService;
????
@WebService?
public interface Helloworld {?
????? public String sayHello(@WebParam(name="text") String text);?
}
?
?
?
实现
package com.cxf.service.impl;
import javax.jws.WebService;
import com.cxf.service.iface.Helloworld;
@WebService(serviceName = "Helloworld")
public class HelloworldImpl implements Helloworld {
??? public String sayHello(String text) {
??? ??? return "hello " + text;
??? }
}
?
?
?
访问:
http://localhost:8080/sgh_publish_platform/myService/sayHello/text/my-namy-is-leiwuluan
?
?
?
?
客户端:
ApplicationContext ctx = new? ClassPathXmlApplicationContext("spring-cxf-client.xml");
??? ??? Helloworld helloworld = (Helloworld) ctx.getBean("helloworldClient");
??? ??? String info = helloworld.sayHello("taoge11111");
??? ??? System.out.println(info);
?
?
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
???
??? <!-- cxf 客户端 -->
???
??? <jaxws:client id="helloworldClient" service/>
??? ???
</beans>
?
?
?
?
?
?
?