通过调用webservice接口新增保存一张单子

文章描述了如何在Java环境中通过创建接口和实现类来构建Webservice,包括编写接口、实现类,生成WSDL和XSD文件,配置服务,处理JSON数据,以及进行数据库交互。还涉及了错误处理和数据转换的方法。
摘要由CSDN通过智能技术生成

通过调用webservice接口新增保存一张单子

1.在public端的nc.itf下新建一个名叫ws的包,在包下建一个名为ICkdWebService.java的接口,在接口里面写一个接口方法

package nc.itf.ws;
import nc.vo.pub.BusinessException;

public interface ICkdWebService {

	public String reqData(String jsonInfo) throws BusinessException;
	
}

2.在private端的nc.impl下新建一个名叫ws的包,在包下建一个名为CkdWebServiceImpl.java的接口实现类,实现接口中的方法

package nc.impl.ws;

public class CkdWebServiceImpl implements ICkdWebService{

	@Override
	public String reqData(String jsonInfo) {
		return null;
	}
}

3.右击接口–>WS Tools–>WSDL,右击接口实现类–>WS Tools–>XSD;这样会在根目录下生成一个ws的文件夹,把生成文件夹中的ICkdWebService.wsdl放到跟接口同一个文件夹下,CkdWebServiceImpl.xsd放到跟接口实现类同一个文件夹下;删掉刚刚生成的ws文件夹
4.运行–>调试配置–>yhly_Server–>自变量,的最后加上,uapws,点击应用,然后关闭
5.在最下面的yhlypx文件夹的META-INF文件夹下新建一个wepservice.upm文件

<?xml version='1.0' encoding='UTF-8'?>
<module name="yhlypx">//模块名
	<public>
		 <component remote="true" singleton="true" tx="CMT">
			<interface>nc.itf.ws.ICkdWebService</interface>//接口路径
			<implementation>nc.impl.ws.CkdWebServiceImpl</implementation>//实现类路径
            <extension class="nc.uap.ws.deploy.OxbWSExtensionProcessor">
				<wsdl>nc/itf/ws/ICkdWebService.wsdl</wsdl>//wsdl路径的后半段
				<address>/ICkdWebService</address>//接口名
			</extension>
        </component>
	</public>
	<private>
	</private>
</module>

6.启动服务,清缓存
7.json格式

{
	"head": {
		"personnel": "002",
		"department": "002",
		"category": "项目出库申请",
		"flag": "1"	
	},
	"body": [{
		"customer": "测试公司002",
		"num": "1.00000000",
		"taxprice": "3.00000000",
		"taxrate": "5.00000000"

	}, {
		"customer": "用户名称",
		"num": "2.00000000",
		"taxprice": "4.00000000",
		"taxrate": "5.00000000"
	}]
}

8.在public端的nc.vo下新建一个名叫ws的包,在包里新建一个CksqdJson.java类,新建一个CksqdHeadJsonVO.java类,新建一个CksqdBodyJsonVO.java类

CksqdJson.java

package nc.vo.ws;

import java.io.Serializable;

public class CksqdJson implements Cloneable,Serializable{

	private static final long serialVersionUID = 1L;
	
	public CksqdHeadJsonVO head;
	public CksqdBodyJsonVO[] body;
	
	public CksqdHeadJsonVO getHead() {
		return head;
	}
	public void setHead(CksqdHeadJsonVO head) {
		this.head = head;
	}
	public CksqdBodyJsonVO[] getBody() {
		return body;
	}
	public void setBody(CksqdBodyJsonVO[] body) {
		this.body = body;
	}
	
}

CksqdHeadJsonVO.java
只传能编辑的,自定义参照穿过来的一般都是编码或名称,需要将其转换成主键(因为数据库自定义参照数据库里存的是主键)

package nc.vo.ws;

public class CksqdHeadJsonVO {

	public String personnel;
	public String department;
	public String category;
	public String flag;
	public String getPersonnel() {
		return personnel;
	}
	public void setPersonnel(String personnel) {
		this.personnel = personnel;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	
}

CksqdBodyJsonVO.java
只传能编辑的,自定义参照穿过来的一般都是编码或名称,需要将其转换成主键(因为数据库自定义参照数据库里存的是主键)

package nc.vo.ws;

import java.math.BigDecimal;

public class CksqdBodyJsonVO {

	public String customer;
	public BigDecimal num;
	public BigDecimal taxprice;
	public BigDecimal taxrate;
	public String getCustomer() {
		return customer;
	}
	public void setCustomer(String customer) {
		this.customer = customer;
	}
	public BigDecimal getNum() {
		return num;
	}
	public void setNum(BigDecimal num) {
		this.num = num;
	}
	public BigDecimal getTaxprice() {
		return taxprice;
	}
	public void setTaxprice(BigDecimal taxprice) {
		this.taxprice = taxprice;
	}
	public BigDecimal getTaxrate() {
		return taxrate;
	}
	public void setTaxrate(BigDecimal taxrate) {
		this.taxrate = taxrate;
	}
	
}


9.打开浏览器,输入http://127.0.0.1:[端口号]/uapws检查接口是否发布
用户名、密码在D:\peixun\lianxi\LXHome\LXHome\hotwebs\uapws\WEB-INF文件下的config.xml下

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <action-mappings >
    <action path="/getAllServices.ajax" type="nc.uap.ws.console.action.GetServicesAction" />
  	<action path="/login.ajax" type="nc.uap.ws.console.action.LoginAction" />
  	
  	<action path="/getBasicInfo.ajax" type="nc.uap.ws.console.action.GetBasicInfoAction" />
  	<action path="/getWssInfo.ajax" type="nc.uap.ws.console.action.GetWssInfoAction" />
  	<action path="/getKSInfo.ajax" type="nc.uap.ws.console.action.GetKSInfoAction" />
  	
  	<action path="/saveDoc.ajax" type="nc.uap.ws.console.action.SaveDocAction" />
  	<action path="/loadDoc.ajax" type="nc.uap.ws.console.action.LoadDocAction" />
  	
  	<action path="/loadReqTemplete.ajax" type="nc.uap.ws.console.action.GenSoapRequestAction" />
  	<action path="/soapRequest.ajax" type="nc.uap.ws.console.action.SoapRequestAction" />
  	<action path="/soapFormat.ajax" type="nc.uap.ws.console.action.SoapFormatAction" />
  </action-mappings>
  <login name = "administrator" password="ufsoft*12345"/>//用户名和密码在这里
</config>

10.检验json语句是否正确的网址

https://www.bejson.com/

11.字符串转化为json格式

12.登陆之后,再在网址后加上service,找到自己的接口,复制地址,打开测试软件,点击SOAP,然后把地址填进去

13.自定义参照的翻译公式

vo里要翻译的字段名->getcolvalue(表名,要翻译的值,code,vo里要翻译的字段名)
vo里要翻译的字段名->getcolvalue2(表名,要翻译的值,code,vo里要翻译的字段名,code,vo里要翻译的字段名)
getcolvalue2可以加多个where条件

14.CkdWebServiceImpl.java

package nc.impl.ws;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.itf.ws.ICkdWebService;
import nc.itf.yhlypx.IKfcksqdMaintain;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.ui.yhlypx.util.NullValueUtils;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVOUtil;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.ws.CksqdBodyJsonVO;
import nc.vo.ws.CksqdJson;
import nc.vo.ws.ResultParam;
import nc.vo.yhlypx.kfcksqd.AggKfcksqdVO;
import nc.vo.yhlypx.kfcksqd.KfcksqdBVO;
import nc.vo.yhlypx.kfcksqd.KfcksqdVO;

import com.google.gson.Gson;


public class CkdWebServiceImpl implements ICkdWebService{

	@Override
	public String reqData(String jsonInfo) throws BusinessException{
		init();
		ResultParam returnParam = new ResultParam();
		try {
			CksqdJson jsonvo = new Gson().fromJson(jsonInfo, CksqdJson.class);
			
			AggKfcksqdVO aggvo = new AggKfcksqdVO();
			KfcksqdVO headvo = new KfcksqdVO();
			List<KfcksqdBVO> bvoslist = new ArrayList<KfcksqdBVO>();
			
			String sql =" select vdef1 from kf_kfcksqd where nvl(dr, 0) = 0 ";
			List<String> list = (List) getDao().executeQuery(sql.toString(), new ColumnListProcessor());
			for(String vdef1 : list){
				if(vdef1 == null){
					continue;
				}
				if(vdef1.equals(NullValueUtils.getNullStringValue(jsonvo.getHead().getFlag()))){
					returnParam.setCode("500");
					returnParam.setMessage("该flag已经存在,新增失败");
					return new Gson().toJson(returnParam);
				}
			}
			headvo.setPk_group(getEnvInfo("groupid"));
			headvo.setPk_org("0001A110000000002CP0");
			headvo.setBillstatus(-1);
			headvo.setDbilldate(new UFDate());
			headvo.setPk_billtype("YH52");
			headvo.setTranstypecode("YH52");
			headvo.setPersonnel(jsonvo.getHead().getPersonnel());
			headvo.setDepartment(jsonvo.getHead().getDepartment());
			headvo.setCategory(jsonvo.getHead().getCategory());
			headvo.setVdef1(jsonvo.getHead().getFlag());
			headvo.setStatus(VOStatus.NEW);
			//将编码翻译为主键
			SuperVOUtil.execFormulaWithVOs(new KfcksqdVO[]{headvo}, new String[]{
				"personnel->getcolvalue(bd_psndoc,pk_psndoc,code,personnel)",
				"department->getcolvalue(org_dept,pk_dept,code,department)"
			});
			aggvo.setParentVO(headvo);
			
			CksqdBodyJsonVO[] bvos = jsonvo.getBody();
			
			if(bvos != null && bvos.length > 0){
				for(int i = 0; i < jsonvo.getBody().length; i++){
					KfcksqdBVO bvo = new KfcksqdBVO();
					bvo.setRowno(NullValueUtils.getNullStringValue((i+1)*10));
					bvo.setCustomer(jsonvo.getBody()[i].getCustomer());
					bvo.setNum(NullValueUtils.getNullUFdoubleValue(jsonvo.getBody()[i].getNum()));
					bvo.setTaxprice(NullValueUtils.getNullUFdoubleValue(jsonvo.getBody()[i].getTaxprice()));
					bvo.setTaxrate(NullValueUtils.getNullUFdoubleValue(jsonvo.getBody()[i].getTaxrate()));
					UFDouble taxprice = NullValueUtils.getNullUFdoubleValue(bvo.getTaxprice());
					UFDouble taxrate = NullValueUtils.getNullUFdoubleValue(bvo.getTaxrate());
					UFDouble num = NullValueUtils.getNullUFdoubleValue(bvo.getNum());
					UFDouble notaxprice = taxprice.div(UFDouble.ONE_DBL.add(taxrate.doubleValue()/100)); 
					UFDouble taxmny = num.multiply(taxprice);
					UFDouble notaxmny = notaxprice.multiply(taxrate.doubleValue()/100);
					UFDouble tax = taxmny.sub(notaxmny);
					bvo.setNotaxprice(notaxprice);
					bvo.setTaxmny(notaxmny);
					bvo.setNotaxmny(notaxmny);
					bvo.setTax(tax);
					bvo.setStatus(VOStatus.NEW);
					bvoslist.add(bvo);
				}
				//将编码翻译为主键
				SuperVOUtil.execFormulaWithVOs(bvoslist.toArray(new KfcksqdBVO[0]), new String[]{
					"customer->getcolvalue(bd_customer,pk_customer,name,customer)"
				});
				aggvo.setChildrenVO(bvoslist.toArray(new KfcksqdBVO[0]));
			}
			//新增
			getService().insert(new AggKfcksqdVO[]{aggvo}, new AggKfcksqdVO[]{aggvo});
			
			String code = "200";
			String resultinfo = "执行成功";
			returnParam.setCode(code);
			returnParam.setMessage(resultinfo);
			
		} catch (BusinessException e) {
			String code = "500";
			returnParam.setCode(code);
			returnParam.setMessage("新增出错" + e.getMessage());
			
		}	
		
		return new Gson().toJson(returnParam);
	}

	/**
	 * 使用数据库
	 */
	private BaseDAO dao;
	private BaseDAO getDao() {
		if(dao == null) {
			dao = new BaseDAO();
		}
		return dao;
	}

	/**
	 * 要修改哪个单子调哪个单子的接口
	 */
	private IKfcksqdMaintain service;
	private IKfcksqdMaintain getService(){
		if (service == null) {
			service = NCLocator.getInstance().lookup(IKfcksqdMaintain.class);
		}
		return service;
	}
	
	
	/**
	 * 初始化数据源、集团、用户
	 */
	private void init(){
		InvocationInfoProxy.getInstance().setUserDataSource(getEnvInfo("datasource"));
		InvocationInfoProxy.getInstance().setGroupId(getEnvInfo("groupid"));
		InvocationInfoProxy.getInstance().setUserId(getEnvInfo("userid"));
	}
	
	/**
	 * 得到配置文件参数值
	 */
	public String getEnvInfo(String param) {
		// 属性集合对象
		Properties prop = new Properties();
		try {
			InputStream fis = CkdWebServiceImpl.class.getResourceAsStream("data.properties");
			// 将属性文件流装载到Properties对象中
			prop.load(fis);
		} catch (Exception e) {
			Logger.debug(e);
		}
		return prop.getProperty(param);
	}

}

15.data.properties

datasource=design
groupid=0001A1100000000002O7
userid=1001A11000000000002X

16.ResultParam.java

package nc.vo.ws;

public class ResultParam {

	private String code;
	private String message;
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	
}

17.注意事项

1.不在json里用UFDate、UFDouble

CksqdJson jsonvo = new Gson().fromJson(jsonInfo, CksqdJson.class);

2.在json里用UFDate、UFDouble

  GsonBuilder gsonBuilder = new GsonBuilder();
  UFDataTypeDeserializer ufDeserializer = new UFDataTypeDeserializer();
  gsonBuilder.registerTypeAdapter(UFDateTime.class, ufDeserializer);
  gsonBuilder.registerTypeAdapter(UFDate.class, ufDeserializer);
  gsonBuilder.registerTypeAdapter(UFBoolean.class, ufDeserializer);
  gsonBuilder.registerTypeAdapter(UFDouble.class, ufDeserializer);
  gsonBuilder.registerTypeAdapter(UFTime.class, ufDeserializer);
  gsonBuilder.registerTypeAdapter(UFLiteralDate.class, ufDeserializer);
  CksqdJson param = gsonBuilder.create().fromJson(jsonInfo.toString(),CksqdJson.class);

3.webservice在接口里面加了方法后,想要在SOAP中显示出来需要重新生成wsdl和xsd文件,然后重启服务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

和安韩Pro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值