您的位置:首页 > 运维架构 > Apache

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);
}

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: