webservice 中 String[], List <String>, ArrayOfString的处理

我的实现是在服务器端实现一个服务函数,其返回值是String[]。到这一切很正常。当来到客户端时,发现自动生成的调用函数把返回值类型自动改为了ArrayOfString。其实看名字也就是那个东西,但要把它还原为我要的String[],也要花销几句代码。

解决代码:

       Object   obarray[];


       ArrayOfString ret;
       ret=service.somefunc(......);


       List<String> p=ret.getString();


       obarray[]=p.toArray();
       for(int i=0;i<re.length;i++){
       System.out.println(re[i].toString());
       }

----------------------------------------------------------------------------------------------------------------------------------

以下是从google上搜索到的网友提到的知识点:(google真的比 baidu 要强,特别是网上资料特别少的时候,还有 英文也不是那么特别难懂,一定要耐心看下去)

------------------------------------

String[] 是定长数组,而 List <String> 可以看作是变长数组。 

长度是固定的,即不会增加也不会减少时采用 String[] 
长度未知,并且长度有可能增加时,使用 List <String>,比如要把 
文件中的每一行都放到数组中,由于并不知道文件中的行数,所以使用 
List <String> 就较为方便一些。 

而且 List <String> 底层是以 String[] 为基础而扩展的。

----------------------------------

使用数组,最显著的特点,就是数组的元素个数是固定的,位置也是和元素相对应的。 
String[] 当你确定每个位置的元素(字符串)内容的时候,或者,要频繁通过位置来取得数组内容的时候。使用数组还是很好的。当然,如果你要对数组进行添加,删除某个元素(字符串)那就是比较麻烦的事情,因为,这不但会影响的其他元素的位置,也要影响的数组的大小。 
List <String > 在J2SE里面,确切的将,它是一个接口,表示一个对于链表的所有功能(操作)。当然,J2SE里面也提供的List接口的一些具体的实现类,最常用的应该是ArrayList和LinkedList,使用尖括号将String类括起来,是JDK1.5的范型所要求的,也就是说,这表明,你所使用的链表里面只能存放String类型的数据。当然,使用ArrayList和LinkedList都不用在意链表的长度问题,因为,一般情况下,我们使用的链表大小是不会超出Java整型变量的最大值的。所以,我认为,预期使用String[]不如使用ArrayList <String>,这样既使用方便,也不用顾虑到其他的一些问题(比如增删里面的元素,有可能是元素位置变动之类的)。当然,如果,你频繁的要对这个集合当中的元素进行增删,不妨使用LinkedList,它以循环链表作为原型,对于数据的增删操作,相当方便,正反两个方向的对元素的遍历也可以使用listIterator,还是暴爽无比的。

===============关于StringOfArray的赋值===================

         ArrayOfString attachaddr=new ArrayOfString();

        attachaddr.getString().add("url1");
        attachaddr.getString().add("url2");

    -----somefun(.......,atttachaddr);

注解:attachaddr.getString().返回的是Link<String>, 但通过操作此Link<String>,同样是在操作attachaddr.

因为:Link<String>是引用型变量,其操作地址就是attachaddr的。注意这与C++的区别,不是赋值传递而是引用传递。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化这段代码: //获取慢病信息 List<SlowDiseasePatInfo> slowDiseasePattInfosList = new ArrayList<>(); slowDiseasePattInfosList.add(new SlowDiseasePatInfo(slowDiseaseId,"普通慢病报卡")); Result result = detail(slowDiseasePattInfosList); if (null == result.getData()){ return ResultGenerator.genFailedResult("未查到需要上报的数据"); } List<SlowDiseasePatientInfo> list = (List<SlowDiseasePatientInfo>)result.getData(); String outpatNum = list.get(0).getOutpatNum().isEmpty() ? list.get(0).getInpatNum() : list.get(0).getOutpatNum(); //获取患者的院区 String hospitalCode = emrSlowDiseaseDao.getHospitalCode(outpatNum); String unitCode = ConstantEnum.GW_CZ.getValue().equals(hospitalCode) ? ConstantEnum.GW_CZDWBM.getValue() : ConstantEnum.GW_YHDWBM.getValue(); String regCode = ConstantEnum.GW_CZ.getValue().equals(hospitalCode) ? ConstantEnum.GW_CZZCM.getValue() : ConstantEnum.GW_YHZCM.getValue(); Map<String,Object> reqMap = new HashMap<>(); reqMap.put("slowInfo",list.get(0)); reqMap.put("unitCode",unitCode); reqMap.put("regCode",regCode); String reqInfo = MessageUtil.select(reqMap, "/velocity/slowdisease.vm"); Document document = DocumentHelper.parseText(StringEscapeUtils.unescapeJava(reqInfo.replace("xmlns=\"urn:hl7-org:v3\"", ""))); reqInfo = XmlUtils.documentToStr(document); logger.info("求慢病接口开始{}", reqInfo); String res = HttpClientUtil.sendWebServiceForLis(webservice + "SlowSfqUploadService/SlowSfqUploadService?WSDL", reqInfo.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","")); logger.info("求慢病接口结束{}", res); return ResultGenerator.genOkResult(res);
06-08
可以尝试进行以下优化: 1. 将获取慢病信息的代码封装到一个方法,让代码更加清晰易读。 2. 使用 Optional 类对可能为空的数据进行处理,避免空指针异常。 3. 使用字符串模板替换字符串拼接,让代码更加简洁。 4. 对于多次使用的常量,可以将其定义为常量或枚举类型,提高代码的可读性和可维护性。 5. 将WebService的代码封装到一个工具类,提高代码的可复用性和可测试性。 下面是代码优化后的示例: ```java public Result getSlowDiseaseInfo(String slowDiseaseId) throws Exception { // 获取慢病信息 List<SlowDiseasePatInfo> slowDiseasePatInfosList = new ArrayList<>(); slowDiseasePatInfosList.add(new SlowDiseasePatInfo(slowDiseaseId, "普通慢病报卡")); Result result = detail(slowDiseasePatInfosList); Optional<List<SlowDiseasePatientInfo>> optional = Optional.ofNullable((List<SlowDiseasePatientInfo>) result.getData()); List<SlowDiseasePatientInfo> list = optional.orElseThrow(() -> new RuntimeException("未查到需要上报的数据")); // 获取患者的院区 String outpatNum = Optional.ofNullable(list.get(0).getOutpatNum()).orElse(list.get(0).getInpatNum()); String hospitalCode = emrSlowDiseaseDao.getHospitalCode(outpatNum); String unitCode = ConstantEnum.GW_CZ.getValue().equals(hospitalCode) ? ConstantEnum.GW_CZDWBM.getValue() : ConstantEnum.GW_YHDWBM.getValue(); String regCode = ConstantEnum.GW_CZ.getValue().equals(hospitalCode) ? ConstantEnum.GW_CZZCM.getValue() : ConstantEnum.GW_YHZCM.getValue(); // 封装参数 Map<String, Object> reqMap = new HashMap<>(); reqMap.put("slowInfo", list.get(0)); reqMap.put("unitCode", unitCode); reqMap.put("regCode", regCode); String reqInfo = MessageUtil.select(reqMap, "/velocity/slowdisease.vm"); // 发送WebServiceString reqBody = String.format("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v3=\"urn:hl7-org:v3\"><soapenv:Header/><soapenv:Body>%s</soapenv:Body></soapenv:Envelope>", reqInfo); String res = HttpClientUtil.sendWebServiceForLis(webservice + "SlowSfqUploadService/SlowSfqUploadService?WSDL", reqBody); return ResultGenerator.genOkResult(res); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值