ITKeyword,专注技术干货聚合推荐

注册 | 登录

maven+spring+cxf编写web service (2)

johnnywww 分享于

2020腾讯云10周年活动,优惠非常大!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1040

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
地址https://www.aliyun.com/minisite/goods

推荐:maven+spring+cxf编写web service

1.创建项目 [plain]  view plain copy   mvn archetype:generate -DarchetypeCatalog=Internal   选择19,创建web项目 2.生成eclipse项目,参见文章 3.修改web.x

先参见文章1学习如何创建web service,现在开始写些通用接口

1.通用web service接口

package com.test.iv.cs.service.webservice;

import com.test.iv.cs.model.common.BaseManagementListRetInfo;
import com.test.iv.cs.model.common.BaseManagementRetInfo;


public interface IGenericManagementWebService<T, RETT extends BaseManagementRetInfo<T>, LRETT extends BaseManagementListRetInfo<T>> {
	public RETT getInfoById( String id);

	public RETT addInfo(T info);

	public RETT updateInfo( T info);

	public RETT deleteInfoById( String id);

	public LRETT listAllInfos();
}

2.通用web service接口实现类

package com.test.iv.cs.service.webservice;

import com.test.iv.cs.model.common.BaseManagementListRetInfo;
import com.test.iv.cs.model.common.BaseManagementRetInfo;
import com.test.iv.cs.service.IGenericManagementService;

public class GenericManagementWebServiceImpl<T, RETT extends BaseManagementRetInfo<T>, LRETT extends BaseManagementListRetInfo<T>> implements IGenericManagementWebService<T, RETT, LRETT> {
	private IGenericManagementService<T, RETT, LRETT> service;

	public void setService(IGenericManagementService<T, RETT, LRETT> service) {
		this.service = service;
	}

	@Override
	public RETT getInfoById(String id) {
		return service.getInfoById(id);
	}

	@Override
	public RETT addInfo(T info) {
		return service.addInfo(info);
	}

	@Override
	public RETT updateInfo(T info) {
		return service.updateInfo(info);
	}

	@Override
	public RETT deleteInfoById(String id) {
		return service.deleteInfoById(id);
	}

	@Override
	public LRETT listAllInfos() {
		return service.listAllInfos();
	}
}

3.通用service接口

package com.test.iv.cs.service;

import com.test.iv.cs.model.common.BaseManagementListRetInfo;
import com.test.iv.cs.model.common.BaseManagementRetInfo;


public interface IGenericManagementService<T, RETT extends BaseManagementRetInfo<T>, LRETT extends BaseManagementListRetInfo<T>> {
	public RETT getInfoById(String id);

	public RETT addInfo(T info);

	public RETT updateInfo(T Info);

	public RETT deleteInfoById(String id);

	public LRETT listAllInfos();
}

4.通用service接口实现类

package com.test.iv.cs.service;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.test.iv.cs.model.common.BaseManagementListRetInfo;
import com.test.iv.cs.model.common.BaseManagementRetInfo;
import com.test.iv.cs.model.common.BaseRetInfo;
import com.test.iv.cs.orm.dao.IGenericDao;
import com.test.iv.cs.utils.AppCommon;
import com.test.iv.cs.utils.AppTools;

public abstract class GenericManagementServiceImpl<T, RETT extends BaseManagementRetInfo<T>, LRETT extends BaseManagementListRetInfo<T>, DAOPOJOT>
		implements IGenericManagementService<T, RETT, LRETT> {
	private static final Log gLog = LogFactory
			.getLog(GenericManagementServiceImpl.class);
	private String manageTypeInfo = "";
	private boolean addCheckId = false;

	protected Class<RETT> rettClass;
	protected Class<LRETT> listRettClass;
	protected Class<DAOPOJOT> daoEntityClass;
	protected IGenericDao<DAOPOJOT> daoObject;

	protected abstract T convertDAOInfoToInfo(DAOPOJOT daoT, BaseRetInfo rett);

	protected abstract boolean checkInfo(T info, BaseRetInfo rett);

	protected abstract String getId(T info, BaseRetInfo rett);

	protected abstract DAOPOJOT convertInfoToDAOInfo(T info, BaseRetInfo rett);

	private boolean checkValidInfo(T info, BaseRetInfo rett) {
		boolean result = false;
		if (null == info) {
			rett.setRetCode(AppCommon.RET_CODE_NULL_OBJECT);
			rett.setDesc("没有" + manageTypeInfo);
			return result;
		}
		if (!checkInfo(info, rett)) {
			return result;
		}
		result = true;
		return result;
	}

	private RETT createRetInstance() {
		RETT result = null;
		if (null == rettClass)
			return null;
		try {
			result = rettClass.newInstance();
			((BaseRetInfo) (result))
					.setRetCode(AppCommon.RET_CODE_ERROR_UNKNOWN);
		} catch (InstantiationException e) {
			gLog.error(e);
		} catch (IllegalAccessException e) {
			gLog.error(e);
		}
		return result;
	}

	private LRETT createListRetInstance() {
		LRETT result = null;
		if (null == listRettClass)
			return null;
		try {
			result = listRettClass.newInstance();
			((BaseRetInfo) (result))
					.setRetCode(AppCommon.RET_CODE_ERROR_UNKNOWN);
		} catch (InstantiationException e) {
			gLog.error(e);
		} catch (IllegalAccessException e) {
			gLog.error(e);
		}
		return result;
	}
	
	private DAOPOJOT getDAOObjectById(String id, BaseRetInfo rett) {
		DAOPOJOT result = daoObject.getById(id);
		if (null == result) {
			rett.setRetCode(AppCommon.RET_CODE_NULL_OBJECT);
			rett.setDesc("没有找到对应的" + manageTypeInfo + "信息");
			return null;
		}
		return result;
	}
	
	protected boolean checkIdValid(String id, BaseRetInfo retInfo) {
		if (AppTools.judgeEmptyStr(id)) {
			retInfo.setRetCode(AppCommon.RET_CODE_NULL_VALUE);
			retInfo.setDesc("没有" + manageTypeInfo + "编号");
			return false;
		}
		return true;
	}



	@Override
	public RETT getInfoById(String id) {
		RETT result = createRetInstance();
		if (null == result)
			return result;

		DAOPOJOT daoT = getDAOPOJOById(id, result);
		if (null == daoT)
			return result;

		T info = convertDAOInfoToInfo(daoT, result);
		if (null != info) {
			((BaseManagementRetInfo<T>) (result)).setInfo(info);
			result.setRetCode(AppCommon.RET_CODE_SUCCESS);
		}
		return result;
	}

	@Override
	public RETT addInfo(T info) {
		RETT result = createRetInstance();
		if (null == result)
			return result;
		if (!checkValidInfo(info, result))
			return result;
		if (addCheckId) {
			String id = getId(info, result);
			if (!checkIdValid(id, result)) {
				return result;
			}
			DAOPOJOT daoR = daoObject.getById(id);
			if (null != daoR) {
				result.setRetCode(AppCommon.RET_CODE_REPEATED);
				result.setDesc(manageTypeInfo + "编号重复");
				return result;
			}
		}
		DAOPOJOT daoT = convertInfoToDAOInfo(info, result);
		daoObject.save(daoT);
		result.setInfo(convertDAOInfoToInfo(daoT, result));
		result.setRetCode(AppCommon.RET_CODE_SUCCESS);
		return result;
	}

	@Override
	public RETT updateInfo(T info) {
		RETT result = createRetInstance();
		if (null == result)
			return result;
		if (!checkValidInfo(info, result))
			return result;
		String id = getId(info, result);
		DAOPOJOT daoT = getDAOPOJOById(id, result);
		if (null == daoT)
			return result;

		DAOPOJOT daoS = convertInfoToDAOInfo(info, result);
		daoObject.saveOrUpdate(daoS);
		result.setInfo(info);
		result.setRetCode(AppCommon.RET_CODE_SUCCESS);
		return result;
	}

	private DAOPOJOT getDAOPOJOById(String id, BaseRetInfo rett) {
		DAOPOJOT result = null;
		if (!checkIdValid(id, rett)) {
			return result;
		}
		result = getDAOObjectById(id, rett);
		return result;
	}
	@Override
	public RETT deleteInfoById(String id) {
		RETT result = createRetInstance();
		if (null == result)
			return result;
		DAOPOJOT daoT = getDAOPOJOById(id, result);
		if (null == daoT)
			return result;
		daoObject.delete(daoT);
		result.setInfo(convertDAOInfoToInfo(daoT, result));
		result.setRetCode(AppCommon.RET_CODE_SUCCESS);	
		return result;
	}

	@Override
	public LRETT listAllInfos() {
		LRETT result = createListRetInstance();
		if (null == result)
			return result;
		Collection<T> list = new ArrayList<T>();
		result.setInfos(list);
		Collection<DAOPOJOT> dList = null;
		try {
			dList = daoObject.findAll(daoEntityClass);
		} catch (Exception ex) {
			result.setRetCode(AppCommon.RET_CODE_LIST_ERROR);
			result.setDesc(ex.getMessage());
			return result;
		}
		if (null != dList) {
			for (DAOPOJOT daoT : dList) {
				list.add(convertDAOInfoToInfo(daoT, result));
			}
		}
		result.setRetCode(AppCommon.RET_CODE_SUCCESS);
		return result;
	}

	public GenericManagementServiceImpl() {
		rettClass = AppTools.getParamClass(getClass(), 1);
		listRettClass = AppTools.getParamClass(getClass(), 2);
		daoEntityClass = AppTools.getParamClass(getClass(), 3);
	}

	public void setDaoObject(IGenericDao<DAOPOJOT> daoObject) {
		this.daoObject = daoObject;
	}

	public void setManageTypeInfo(String manageTypeInfo) {
		this.manageTypeInfo = manageTypeInfo;
	}

	public void setAddCheckId(boolean addCheckId) {
		this.addCheckId = addCheckId;
	}

	public String getManageTypeInfo() {
		return manageTypeInfo;
	}

}



5.通用DAO接口

package com.test.iv.cs.orm.dao;

import java.io.Serializable;
import java.util.Collection;

public interface IGenericDao<T> {
	public void save(Object o);// 保存

	void delete(Object o);// 删除修改

	void deleteById(Serializable id);

	public void update(Object o);//

	public void saveOrUpdate(Object o);

	public T getById(Serializable id);// 查询

	public Collection<T> findAll(Class<T> entityClass);

	public Collection<T> findByExample(Object o);
}

6.通用DAO接口实现类

package com.test.iv.cs.orm.dao.hibernate;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.util.Assert;

import com.test.iv.cs.orm.dao.IGenericDao;

@SuppressWarnings("unchecked")
public class GenericDaoHibernateImpl<T> extends HibernateGenericDao implements
		IGenericDao<T> {

	protected Class<T> entityClass;

	public GenericDaoHibernateImpl() {
		entityClass = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
	}

	protected Class<T> getEntityClass() {
		return entityClass;
	}

	@Override
	public Collection<T> findAll(Class<T> entityClass) {
		return getHibernateTemplate().loadAll(entityClass);
	}

	public Collection<T> findAll(Class<T> entityClass, String orderBy,
			boolean isAsc) {
		Assert.hasText(orderBy);
		if (isAsc)
			return getHibernateTemplate().findByCriteria(
					DetachedCriteria.forClass(entityClass).addOrder(
							Order.asc(orderBy)));
		else
			return getHibernateTemplate().findByCriteria(
					DetachedCriteria.forClass(entityClass).addOrder(
							Order.desc(orderBy)));
	}

	@Override
	public Collection<T> findByExample(Object o) {
		return getHibernateTemplate().findByExample(o);
	}

	@Override
	public T getById(Serializable id) {
		return (T) getHibernateTemplate().get(entityClass, id);
	}

	@Override
	public void deleteById(Serializable id) {
		delete(getById(id));
	}

	@Override
	public void delete(Object o) {
		getHibernateTemplate().delete(o);
	}

	@Override
	public void save(Object o) {
		getHibernateTemplate().save(o);
	}

	@Override
	public void saveOrUpdate(Object o) {
		getHibernateTemplate().saveOrUpdate(o);
	}

	@Override
	public void update(Object o) {
		getHibernateTemplate().update(o);
	}

}

7.通用数据对象

package com.test.iv.cs.model.common;

public class BaseManagementRetInfo<T> extends BaseRetInfo {
	protected T info;

	public T getInfo() {
		return info;
	}

	public void setInfo(T info) {
		this.info = info;
	}
	
}

package com.test.iv.cs.model.common;

import java.util.ArrayList;
import java.util.Collection;

public class BaseManagementListRetInfo<T> extends BaseRetInfo {
	protected Collection<T> infos = new ArrayList<T>();

	public Collection<T> getInfos() {
		return infos;
	}

	public void setInfos(Collection<T> infos) {
		this.infos = infos;
	}
}
package com.test.iv.cs.model.common;

public class BaseRetInfo {
	private int retCode;
	private String desc;

	public int getRetCode() {
		return retCode;
	}

	public void setRetCode(int retCode) {
		this.retCode = retCode;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	public void setRetInfo(BaseRetInfo retInfo) {
		this.retCode = retInfo.retCode;
		this.desc = retInfo.desc;
	}
}


具体实现详细实现

8.数据对象

package com.test.iv.cs.model.devicemanufacturer;

public class DeviceManufacturerInfo {
	private String deviceManufacturerId;

	public String getDeviceManufacturerId() {
		return deviceManufacturerId;
	}

	public void setDeviceManufacturerId(String deviceManufacturerId) {
		this.deviceManufacturerId = deviceManufacturerId;
	}

	public String getDescript() {
		return descript;
	}

	public void setDescript(String descript) {
		this.descript = descript;
	}

	private String descript;
}

package com.test.iv.cs.model.devicemanufacturer;

import java.util.Collection;

import com.test.iv.cs.model.common.BaseManagementListRetInfo;

public class DeviceManufacturerListRetInfo extends BaseManagementListRetInfo<DeviceManufacturerInfo> {
	public Collection<DeviceManufacturerInfo> getDeviceManufacturerInfos() {
		return infos;
	}

	public void setDeviceManufacturerInfos(Collection<DeviceManufacturerInfo> infos) {
		this.infos = infos;
	}
}

必须添加那两个函数,否则cxf产生的wsdl不能被客户端用

package com.test.iv.cs.model.devicemanufacturer;

import com.test.iv.cs.model.common.BaseManagementRetInfo;

public class DeviceManufacturerRetInfo extends
		BaseManagementRetInfo<DeviceManufacturerInfo> {
	public DeviceManufacturerInfo getDeviceManufacturer() {
		return info;
	}

	public void setDeviceManufacturer(DeviceManufacturerInfo info) {
		this.info = info;
	}
}
必须添加那两个函数,否则cxf产生的wsdl不能被客户端用

推荐:使用apache CXF和maven开发Web Service

目前主要的java webservice框架剩下了axis2和cxf。本文对两个框架的目标、标准支持、开发和部署等方面进行了简单的对比。对于在现有web应用中发布webservice,本

9.Web Service接口

package com.test.iv.cs.service.webservice.devicemanufacture;

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

import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerInfo;
import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerListRetInfo;
import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerRetInfo;

@WebService
public interface DeviceManufactureManagementWebService  {
	@WebMethod(operationName = "getInfoById")
	@WebResult(name = "result")
	public DeviceManufacturerRetInfo getInfoById(@WebParam(name = "id") String id);
	
	@WebMethod(operationName = "addInfo")
	@WebResult(name = "result")
	public DeviceManufacturerRetInfo addInfo(@WebParam(name = "info")DeviceManufacturerInfo info);
	
	@WebMethod(operationName = "updateInfo")
	@WebResult(name = "result")
	public DeviceManufacturerRetInfo updateInfo(@WebParam(name = "info")DeviceManufacturerInfo info);
	
	@WebMethod(operationName = "deleteInfoById")
	@WebResult(name = "result")
	public DeviceManufacturerRetInfo deleteInfoById(@WebParam(name = "id") String id);
	
	@WebMethod(operationName = "listAllInfos")
	@WebResult(name = "result")
	public DeviceManufacturerListRetInfo listAllInfos();	
}

必须这样写,否则cxf无法创建有效的wsdl

10.Web Service接口实现类

package com.test.iv.cs.service.webservice.devicemanufacture;

import javax.jws.WebService;

import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerInfo;
import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerListRetInfo;
import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerRetInfo;
import com.test.iv.cs.service.webservice.GenericManagementWebServiceImpl;

@WebService(endpointInterface = "com.test.iv.cs.service.webservice.devicemanufacture.DeviceManufactureManagementWebService")
public class DeviceManufactureManagementWebServiceCXFImpl
		extends
		GenericManagementWebServiceImpl<DeviceManufacturerInfo, DeviceManufacturerRetInfo, DeviceManufacturerListRetInfo>
		implements DeviceManufactureManagementWebService {
	
}

11.web service接口设置

	<bean id="deviceManufactureManagementWebServiceCXFImpl"
		class="com.test.iv.cs.service.webservice.devicemanufacture.DeviceManufactureManagementWebServiceCXFImpl">
		<property name="service" ref="deviceManufactureManagementService"></property>
	</bean>
	<jaxws:endpoint id="deviceManufactureManagementWebService" implementor="#deviceManufactureManagementWebServiceCXFImpl"
		address="/deviceManufactureManagementWebService"></jaxws:endpoint>		
12.Service 接口

package com.test.iv.cs.service.devicemanufacture;

import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerInfo;
import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerListRetInfo;
import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerRetInfo;
import com.test.iv.cs.service.IGenericManagementService;

public interface IDeviceManufactureManagementService extends
		IGenericManagementService<DeviceManufacturerInfo, DeviceManufacturerRetInfo, DeviceManufacturerListRetInfo> {

}

13.Service接口实现类

package com.test.iv.cs.service.devicemanufacture;

import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerInfo;
import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerListRetInfo;
import com.test.iv.cs.model.devicemanufacturer.DeviceManufacturerRetInfo;
import com.test.iv.cs.orm.pojo.Devicemanufacturer;
import com.test.iv.cs.service.GenericManagementServiceImpl;
import com.test.iv.cs.utils.AppTools;

public class DeviceManufactureManagementServiceImpl extends
		GenericManagementServiceImpl<DeviceManufacturerInfo, DeviceManufacturerRetInfo, DeviceManufacturerListRetInfo, Devicemanufacturer> implements
		IDeviceManufactureManagementService {

	@Override
	protected DeviceManufacturerInfo convertDAOInfoToInfo(
			Devicemanufacturer daoT, DeviceManufacturerRetInfo rett) {		
		DeviceManufacturerInfo result = new DeviceManufacturerInfo();
		result.setDeviceManufacturerId(AppTools.convertNullStr(daoT.getDevicemanufacturerId()));
		result.setDescript(AppTools.convertNullStr(daoT.getDescript()));
		return result;
	}



}

14.Service接口设置
	<bean id="deviceManufactureManagementServiceImpl"
		class="com.test.iv.cs.service.devicemanufacture.DeviceManufactureManagementServiceImpl">
		<property name="daoObject" ref="deviceManufactureDAO"></property>
	</bean>
	<alias name="deviceManufactureManagementServiceImpl" alias="deviceManufactureManagementService" />

15.DAO接口

package com.test.iv.cs.orm.dao.devicemanufacture;

import com.test.iv.cs.orm.dao.IGenericDao;
import com.test.iv.cs.orm.pojo.Devicemanufacturer;

public interface IDeviceManufactureDAO extends IGenericDao<Devicemanufacturer> {

}

16.DAO接口实现类

package com.test.iv.cs.orm.dao.hibernate.devicemanufacture;

import com.test.iv.cs.orm.dao.devicemanufacture.IDeviceManufactureDAO;
import com.test.iv.cs.orm.dao.hibernate.GenericDaoHibernateImpl;
import com.test.iv.cs.orm.pojo.Devicemanufacturer;

public class DeviceManufactureDAOHibernateImpl extends
		GenericDaoHibernateImpl<Devicemanufacturer> implements IDeviceManufactureDAO {


}

17.DAO设置

	<bean abstract="true" id="genericDaoHibernateImpl" class="com.test.iv.cs.orm.dao.hibernate.GenericDaoHibernateImpl">
			<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<bean id="deviceManufactureDAOHibernateImpl" class="com.test.iv.cs.orm.dao.hibernate.devicemanufacture.DeviceManufactureDAOHibernateImpl" parent="genericDaoHibernateImpl">
	</bean>	
	<alias name="deviceManufactureDAOHibernateImpl" alias="deviceManufactureDAO"/>

18.公用函数

package com.test.ivs.cs.utils;

import java.lang.reflect.ParameterizedType;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AppTools {
	private static final Log log = LogFactory.getLog(AppTools.class);

	public static String formatDateTimeStr(Date date, String pattern) {
		if (null == date)
			return "";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	public static Date parseDateTime(String value, String pattern) {
		Date result = null;
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		try {
			result = format.parse(value);
		} catch (ParseException e) {
			log.error(e);
		}
		return result;
	}

	public static boolean judgeEmptyStr(String value) {
		return ((null == value) || (value.length() < 1));
	}

	public static String convertNullStr(String value) {
		if (null == value)
			return "";
		else
			return value;
					
	}
	
	@SuppressWarnings("unchecked")
	public static <T>  Class<T> getParamClass(Class<?> cls, int index) {
		return  (Class<T>) (((ParameterizedType) cls.getGenericSuperclass()).getActualTypeArguments()[index]); 
	}
}



推荐:基于Maven在Spring中集成CXF Web Service框架

引言: 在跨系统和跨平台的系统通信中,WebService是一个事实上的标准,其以平台无关性,获得了广泛的应用。本文将讲述如何基于Spring来集成CXF,并开发出第一个

先参见文章1学习如何创建web service,现在开始写些通用接口 1.通用web service接口 package com.test.iv.cs.service.webservice;import com.test.iv.cs.model.common.BaseManagementListRetI

相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。