Apache CXF webservice 注入Spring Bean
2016-06-02 19:50
501 查看
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);
}
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;
}
}
}
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>
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>
相关文章推荐
- xampp 安装后无法启动apache 的解决方法
- Windows下安装Apache+OpenSSL+PHP
- 安装Apache2.4步骤问题(1)
- 安装apache2.4版本中需要注意的问题(2)
- Apache搭建两级代理器自由访问国外网站
- Apache DdlUtils入门
- apache防盗链
- 使用apache benchmark(ab) 测试报错: apr_socket_recv: Connection timed out (110)
- 使用apache benchmark(ab) 测试报错: apr_socket_recv: Connection timed out (110)
- Log4j使用
- Mac下启动Apache配置
- Windows环境下,Apache,MySQL等服务以普通用户身份运行具体设置
- Web性能优化之Apache篇
- 安装最新版本的PHP或Apache中遇到的问题
- Dubbo消费端错误: ClassNotFoundException: org.apache.zookeeper.proto.WatcherEvent
- MQ消息队列系列(5)ActiviteMQ Getting Started Guide(apache官网翻译)
- apache-utf8中文问题
- apache 域名转发功能
- Apache常用配置项
- ClassNotFoundException:org.apache.commons.logging.LogFactory