spring整合cxf开发webservice

我们开发webservice接口时候,一般都是web工程,之前联调的时候,就和一个IBM的联调过一个OA接口,他们使用的是Spring整合axis  持久层用的hibernate

   下面我们来整合下spring与cxf

  建立一个web工程 ,引入cxf  lib下面的jar包

  配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>webservice_cxf_spring_day01</display-name>
  
  <!--加载spring容器-->
   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.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>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/*</url-pattern>  
    </servlet-mapping>  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
beans.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:p="http://www.springframework.org/schema/p"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xmlns:cxf="http://cxf.apache.org/core"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    http://cxf.apache.org/jaxws   
    http://cxf.apache.org/schemas/jaxws.xsd">  
      
    <import resource="classpath:META-INF/cxf/cxf.xml" />  
      
    <jaxws:endpoint id="userService" implementor="com.asiainfo.webservice.service.impl.UserServiceImpl" address="/userService" />  
  
</beans> 
SEI:

package com.asiainfo.webservice.service.interfaces;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.asiainfo.webservice.pojo.User;


@WebService
public interface IUserService {

	@WebMethod
	public void printUserName(String username);
	
	@WebMethod
	public User queryUserInfo(int id);
	
	
	@WebMethod
	public int addUserInfo(User user);
	
}
SEI实现类

package com.asiainfo.webservice.service.impl;

import javax.jws.WebService;

import com.asiainfo.webservice.pojo.User;
import com.asiainfo.webservice.service.interfaces.IUserService;


@WebService
public class UserServiceImpl implements IUserService{
	
	public void printUserName(String username){
		System.out.println(username);
	}
	
	public User queryUserInfo(int id){
		return new User(1,"曾孟良","123456");
	}
	
	public int addUserInfo(User user){
		System.out.println(user.toString());
		return 0;
	}
}
启动tomcat ,在启动tomcat的时候,其实就发布了webservice,你可以给UserServiceImpl ,加一个无参构造器。在tomcat启动时候,这个类就实例化了

  测试结果:

   

  说明已经整合成功

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值