一,引入jar包 pom.xml加入如下配置
<!-- hanzl axis2 jar -->
<dependency>
<groupId>fakepath</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axiom-impl</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.ksoap2</groupId>
<artifactId>ksoap2-android-assembly</artifactId>
<version>3.2.0</version>
<classifier>dependencies</classifier>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axiom-impl</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.ksoap2</groupId>
<artifactId>ksoap2-android-assembly</artifactId>
<version>3.2.0</version>
<classifier>dependencies</classifier>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.4.7</version>
</dependency>
<!-- cxf开始 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.11</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.11</version>
</dependency>
<!-- cxf结束 -->
<!-- 结束 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.11</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.11</version>
</dependency>
<!-- cxf结束 -->
<!-- 结束 -->
二.在spring.xml 配置发布地址
首先在spring.xml中引入cxf的xml约束文件
<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" />
其次配置webservice的服务端的发布地址
配置如下:
<jaxws:endpoint id="userCodeDataShareService" address="http://localhost:8088/services/userCodeDataShareService">
<jaxws:implementor>
<bean
class="com.hyjx.business.user_codeDataservice.UserCodeDataShareServiceImpl">
<property name="service">
<bean class="com.hyjx.business.user_codeDataservice.UserinfoandCodeServiceImpl" >
<property name="dao" >
<ref bean="baseDao" />
</property>
</bean>
</property>
</bean>
</jaxws:implementor>
</jaxws:endpoint>
<jaxws:implementor>
<bean
class="com.hyjx.business.user_codeDataservice.UserCodeDataShareServiceImpl">
<property name="service">
<bean class="com.hyjx.business.user_codeDataservice.UserinfoandCodeServiceImpl" >
<property name="dao" >
<ref bean="baseDao" />
</property>
</bean>
</property>
</bean>
</jaxws:implementor>
</jaxws:endpoint>
解释: address就是服务端的一个发布地址 端口号为8088
实现的类是 com.hyjx.business.user_codeDataservice.UserCodeDataShareServiceImpl
datacollecservice 是我的webservice方法中用到的接口方法
dao 是调取数据数据的类
三,服务端实例代码
package com.hyjx.business.user_codeDataservice;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import com.hyjx.business.ybccjdepartsetting.YbccjDepartsettingModel;
import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.orm.dao.IBaseDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class UserCodeDataShareServiceImpl implements UserCodeDataShareService {
@Resource
private UserinfoandCodeService service;
@Resource
private IBaseDao dao;
public UserinfoandCodeService getService() {
return service;
}
public void setService(UserinfoandCodeService service) {
this.service = service;
}
public IBaseDao getDao() {
return dao;
}
public void setDao(IBaseDao dao) {
this.dao = dao;
}
/**检查传入参数是否合法*/
public boolean checkQxdm(String qxdm){
boolean b=true;
if(qxdm==null||"".equals(qxdm)){
b=false;
}
return b;
}
/**返回Json数据*/
public String returnJson(String total,List list,String rescode,String resmessage){
StringBuffer str=new StringBuffer();
str.append("{");
str.append("\"total\":"+list.size()+",");
str.append("\"rescode\":"+rescode+",");
str.append("\"resmessage\":"+"\""+resmessage+"\"");
if(list!=null&&list.size()>0){
String listJson=CommonUtil.getJsonString(list);
str.append(",\"rows\":"+listJson);
}
str.append("}");
return str.toString();
}
@WebMethod
public String usershareData(String qxdm) {
//String jsonStr ="[{\"id\":\"3\",\"name\":\"bob\",\"pass\":\"123\"},{\"id\":\"4\",\"name\":\"lancy\",\"pass\":\"134\"}]";
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<UserinfoModel> list=service.queryUserByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String codeshareData() {
List<Codedata> list=service.queryCodedata();
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String entinfoshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<EntinfoModel> list=service.queryEntinfoByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String recheckSettingshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<RechecksettingModel> list=service.queryRechecksettingByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String druginfoshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<DruginfoModel> list=service.queryDruginfoByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String instrumentinfoshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<InstrumentinfoModel> list=service.queryInstrumentinfoByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String cosmeticinfoshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<CosmeticinfoModel> list=service.queryCosmeticinfoByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String serialnumberShara(String qxdm,String bhlx,String num) {
int number=Integer.parseInt(num);
if(!"".equals(checkSerialPara(qxdm,bhlx,number))){
return checkSerialPara(qxdm,bhlx,number);
}
SerialnumberModel model=new SerialnumberModel();
Date year=new Date();
//格式化日期为年
SimpleDateFormat format=new SimpleDateFormat("YYYY");
String nf=format.format(year);
model.setQxdm(qxdm);
model.setBhlx(bhlx);
model.setNf(nf);
SerialnumberModel temp=new SerialnumberModel();
//查询编号最大的对象
temp=service.queryMaxSerialnumber(model);
//拼接返回的数据连续的编号
StringBuffer sb=new StringBuffer();
//插入数据
if(temp==null){
model.setId(CommonUtil.getUUID32());
model.setBh(0+number);
service.addSerialnumberModel(model);
for(int i=1;i<=number;i++){
if(i==number){
sb.append(i);
}else{
sb.append(i+",");
}
}
}else{//更新并返回编号
model.setBh(temp.getBh()+number);
model.setId(temp.getId());
service.updateSerialnumberModel(model);
for(int i=temp.getBh()+1;i<=temp.getBh()+number;i++){
if(i==temp.getBh()+number){
sb.append(i);
}else{
sb.append(i+",");
}
}
}
return sb.toString();
}
//检查闯入参数是否合法
public String checkSerialPara(String qxdm,String bhlx,int num){
String str="";
if(qxdm==null||"".equals(qxdm)){
str= "传入的区县代码不能为空!";
}
if(bhlx==null||"".equals(bhlx)){
str= "传入的编号类型不能为空!";
}
if(num<0){
str= "传入的序号不能小于零!";
}
return str;
}
@Override
public String departsettingshareData() {
// TODO Auto-generated method stub
List<YbccjDepartsettingModel> list=service.queryDepartSet();
return returnJson("1",list,"1","成功返回数据");
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import com.hyjx.business.ybccjdepartsetting.YbccjDepartsettingModel;
import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.orm.dao.IBaseDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class UserCodeDataShareServiceImpl implements UserCodeDataShareService {
@Resource
private UserinfoandCodeService service;
@Resource
private IBaseDao dao;
public UserinfoandCodeService getService() {
return service;
}
public void setService(UserinfoandCodeService service) {
this.service = service;
}
public IBaseDao getDao() {
return dao;
}
public void setDao(IBaseDao dao) {
this.dao = dao;
}
/**检查传入参数是否合法*/
public boolean checkQxdm(String qxdm){
boolean b=true;
if(qxdm==null||"".equals(qxdm)){
b=false;
}
return b;
}
/**返回Json数据*/
public String returnJson(String total,List list,String rescode,String resmessage){
StringBuffer str=new StringBuffer();
str.append("{");
str.append("\"total\":"+list.size()+",");
str.append("\"rescode\":"+rescode+",");
str.append("\"resmessage\":"+"\""+resmessage+"\"");
if(list!=null&&list.size()>0){
String listJson=CommonUtil.getJsonString(list);
str.append(",\"rows\":"+listJson);
}
str.append("}");
return str.toString();
}
@WebMethod
public String usershareData(String qxdm) {
//String jsonStr ="[{\"id\":\"3\",\"name\":\"bob\",\"pass\":\"123\"},{\"id\":\"4\",\"name\":\"lancy\",\"pass\":\"134\"}]";
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<UserinfoModel> list=service.queryUserByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String codeshareData() {
List<Codedata> list=service.queryCodedata();
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String entinfoshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<EntinfoModel> list=service.queryEntinfoByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String recheckSettingshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<RechecksettingModel> list=service.queryRechecksettingByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String druginfoshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<DruginfoModel> list=service.queryDruginfoByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String instrumentinfoshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<InstrumentinfoModel> list=service.queryInstrumentinfoByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String cosmeticinfoshareData(String qxdm) {
if(!checkQxdm(qxdm)) return returnJson("0",null,"0","传入区县代码不能为空");
List<CosmeticinfoModel> list=service.queryCosmeticinfoByqxdm(qxdm);
return returnJson("1",list,"1","成功返回数据");
}
@WebMethod
public String serialnumberShara(String qxdm,String bhlx,String num) {
int number=Integer.parseInt(num);
if(!"".equals(checkSerialPara(qxdm,bhlx,number))){
return checkSerialPara(qxdm,bhlx,number);
}
SerialnumberModel model=new SerialnumberModel();
Date year=new Date();
//格式化日期为年
SimpleDateFormat format=new SimpleDateFormat("YYYY");
String nf=format.format(year);
model.setQxdm(qxdm);
model.setBhlx(bhlx);
model.setNf(nf);
SerialnumberModel temp=new SerialnumberModel();
//查询编号最大的对象
temp=service.queryMaxSerialnumber(model);
//拼接返回的数据连续的编号
StringBuffer sb=new StringBuffer();
//插入数据
if(temp==null){
model.setId(CommonUtil.getUUID32());
model.setBh(0+number);
service.addSerialnumberModel(model);
for(int i=1;i<=number;i++){
if(i==number){
sb.append(i);
}else{
sb.append(i+",");
}
}
}else{//更新并返回编号
model.setBh(temp.getBh()+number);
model.setId(temp.getId());
service.updateSerialnumberModel(model);
for(int i=temp.getBh()+1;i<=temp.getBh()+number;i++){
if(i==temp.getBh()+number){
sb.append(i);
}else{
sb.append(i+",");
}
}
}
return sb.toString();
}
//检查闯入参数是否合法
public String checkSerialPara(String qxdm,String bhlx,int num){
String str="";
if(qxdm==null||"".equals(qxdm)){
str= "传入的区县代码不能为空!";
}
if(bhlx==null||"".equals(bhlx)){
str= "传入的编号类型不能为空!";
}
if(num<0){
str= "传入的序号不能小于零!";
}
return str;
}
@Override
public String departsettingshareData() {
// TODO Auto-generated method stub
List<YbccjDepartsettingModel> list=service.queryDepartSet();
return returnJson("1",list,"1","成功返回数据");
}
}
检查是否发布成功:访问http://localhost:8088/services/userCodeDataShareService?wsdl 看能都查看wsdl文档
四,客户端代码 采用axis2调用(部分代码)
String url=uploadurl;
String jsonList=net.sf.json.JSONArray.fromObject(listCosmeticinUpload).toString();
Object[] obj = new Object[]{jsonList};
String namespace="http://infoDataCollect.business.hyjx.com/";
String methodName="cosmeticinfoCollect";
String result=returSult(url, obj, namespace, methodName);
Object[] obj = new Object[]{jsonList};
String namespace="http://infoDataCollect.business.hyjx.com/";
String methodName="cosmeticinfoCollect";
String result=returSult(url, obj, namespace, methodName);
public String returSult(String url,Object[] obj,String namespace,String methodName) {
String result="";
RPCServiceClient client=null;
try {
client = new RPCServiceClient();
Options options = client.getOptions();
//options.setTimeOutInMilliSeconds(1000*60*5);
EndpointReference end = new EndpointReference(url);
Class<?>[] classes = new Class[] { String.class};
options.setTo(end);
options.setManageSession(true);
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);
options.setProperty(org.apache.axis2.Constants.Configuration.DISABLE_SOAP_ACTION,true);
QName qname = new QName(namespace, methodName);
result = (String)client.invokeBlocking(qname, obj,classes)[0];
client.cleanupTransport();
} catch (Exception e) {
result="连接服务器失败";
}finally{
if(client != null){
try {
//client.cleanup();
client.cleanupTransport();
} catch (AxisFault e) {
e.printStackTrace();
result="连接服务器失败";
}
}
}
return result;
}
String result="";
RPCServiceClient client=null;
try {
client = new RPCServiceClient();
Options options = client.getOptions();
//options.setTimeOutInMilliSeconds(1000*60*5);
EndpointReference end = new EndpointReference(url);
Class<?>[] classes = new Class[] { String.class};
options.setTo(end);
options.setManageSession(true);
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);
options.setProperty(org.apache.axis2.Constants.Configuration.DISABLE_SOAP_ACTION,true);
QName qname = new QName(namespace, methodName);
result = (String)client.invokeBlocking(qname, obj,classes)[0];
client.cleanupTransport();
} catch (Exception e) {
result="连接服务器失败";
}finally{
if(client != null){
try {
//client.cleanup();
client.cleanupTransport();
} catch (AxisFault e) {
e.printStackTrace();
result="连接服务器失败";
}
}
}
return result;
}
解释:url:服务端wsdl地址 例如
http://localhost:8088/services/userCodeDataShareService?wsdl
obj :参数数组
namespace:命名空间(这个可以在访问http://localhost:8088/services/userCodeDataShareService?wsdl在文档里看到)
methodName:方法名字