Spring+WebService+CXF

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;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值