NCC项目中Rest接口符合restful规范
从NC65开始,NC中除了支持SOAP协议的webservice接口外,还支持REST协议的接口,NC65的REST服务封装的是restlet
1、接口注册方法
在模块的META-INF文件夹下,增加.rest扩展名的接口文件,内容为xml格式,参考脚本如下
<?xml version="1.0" encoding='gb2312'?>
<module>
<rest>
<resource classname="nccloud.api.uapbd.commission.ongathering.MDMBaseResources" exinfo=""/>
<resource classname="nccloud.api.uapbd.commission.ongathering.OCCBaseResources" exinfo=""/>
</rest>
</module>
2、服务代码编写
注册文件中的nc.pubitf.dm.api.rest.DMResource需要放在开发环境的public(或者 client )源码目录下
package nccloud.api.uapbd.commission.ongathering;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import nc.pub.baseapp.rest.tool.MakeNcLog;
import org.json.JSONString;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.NCLocator;
import nc.itf.bd.cust.baseinfo.ICustBaseInfoQueryService;
import nc.itf.bd.material.assign.IMaterialAssignService;
import nc.itf.bd.supplier.assign.ISupplierAssignService;
import nc.itf.uap.IUAPQueryBS;
import nc.itf.uap.busibean.ISysInitQry;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.pub.occnc.syn.common.EventOperationEnum;
import nc.pub.occnc.syn.common.MessageTopics;
import nc.pub.occnc.syn.common.SynService;
import nc.pub.occnc.syn.utils.CollectionUtils;
import nc.pub.occnc.syn.utils.GsonUtil;
import nc.pub.occnc.syn.utils.SplitVOUtil;
import nc.pub.occnc.syn.utils.StringUtils;
import nc.pubitf.uapbd.IAddressPubService;
import nc.util.mmf.framework.base.MMArrayUtil;
import nc.vo.bd.address.AddressVO;
import nc.vo.bd.bankaccount.BankAccSubVO;
import nc.vo.bd.bankaccount.BankAccbasVO;
import nc.vo.bd.cust.CustbankVO;
import nc.vo.bd.cust.CustlinkmanVO;
import nc.vo.bd.cust.CustomerVO;
import nc.vo.bd.cust.custaddress.CustaddressVO;
import nc.vo.bd.cust.saleinfo.CustsaleVO;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.bd.errorlog.ErrLogReturnValue;
import nc.vo.bd.errorlog.ErrorMsgVO;
import nc.vo.bd.linkman.LinkmanVO;
import nc.vo.bd.material.MaterialVO;
import nc.vo.bd.material.marbasclass.MarBasClassVO;
import nc.vo.bd.material.stock.MaterialStockVO;
import nc.vo.bd.prodline.ProdLineVO;
import nc.vo.corg.CostRegionVO;
import nc.vo.occnc.customer.CustAddressMessageVO;
import nc.vo.occnc.customer.CustomerMessageVO;
import nc.vo.org.FinanceOrgVO;
import nc.vo.pf.pub.util.SQLUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.para.SysInitVO;
import nccloud.api.rest.utils.NCCRestUtils;
import nccloud.impl.platform.db.processor.InSqlBuilder;
import nccloud.ws.rest.resource.AbstractNCCRestResource;
import zhhf.CustomerMDMBase;
@Path("uapbd/mdm/mdmbase")
public class MDMBaseResources extends AbstractNCCRestResource {
private IMDMBaseService iMDMBaseService = null ;
private ICustBaseInfoQueryService iCustBaseInfoQueryService = null ;
private BaseDAO baseDao = new BaseDAO();
org.apache.log4j.Logger logzdy = MakeNcLog.setParam("uapbd", "mdmbase");//日志记录
/**
* @desc 中化化肥 客户同步回推结果
* @param json
* @return
*/
@POST
@Produces({ "application/json" })
@Consumes({ "application/json" })
@Path("synMDMInfo")
@SuppressWarnings("unchecked")
public JSONString synMDMInfo(JSONString json) {
}
}
如果需要传入head 参数可以参考:
package nc.api.controller.pingpai;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import com.google.common.base.Optional;
import nc.api.controller.base.UapBaseRestController;
import nc.api.vos.ProResultVo;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.server.ISecurityTokenCallback;
import nc.cmp.utils.Lists;
import nc.itf.bd.material.branddoc.IBrandDocManageServicer;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.BeanProcessor;
import nc.vo.bd.material.branddoc.BrandDocVO;
import nc.vo.bd.meta.BatchOperateVO;
import nc.vo.org.GroupVO;
import nc.vo.org.OrgVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.sm.UserVO;
@Path("/apibranddoc")
public class PingpaiController extends UapBaseRestController{
/***
* 新增
* @param positionclassVo
* @return
*/
@POST
@Path("/add")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ProResultVo postSeriesAdd(@HeaderParam("dbsource")String dbsource,PingpaiVo pingpaiVo) {
ProResultVo proResult = new ProResultVo();
BatchOperateVO atchVO = null; //对象
try {
NCLocator.getInstance().lookup(ISecurityTokenCallback.class).token("NCSystem".getBytes(),"pfxx".getBytes());
if(!Optional.fromNullable(pingpaiVo).isPresent()){
proResult = errorMsg("参数不是json 或为空!");
return proResult;
}
String pk_org = pingpaiVo.getPk_org();
if(StringUtils.isBlank(pk_org)) {
proResult = errorMsg("组织不能为空");
return proResult;
}
OrgVO orgVO = getOrgVos(pk_org);
if(!Optional.fromNullable(orgVO).isPresent()){
proResult = errorMsg("组织编码不存在");
return proResult;
}
String pk_group = pingpaiVo.getPk_group();
if(StringUtils.isBlank(pk_group)) {
proResult = errorMsg("集团不能为空");
return proResult;
}
GroupVO groupVO = getGroupVo(pk_group);
if(!Optional.fromNullable(groupVO).isPresent()){
proResult = errorMsg("集团编码不存在");
return proResult;
}
String creator = pingpaiVo.getCreator();//创建人
String enablestate = pingpaiVo.getEnablestate();
List<BrandDocVO> brandDocVOs = Lists.newArrayList();
BrandDocVO brandDocVO = new BrandDocVO();
brandDocVO.setCode(pingpaiVo.getCode());//编码
brandDocVO.setPk_org(orgVO.getPk_org());//组织
brandDocVO.setName(pingpaiVo.getName());//名称
brandDocVO.setPk_group(groupVO.getPk_group());//集团
brandDocVO.setCreationtime(new UFDateTime());//创建时间
brandDocVO.setStatus(VOStatus.NEW);//新增
if(StringUtils.isNotBlank(enablestate)&&StringUtils.isNumeric(enablestate)) {
brandDocVO.setEnablestate(Integer.parseInt(enablestate));
}else {
brandDocVO.setEnablestate(2);//启用
}
if(StringUtils.isNotBlank(creator)) {
UserVO userVO = getUserVOBByCode(creator);
if(!Optional.fromNullable(userVO).isPresent()){
proResult = errorMsg("创建人不存在");
return proResult;
}
InvocationInfoProxy.getInstance().setGroupId(groupVO.getPk_group());// 集团
InvocationInfoProxy.getInstance().setUserId(userVO.getCuserid());// 登陆人
brandDocVO.setCreator(userVO.getCuserid());
// defdocVO.setCreator(creator);
}
brandDocVOs.add(brandDocVO);
atchVO = new BatchOperateVO();
atchVO.setAddObjs(brandDocVOs.toArray(new BrandDocVO[0]));
BatchOperateVO savevo = NCLocator.getInstance().lookup(IBrandDocManageServicer.class).batchSaveBrandDoc(atchVO);
proResult = successMsg("添加成功!", savevo);
} catch (BusinessException e) {
proResult = errorMsg(e.getMessage());
return proResult;
}
return proResult;
}
/***
* 新增
* @param positionclassVo
* @return
*/
@POST
@Path("/update")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ProResultVo postSeriesUpdate(@HeaderParam("dbsource")String dbsource,PingpaiVo pingpaiVo) {
ProResultVo proResult = new ProResultVo();
BatchOperateVO atchVO = null; //对象
try {
NCLocator.getInstance().lookup(ISecurityTokenCallback.class).token("NCSystem".getBytes(),"pfxx".getBytes());
if(!Optional.fromNullable(pingpaiVo).isPresent()){
proResult = errorMsg("参数不是json 或为空!");
return proResult;
}
String pk_org = pingpaiVo.getPk_org();
if(StringUtils.isBlank(pk_org)) {
proResult = errorMsg("组织不能为空");
return proResult;
}
OrgVO orgVO = getOrgVos(pk_org);
if(!Optional.fromNullable(orgVO).isPresent()){
proResult = errorMsg("组织编码不存在");
return proResult;
}
String pk_group = pingpaiVo.getPk_group();
if(StringUtils.isBlank(pk_group)) {
proResult = errorMsg("集团不能为空");
return proResult;
}
GroupVO groupVO = getGroupVo(pk_group);
if(!Optional.fromNullable(groupVO).isPresent()){
proResult = errorMsg("集团编码不存在");
return proResult;
}
String modifier = pingpaiVo.getModifier();//最后修改人
String enablestate = pingpaiVo.getEnablestate();
String code = pingpaiVo.getCode();
String name = pingpaiVo.getName();
List<BrandDocVO> brandDocVOs = Lists.newArrayList();
BrandDocVO brandDocVO = getBrandDocByCode(code);
if(!Optional.fromNullable(brandDocVO).isPresent()){
proResult = errorMsg("品牌编码不存在,不能修改!");
return proResult;
}
if(StringUtils.isNotBlank(code)) {
brandDocVO.setCode(code);//编码
}
if(StringUtils.isNotBlank(name)) {
brandDocVO.setName(name);//名称
}
// brandDocVO.setPk_org(orgVO.getPk_org());//组织
// brandDocVO.setPk_group(groupVO.getPk_group());//集团
brandDocVO.setModifiedtime(new UFDateTime());
brandDocVO.setStatus(VOStatus.UPDATED);//更新
if(StringUtils.isNotBlank(enablestate)&&StringUtils.isNumeric(enablestate)) {
brandDocVO.setEnablestate(Integer.parseInt(enablestate));
}else {
brandDocVO.setEnablestate(2);//启用
}
if(StringUtils.isNotBlank(modifier)) {
UserVO userVO = getUserVOBByCode(modifier);
if(!Optional.fromNullable(userVO).isPresent()){
proResult = errorMsg("更新人不存在");
return proResult;
}
InvocationInfoProxy.getInstance().setGroupId(groupVO.getPk_group());// 集团
InvocationInfoProxy.getInstance().setUserId(userVO.getCuserid());// 登陆人
brandDocVO.setModifiedtime(new UFDateTime());
// defdocVO.setCreator(creator);
}
brandDocVOs.add(brandDocVO);
atchVO = new BatchOperateVO();
atchVO.setAddObjs(brandDocVOs.toArray(new BrandDocVO[0]));
BatchOperateVO savevo = NCLocator.getInstance().lookup(IBrandDocManageServicer.class).batchSaveBrandDoc(atchVO);
proResult = successMsg("添加成功!", savevo);
} catch (BusinessException e) {
proResult = errorMsg(e.getMessage());
return proResult;
}
return proResult;
}
/***
* getBrandDocByCode
* @param code
* @return
* @throws BusinessException
*/
private BrandDocVO getBrandDocByCode(String code) throws BusinessException {
SQLParameter brandDocParam = new SQLParameter();
StringBuffer codeSeriesSql = new StringBuffer();
codeSeriesSql.append("select t.* from bd_branddoc t where t.code = ? and t.dr = 0 order by t.ts desc ");
brandDocParam.addParam(code);
BrandDocVO brandDocVO = null;
@SuppressWarnings("unchecked")
List<BrandDocVO> brandDocVOs = (List<BrandDocVO>) getIUAPQueryBS().executeQuery(codeSeriesSql.toString(),brandDocParam, new BeanListProcessor(BrandDocVO.class));
if(!Optional.fromNullable(brandDocVOs).isPresent()&&CollectionUtils.isNotEmpty(brandDocVOs)){
brandDocVO = brandDocVOs.get(0);
}
return brandDocVO;
}
/***
* get org
* @return
* @throws BusinessException
*/
private OrgVO getOrgVos(String org_code) throws BusinessException {
OrgVO orgVO = null;
try {
SQLParameter orgSqlParam = new SQLParameter();
StringBuffer orgSql = new StringBuffer();
orgSql.append("select t.* from org_orgs t where t.code = ? and t.dr=0 ");
orgSqlParam.addParam(org_code);
orgVO = (OrgVO) getIUAPQueryBS().executeQuery(orgSql.toString(),orgSqlParam, new BeanProcessor(OrgVO.class));
} catch (Exception e) {
e.printStackTrace();
}
return orgVO;
}
/***
* get group
* @return
* @throws BusinessException
*/
private GroupVO getGroupVo(String group_code) throws BusinessException {
SQLParameter groupSqlParam = new SQLParameter();
StringBuffer groupSql = new StringBuffer();
groupSql.append("select t.* from org_group t where t.code = ? and t.dr=0 ");
groupSqlParam.addParam(group_code);
GroupVO groupVO = (GroupVO) getIUAPQueryBS().executeQuery(groupSql.toString(),groupSqlParam, new BeanProcessor(GroupVO.class));
return groupVO;
}
/***
* get user
* @return
* @throws BusinessException
*/
private UserVO getUserVOBByCode(String user_code) throws BusinessException {
SQLParameter userSqlParam = new SQLParameter();
StringBuffer userSql = new StringBuffer();
userSql.append(" select t.* from sm_user t where t.user_code = ? and t.dr=0 ");
userSqlParam.addParam(user_code);
UserVO userVO = (UserVO) getIUAPQueryBS().executeQuery(userSql.toString(),userSqlParam, new BeanProcessor(UserVO.class));
return userVO;
}
private static IUAPQueryBS queryBS = null;
/***
* get iuapquery
* @return
*/
private IUAPQueryBS getIUAPQueryBS() {
if(!Optional.fromNullable(queryBS).isPresent()){
queryBS = NCLocator.getInstance().lookup(IUAPQueryBS.class);
}
return queryBS;
}
}
3、部署和测试
将上面的代码部署到dm模块后,重启中间件,Postman测试时,测试URL设置为:http://ip:port/uapws/rest//hcr/positionclass/add,其中service/hello为代码中的类声明中的Path和方法中的Path,uapws/rest/为固定的NC中的REST服务访问路径
注意启动参数需要添加uapws
NC中可以通过/uapws/service查看SOAP协议的接口的部署情况,REST协议的实际也有一个查看REST接口的serverlet,地址为uapws/restmanage,如下图,通过这个地址可以查看已经部署的REST接口清单以及接口所属的模块: