webservices相关配置-cxf

本文详细介绍了如何使用Spring框架整合Apache CXF组件来实现WebService服务端和客户端的搭建,包括配置文件的编写、JavaBean类的实现以及客户端的调用方法。
1.增加applicationContext-webservoce.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"/>
<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
      <jaxws:endpoint id="helloWorld" implementor="com.jetsum.webservice.impl.HelloWorldImpl" address="/helloWorld" />
      <jaxws:endpoint id="helloworld6" implementor="com.jetsum.webservice.impl.Helloworld1Impl" address="/helloworld2" />
   
</beans>




2.applicationContext.xml文件中增加资源的导入
    <import resource="applicationContext-WebService.xml" />


3. 在web.xml 文件中增加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>/webservice/*</url-pattern>
  </servlet-mapping>




4.创建java bean类


HelloWorld.java(接口类)
package com.jetsum.webservice;


import java.util.List;
import javax.jws.WebService;
import javax.jws.WebParam;
import com.jetsum.zhxy.entity.StuStudent;
/**

* @author Daley
* 调用webservice服务及接口方法
*
*/


@WebService
public interface HelloWorld {
    String sayHi(@WebParam(name="text")String text);
    String sayHiToUser(StuStudent user);
    String[] SayHiToUserList(List<StuStudent> userList);
}




HelloWorldImpl.java(实现类)
package com.jetsum.webservice.impl;


import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import com.jetsum.webservice.HelloWorld;
import com.jetsum.zhxy.entity.StuStudent;
/**

* @author Daley
* 对应webservice服务及接口的实现方法
*
*/




@WebService(endpointInterface="com.jetsum.webservice.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld {


Map<Integer, StuStudent> users = new LinkedHashMap<Integer, StuStudent>();


public String sayHi(String text) {
return "Hello " + text;
   }


public String sayHiToUser(StuStudent user) {
users.put(users.size()+1, user);
return "Hello "+ user.getName();
   }


   public String[] SayHiToUserList(List<StuStudent> userList) {
String[] result = new String[userList.size()];
int i=0;
for(StuStudent u:userList){
    result[i] = "Hello " + u.getName();
    i++;
}
return result;
   }


}






WebserviceCenter.java(测试类)


package com.jetsum.webservice;


import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;


import com.jetsum.zhxy.entity.StuStudent;


public class WebserviceCenter {

public static <T> T getService(Class<T> tag){
//创建调用客户端生成Factory用于调用Webserivce
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
//设置的webservice服务端接口

   svr.setServiceClass(tag);
   
   //设置调用地址


   svr.setAddress(getUrlByClassname(tag));
   
  
return (T)svr.create();
}


/**
* @param args
*/
public static void main(String[] args) {
HelloWorld hw =  WebserviceCenter.getService(HelloWorld.class);

   System.out.println(hw.sayHi("fda"));
   
   StuStudent s=new StuStudent();
   s.setName("linda");
   System.out.println(hw.sayHiToUser(s));


}

public static String getUrlByClassname(Class c){


//运营平台客户端调用地址
//helloworld例子
if(c==HelloWorld.class) return "http://localhost:6033/zhxy/webservice/helloWorld?wsdl";


//学籍系统
return null;
}


}


*************************************************
以上是server服务端通过Spring整合配置的,下面我们将Client端也通过Spring配置完成整合
首先增加applicationContext-client.xml配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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-extension-soap.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    
    <jaxws:client id="userWsClient" serviceClass="com.hoo.service.IComplexUserService" 
        address="http://localhost:8080/webservice/helloWorld"/>
</beans>


客户端代码
package com.hoo.client;
 
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hoo.entity.User;
import com.hoo.service.IComplexUserService;
 
/**
 * <b>function:</b>请求Spring整合CXF的WebService客户端
 * @author hoojo
 * @createDate 2011-3-28 下午03:20:35
 * @file SpringUsersWsClient.java
 * @package com.hoo.client
 * @project CXFWebService
 * @blog http://blog.youkuaiyun.com/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
public class SpringUsersWsClient {
 
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
        
        IComplexUserService service = ctx.getBean("userWsClient", IComplexUserService.class);
        
        System.out.println("#############Client getUserByName##############");
        User user = service.getUserByName("hoojo");
        System.out.println(user);
        
        user.setAddress("China-Guangzhou");
        service.setUser(user);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值