cxf2.7.10 与 spring3.0.5集成

时间:2023-03-10 01:13:20
cxf2.7.10 与 spring3.0.5集成

开发环境:

NetBeans7.4

Tomcat 6.0.32

一 服务端:

1:新建JavaWeb工程 cxfspring-server,导入jar包如下图所示:

cxf2.7.10 与 spring3.0.5集成

2:在web.xml文件中添加如下配置项:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<!--监听spring-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--监听CXFServlet-->
<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>/ws/*</url-pattern>
</servlet-mapping>

3:新建一个com.test.server包,里面创建接口;新建一个com.test.server.impl包,里面实现接口,结构如下:

cxf2.7.10 与 spring3.0.5集成

(1):IArithmeticServer.java

@WebService
public interface IArithmeticServer {
/**
* 两个整数相加
* @param opr1
* @param opr2
* @return
*/
public int addition(int opr1,int opr2); /**
* 两个整数相减
* @param opr1
* @param opr2
* @return
*/
public int subtraction(int opr1, int opr2);
}

(2):ArithmeticServerImp.java

targetNamespace = "http://server.test.com/"一定要配置否则的话看这里:
http://www.cnblogs.com/yshyee/p/3633537.html
@WebService(
endpointInterface = "com.test.server.IArithmeticServer",
targetNamespace = "http://server.test.com/")
public class ArithmeticServerImp implements IArithmeticServer{ public int addition(int opr1, int opr2) {
return opr1+opr2;
} public int subtraction(int opr1, int opr2) {
return opr1-opr2;
} }

(3):spring-config.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.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-servlet.xml" /> <!--一个endpoint对应于一个服务-->
<jaxws:endpoint
id="arithmeticServer"
implementor="com.test.server.impl.ArithmeticServerImp"
address="/ArithmeticServer"/> </beans>

二 客户端:

1:新建JavaWeb工程,cxf-spring-client,导入的jar包与服务端相同。

2:新建一个测试类:通过动态方式调用CXF WebService。

public class Test {
public static void main(String args[]) throws Exception{
fun1();
} public static void fun1() throws Exception{
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
Client client = clientFactory.createClient("http://localhost:8080/cxfspring-server/ws/ArithmeticServer?wsdl"); Object[] result = client.invoke("addition", 1,2);
System.out.println("1+2:"+result[0]); result = client.invoke("subtraction", 1,2);
System.out.println("1-2:"+result[0]);
}
}

3:运行结果:

信息: Created classes: com.test.server.Addition, com.test.server.AdditionResponse, com.test.server.ObjectFactory, com.test.server.Subtraction, com.test.server.SubtractionResponse
1+2:3
1-2:-1
成功构建 (总时间: 3 秒)