1.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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring配置文件的加载 -->
<context-param>
<param-name>contextConfigLocation </param-name>
<param-value>/WEB-INF/classes/applicationcontext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- cxf的加载 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
2.spring的配置文件即applicationcontext.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">
<!--以下三个资源是必须有的 -->
<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="hello" class="com.cxf.server.HelloImpl" />
<jaxws:endpoint id="hellows" implementor="#hello" address="/Hello" />
<bean id="client" class="com.cxf.client.Hello" factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.cxf.client.Hello"/>
<property name="address" value="http://localhost:8080/Spring_WebService/Hello"/>
</bean>
</beans>
3.客户端代码:
package com.cxf.client;
import javax.jws.WebService;
@WebService
public interface Hello {
String SayHi(String text);
}
package com.cxf.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloText {
public static void main(String[] args){
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(com.cxf.server.Hello .class);
factory.setAddress("http://localhost:8080/Spring_WebService/Hello");
com.cxf.server.Hello service=(com.cxf.server.Hello) factory.create();
System.out.println("invoke webservice...");
System.out.println("message context is:" + service.SayHi("i'm jjd"));
// JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
// factory.setServiceClass(IHelloWorld.class);
// factory.setAddress("http://localhost:9000/HelloWorld");
// IHelloWorld iHelloWorld = (IHelloWorld)factory.create();
// System.out.println("invoke webservice...");
// System.out.println("message context is:"+iHelloWorld.sayHi(" Josen"));
// System.out.println("The Calculated Result is:"+iHelloWorld.sum(10L, 20L));
//
//
//
// ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
// Hello client = (Hello)context.getBean("client");
// String s="test";
// if (client!=null){
// s = client.SayHi("i'm jjd");
// }
// System.out.println("服务器返回值是:"+s);
}
}
4.服务端代码
package com.cxf.server;
import javax.jws.WebService;
@WebService
public interface Hello {
String SayHi(String text);
}
package com.cxf.server;
import javax.jws.WebService;
@WebService(endpointInterface="com.cxf.server.Hello")
public class HelloImpl implements Hello {
public String SayHi(String text) {
//System.out.println("客户端传值是:"+text);
int n = text.indexOf(" ");
text = text.substring(n+1, text.length());
return "你好 " + text;
}
}