cxf公布webServices

cxf发布webServices

cxf发布webServices

 

1、spring配置文件

<?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-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-extension-soap.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
   	
    <jaxws:endpoint id="GreetingService" implementor="com.zcz.cxf.service.impl.GreetingServiceImpl" address="/GreetingService"/>
</beans>

 

2、服务器端

    1、接口

package com.zcz.cxf.service;  
  
import javax.jws.WebService;  
  
@WebService   
public interface GreetingService {   
   public String greeting(String userName);  
   public String say(String eat);
}  

   2、实现类

package com.zcz.cxf.service.impl;

import javax.jws.WebService;
import com.zcz.cxf.service.GreetingService;

@WebService
public class GreetingServiceImpl implements GreetingService {
	public String greeting(String userName) {
		return "你好! " + userName;
	}

	public String say(String eat) {
		return "该吃饭了" + eat;
	}
}

 

3、客服端

package com.zcz.cxf.client;  
  
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
import com.zcz.cxf.service.GreetingService;
  
public class ClientTest{  
    public static void main(String[] args) {  
    	//创建WebService客户端代理工厂    
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();    
        //注册WebService接口    
        factory.setServiceClass(GreetingService.class);    
        //设置WebService地址    
        factory.setAddress("http://localhost:8080/testWebService/cxf/GreetingService");    
        GreetingService greetingService = (GreetingService)factory.create();    
        System.out.println("开始调用webservice...");    
        System.out.println("返回的信息是:"+greetingService.say("米饭"));   
    }  
}