Apache CXF webservice 注入Spring Bean

本文介绍了一个具体的Webservice接口实现案例,包括接口定义、实现类及Spring配置等。通过该案例,读者可以了解到如何使用Java技术栈搭建并实现Webservice服务,如账号增删改等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

webservice 接口定义:

package com.wlsq.kso.webservice;

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

@WebService
public interface AccountInsert {
	@WebMethod
	String insert(@WebParam(name="username") String username,@WebParam(name="password") String password,@WebParam(name="openid") String openid,@WebParam(name="type") String type,@WebParam(name="client_id") String client_id);
	
	@WebMethod
	String update(@WebParam(name="username") String username,@WebParam(name="password") String password,@WebParam(name="openid") String openid);
}

package com.wlsq.kso.webservice;

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

@WebService
public interface AccountDelete {
	@WebMethod
	String delete(@WebParam(name="username") String username,@WebParam(name="password") String password,@WebParam(name="openid") String openid);
}


webservice 接口定义实现类:

package com.wlsq.kso.webservice.impl;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wlsq.kso.entity.Account;
import com.wlsq.kso.service.IAccountService;
import com.wlsq.kso.util.ErrorDesc;
import com.wlsq.kso.webservice.AccountInsert;

@WebService(endpointInterface = "com.wlsq.kso.webservice.AccountInsert", serviceName = "accountInsert", targetNamespace = "http://dao.cxf.ws.com/")
public class AccountInsertImpl implements AccountInsert {
	@Resource
	private IAccountService accountService;

	@Override
	public String insert(String username, String password, String openId,
			String type, String client_id) {
		// TODO Auto-generated method stub
		String result = "";

		ErrorDesc desc = new ErrorDesc();
		if (StringUtils.isEmpty(username)) {
			desc.setError_code("101");
			desc.setError_desc("用户名不能为空");

			result = JSON.toJSONString(desc);
		}
		if (StringUtils.isEmpty(password)) {
			desc.setError_code("102");
			desc.setError_desc("密码不能为空");

			result = JSON.toJSONString(desc);
		}
		if (StringUtils.isEmpty(openId)) {
			desc.setError_code("103");
			desc.setError_desc("唯一编码不能为空");

			result = JSON.toJSONString(desc);
		}

		Account account = accountService.selectByUserPass(username, password);
		if (account == null) {
			account = accountService.selectByOpenId(openId);
			if (account == null) {
				account = new Account();
				account.setOpenId(openId);
				account.setUsername(username);
				account.setPassword(password);
				account.setPlateType(type);
				account.setSystemType(client_id);

				int num = accountService.insert(account);
				if (num > 0) {
					desc.setError_code("106");
					desc.setError_desc("数据插入成功");
					result = JSON.toJSONString(desc);
				} else {
					desc.setError_code("107");
					desc.setError_desc("数据插入失败");
					result = JSON.toJSONString(desc);
				}
			} else {
				desc.setError_code("105");
				desc.setError_desc("唯一编码重复");
				result = JSON.toJSONString(desc);
			}
		} else {
			desc.setError_code("104");
			desc.setError_desc("用户名或密码重复");

			result = JSON.toJSONString(desc);
		}
		return result;
	}

	public IAccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}	

	@Override
	public String update(String username, String password, String openid) {
		// TODO Auto-generated method stub
		String result = "";

		ErrorDesc desc = new ErrorDesc();
		if (StringUtils.isEmpty(username)) {
			desc.setError_code("101");
			desc.setError_desc("用户名不能为空");

			result = JSON.toJSONString(desc);
			return result;
		}
		if (StringUtils.isEmpty(password)) {
			desc.setError_code("102");
			desc.setError_desc("密码不能为空");

			result = JSON.toJSONString(desc);
			return result;
		}
		if (StringUtils.isEmpty(openid)) {
			desc.setError_code("103");
			desc.setError_desc("唯一编码不能为空");

			result = JSON.toJSONString(desc);
			return result;
		}
		
		Account account = new Account();
		account.setOpenId(openid);
		account.setPassword(password);
		account.setUsername(username);

		int num = accountService.updateByUserOpenId(account);
		if (num > 0) {
			desc.setError_code("110");
			desc.setError_desc("数据更新成功");
			result = JSON.toJSONString(desc);
			return result;
		} else {
			desc.setError_code("111");
			desc.setError_desc("数据更新失败");
			result = JSON.toJSONString(desc);
			return result;
		}
	}

}


package com.wlsq.kso.webservice.impl;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.wlsq.kso.service.IAccountService;
import com.wlsq.kso.util.ErrorDesc;
import com.wlsq.kso.webservice.AccountDelete;

@WebService(endpointInterface = "com.wlsq.kso.webservice.AccountDelete", serviceName = "accountDelete", targetNamespace = "http://dao.cxf.ws.com/")
public class AccountDeleteImpl implements AccountDelete {
	@Resource
	private IAccountService accountService;
	@Override
	public String delete(String username, String password, String openid) {
		// TODO Auto-generated method stub
		String result = "";

		ErrorDesc desc = new ErrorDesc();
		if (StringUtils.isEmpty(username)) {
			desc.setError_code("101");
			desc.setError_desc("用户名不能为空");

			result = JSON.toJSONString(desc);
			return result;
		}
		if (StringUtils.isEmpty(password)) {
			desc.setError_code("102");
			desc.setError_desc("密码不能为空");

			result = JSON.toJSONString(desc);
			return result;
		}
		if (StringUtils.isEmpty(openid)) {
			desc.setError_code("103");
			desc.setError_desc("唯一编码不能为空");

			result = JSON.toJSONString(desc);
			return result;
		}

		int num = accountService.deleteByUserPassOpenId(username, password,
				openid);
		if (num > 0) {
			desc.setError_code("108");
			desc.setError_desc("数据删除成功");
			result = JSON.toJSONString(desc);
			return result;
		} else {
			desc.setError_code("109");
			desc.setError_desc("数据删除失败");
			result = JSON.toJSONString(desc);
			return result;
		}
	}
	public IAccountService getAccountService() {
		return accountService;
	}
	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}
	
	

}

apache-cxf.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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 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/tx
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-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">

	<!--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" />
	
	<bean id="accountService" class="com.wlsq.kso.service.impl.AccountServiceImpl">		
	</bean>
	<bean id="accountInsertService" class="com.wlsq.kso.webservice.impl.AccountInsertImpl">
		<property name="accountService" ref="accountService"></property>
	</bean>
	<bean id="accountDeleteService" class="com.wlsq.kso.webservice.impl.AccountDeleteImpl">
		<property name="accountService" ref="accountService"></property>
	</bean>
	

	<!--服务端发布的webservice 
		在spring中使用jaxws:endpoint元素来暴露Web Service 
		id:指在spring配置的bean的ID 
		Implementor:指明具体的实现类
		Address:指明这个web service的相对地址 -->
	<jaxws:endpoint id="helloWorld" implementor="com.wlsq.kso.webservice.impl.HelloWorldImpl"
		address="/HelloWorld" />
	<!--服务端发布的webservice 
		在spring中使用jaxws:endpoint元素来暴露Web Service 
		id:指在spring配置的bean的ID 
		Implementor:指明具体的实现类
		Address:指明这个web service的相对地址 -->	
	 <jaxws:endpoint id="accountInsert" 
                implementor="#accountInsertService" 
                address="/AccountInsert"> 
     </jaxws:endpoint> 
     <!--服务端发布的webservice 
		在spring中使用jaxws:endpoint元素来暴露Web Service 
		id:指在spring配置的bean的ID 
		Implementor:指明具体的实现类
		Address:指明这个web service的相对地址 -->	
	 <jaxws:endpoint id="accountDelete" 
                implementor="#accountDeleteService" 
                address="/AccountDelete"> 
     </jaxws:endpoint> 
	
</beans>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值