Apache-cxf2.5 + spring DEMO/code

本文详细介绍了如何利用CXF框架构建企业级Web服务,包括服务接口设计、实现和服务调用流程。通过提供的代码示例,展示了UserService接口及其实现类UserServiceImpl的结构,以及客户端如何通过CXF进行服务调用。此外,还提供了Spring配置文件示例,确保服务能够在应用中正常运行。

cxf的作用、优点和缺点网上很多,请自行了解。


建立web项目 cxf  结构 如下图

下面是代码:

User

package demo.cxf;

import java.util.Date;

public class User {
	private Date birthday;
	private int height;
	private String password;
	private String sex;
	private String sign;
	private String username;

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}
UserService

package demo.cxf;

import javax.jws.WebService;

@WebService
public interface UserService {
	public void changeUserState(User user, int state);

	public boolean checkUserExists(String username);

	public void createUser(User user);

	public void cusumePremiumPoint(String username, int points);

	public void deleteUser(String username) throws NotFoundException;

	public void increasePremiumPoint(String username, int points);

	public void updateUserInformation(User user);
}

UserServiceImpl

package demo.cxf;

import javax.jws.WebService;
/**
 * 
 * @author Administrator
 * @date 2011-12-11
 * 
 */
/*
 * @WebService,必选的标注。用于导出的服务接口及其实现类
 * endpointInterface	指定服务接口的Java类。通常用于服务实现类的标注。应当指定类的全名,如demo.cxf.UserService
 * serviceName	定义服务名,与名域一起唯一标识一个服务。默认是其Java类名
 */
@WebService(endpointInterface = "demo.cxf.UserService", serviceName = "/UserService")
public class UserServiceImpl implements UserService{

	public void changeUserState(User user, int state) {
		print("changeUserState"); 
	}
	public boolean checkUserExists(String username) {
		print("checkUserExists");
		return false;
	}
	public void createUser(User user) {
		print("createUser");
		print(user.getUsername());
	}
	public void cusumePremiumPoint(String username, int points) {
		print("consumePremiumPoint");
	}
	public void deleteUser(String username) throws NotFoundException {
		throw new NotFoundException("我故意没找到");
	}
	public void increasePremiumPoint(String username, int points) {
		print("increasePremiumPoint");
	}
	public void updateUserInformation(User user) {
		print("updateUserInformation");
	}
	private void print(Object o) {
		System.out.println(o);
	}

}

NotFoundException

package demo.cxf;

public class NotFoundException extends Exception {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5356795954914115184L;

	public NotFoundException() {
		super();
	}

	public NotFoundException(String message, Throwable cause) {
		super(message, cause);
	}

	public NotFoundException(String message) {
		super(message);
	}

	public NotFoundException(Throwable cause) {
		super(cause);
	}
}

Client

package demo.cxf;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {
	private static  void deleteUser(String username) throws NotFoundException{
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(UserService.class);
		factory.setAddress("http://localhost:8080/cxf/services/UserService");
		UserService client = (UserService)factory.create();
		client.deleteUser(username);
	}
	
	public static void main(String[] args) {
		try {
			Client.deleteUser("lin");
		} catch (NotFoundException e) {
			e.printStackTrace();
		}	
	}
}


spring配置文件 beanRefServer.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:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:context="http://www.springframework.org/schema/context"
	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://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">
	<context:component-scan base-package="demo.cxf" />
	
	<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="UserService" implementor="demo.cxf.UserServiceImpl" 
	address="/UserService"></jaxws:endpoint>
	

</beans> 

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>cxf</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/beanRefServer.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值