天天看點

webservice用戶端在weblogic中間件下失效問題

背景描述:公司産品開發環境為eclipse+tomcat,是以開發的webservice接口是在tomcat下測試的,測試沒發現問題。但是部署到weblogic下發現接口調不通,報空指針。

處理方法:

1、由于在本地測試沒發現問題,并且本地與部署到生産使用的是同一個websrvice接口。于是在生産部署了一套tomcat環境,測試發現沒問題,考慮非代碼bug問題。

2、網上找了很多解決辦法,嘗試了一些,都沒效果。無奈,隻能再次回到代碼方面,修改代碼開發邏輯,之前采用的是jdk原生模式生成的用戶端代碼(懶人專用),不會的可以參考我的另一篇文章,https://www.cnblogs.com/jjflover/p/13732368.html。

3、期間修改了兩種實作方法,發現還是各種問題,偶然間在一片文章裡看到需要修改weblogic加載jar包的順序,試了一下,竟然成功了,血淚史就此結束。。。。

4、總結,開始報的空指針是由于産品本身的jar包跟weblogic自身的jar包沖突導緻,我曾試着分析,但是沒找到具體沖突jar包和處理方法。就用了一個簡單粗暴的方法,由于本項目jar不需要weblogic的依賴jar,是以找到weblogic.xml。

prefer-web-inf-classes 中true 改為false。

webservice用戶端在weblogic中間件下失效問題

5、期間嘗試的另外兩種實作方法也記錄一下

①:此種方法不需要額外導入jar包

webservice用戶端在weblogic中間件下失效問題
webservice用戶端在weblogic中間件下失效問題
1 package com.fotic.it.support.rpcservice.api.webservice;
 2 
 3 import java.net.MalformedURLException;
 4 import java.net.URL;
 5 import java.text.SimpleDateFormat;
 6 import java.util.Calendar;
 7 import java.util.Date;
 8 import java.util.Iterator;
 9 import java.util.List;
10 import java.util.Map;
11 
12 import javax.xml.namespace.QName;
13 import javax.xml.ws.Service;
14 import javax.xml.ws.soap.SOAPBinding;
15 
16 import org.junit.Test;
17 
18 import com.fotic.it.support.rpcservice.api.webservice.NoticeSendService;
19 
20 public class myTest {
21             static String toMail = "[email protected]";
22             static Date date = new Date();
23             static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); 
24             static Calendar c = Calendar.getInstance();
25             static String sendDate = sf.format(date);
26             static String sDate = sendDate;
27             static String eDate = sf.format(c.getTime());
28             static String appId = "1";
29             static String attachInfo = "";
30             static String businessCateGory = "業務分類";
31             static String businessType = "1";
32             static String htmlContent = "您有任務被駁回,請前往系統檢視!!!";
33             static String sendRange = "5";
34             static String infoUrl = "\'\'";
35             static Long projectId = (long) 1;
36             static String sendRangValue = "";
37             static String remindLevel = "1";
38             static String roleRangeId = "[email protected]";
39             static String secCode = "98765";
40             static String channel = "2";
41             static String sponsornam = "";
42             static String sponsorId = "";
43             static String textContent = "";
44             static String title = "任務提醒";
45     
46     public String sendEmail(String appId2, String attachInfo2, String businessCateGory2, String businessType2,
47             String eDate2, String htmlContent2, String infoUrl2, Long projectId2, String remindLevel2,
48             String roleRangeId2, String sDate2, String secCode2, String channel2, String sendDate2, String sendRange2,
49             String sendRangValue2, String sponsorId2, String sponsornam2, String textContent2, String title2)
50             throws MalformedURLException, ClassNotFoundException {
51         URL url = new URL("http://ip:8651/services/NoticeSendService?wsdl");
52         // 指定命名空間和服務名稱
53         QName qName = new QName("http://email.fotic.com", "NoticeSendService");
54         Service service = Service.create(url, qName);
55         System.out.println(service.getPorts());        
56         // 通過getPort方法傳回指定接口
57         NoticeSendService myServer = service.getPort(NoticeSendService.class);
58         // 調用方法 擷取傳回值
59         String result = myServer.executeProcedure(appId2, attachInfo2, businessCateGory2, businessType2, eDate2, htmlContent2,
60                 infoUrl2, projectId2, remindLevel2, roleRangeId2, sDate2, secCode2, channel2, sendDate2, sendRange2,
61                 sendRangValue2, sponsorId2, sponsornam2, textContent2, title2);
62         System.out.println(result);
63         return result;
64     }    
65     public static void main(String[] args) throws MalformedURLException {
66         URL url = new URL("http://ip:8651/services/NoticeSendService?wsdl");
67         // 指定命名空間和服務名稱
68         QName qName = new QName("http://email.fotic.com", "NoticeSendService");
69         Service service = Service.create(url, qName);
70         // 通過getPort方法傳回指定接口
71         NoticeSendService myServer = service.getPort(new QName("http://email.fotic.com","NoticeSendServiceImplPort"), NoticeSendService.class);
72         // 調用方法 擷取傳回值
73         String result = myServer.executeProcedure(appId, attachInfo, businessCateGory, businessType, eDate, htmlContent, 
74                 infoUrl, projectId, remindLevel, roleRangeId, sDate, secCode, channel, sendDate, sendRange, 
75                 sendRangValue, sponsorId, sponsornam, textContent, title );
76         System.out.println(result);
77     }
78 }      

View Code

②:此種方法采用axis方式,需要額外導入jar包

webservice用戶端在weblogic中間件下失效問題
webservice用戶端在weblogic中間件下失效問題
1 package com.fotic.it.support.rpcservice.api.webservice;
  2 
  3 import java.net.URL;
  4 import java.text.SimpleDateFormat;
  5 import java.util.Calendar;
  6 import java.util.Date;
  7 import javax.xml.namespace.QName;
  8 import org.apache.axis.client.Call;
  9 import org.apache.axis.client.Service;
 10 import org.apache.axis.encoding.XMLType;
 11 
 12 public class myTest2 {
 13             static String toMail = "[email protected]";
 14             static Date date = new Date();
 15             static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); 
 16             static Calendar c = Calendar.getInstance();
 17             static String sendDate = sf.format(date);
 18             static String sDate = sendDate;
 19             static String eDate = sf.format(c.getTime());
 20             static String appId = "1";
 21             static String attachInfo = "";
 22             static String businessCateGory = "業務分類";
 23             static String businessType = "1";
 24             static String htmlContent = "您有任務被駁回,請前往系統檢視!!!";
 25             static String sendRange = "5";
 26             static String infoUrl = "\'\'";
 27             static Long projectId = (long) 1;
 28             static String sendRangValue = "";
 29             static String remindLevel = "1";
 30             static String roleRangeId = "[email protected]";
 31             static String secCode = "98765";
 32             static String channel = "2";
 33             static String sponsornam = "";
 34             static String sponsorId = "";
 35             static String textContent = "";
 36             static String title = "任務提醒";
 37     
 38     public String sendEmail(String appId2, String attachInfo2, String businessCateGory2, String businessType2, String eDate2, 
 39             String htmlContent2, String infoUrl2, Long projectId2, String remindLevel2, String roleRangeId2, String sDate2, 
 40             String secCode2, String channel2, String sendDate2, String sendRange2, String sendRangValue2, String sponsorId2,
 41             String sponsornam2, String textContent2, String title2) {
 42         System.out.println("0000000");
 43         String url = "http://ip:8651/services/NoticeSendService?wsdl";
 44         org.apache.axis.client.Service service = new Service();
 45         try {
 46             org.apache.axis.client.Call call = (Call) service.createCall();
 47             call.setTargetEndpointAddress(new URL(url));
 48             System.out.println("0");
 49             // WSDL裡面描述的接口名稱(要調用的方法)
 50             call.setOperationName(new QName("http://email.fotic.com", "executeProcedure"));
 51             System.out.println("1");
 52             //跨平台調用加上這個
 53            call.setUseSOAPAction(true);
 54            System.out.println("2");
 55            //call.setSOAPActionURI("http://ip:8651/services/NoticeSendService");
 56             // 接口方法的參數名, 參數類型,參數模式 IN(輸入), OUT(輸出) or INOUT(輸入輸出)
 57             call.addParameter("appId", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 58             call.addParameter("attachInfo", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 59             call.addParameter("businessCateGory", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 60             call.addParameter("businessType", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 61             call.addParameter("eDate", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 62             call.addParameter("htmlContent", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 63             call.addParameter("infoUrl", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 64             call.addParameter("projectId", XMLType.XSD_LONG, javax.xml.rpc.ParameterMode.IN);
 65             call.addParameter("remindLevel", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 66             call.addParameter("roleRangeId", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 67             call.addParameter("sDate", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 68             call.addParameter("secCode", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 69             call.addParameter("channel", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 70             call.addParameter("sendDate", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 71             call.addParameter("sendRange", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 72             call.addParameter("sendRangValue", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 73             call.addParameter("sponsorId", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 74             call.addParameter("sponsornam", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 75             call.addParameter("textContent", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 76             call.addParameter("title", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
 77             // 設定被調用方法的傳回值類型
 78             call.setReturnType(XMLType.XSD_STRING);
 79             
 80             //輸出SOAP請求封包
 81            // System.out.println("--SOAP Request: " + call.getMessageContext().getRequestMessage().getSOAPPartAsString());
 82             //輸出SOAP傳回封包
 83            // System.out.println("--SOAP Response: " + call.getResponseMessage().getSOAPPartAsString());
 84             //輸出傳回資訊
 85            // System.out.println("result==="+ret.toString());
 86             
 87             // 設定方法中參數的值
 88             Object result = call.invoke(new Object[] { appId, attachInfo, businessCateGory, businessType, eDate, htmlContent, 
 89                     infoUrl, projectId, remindLevel, roleRangeId, sDate, secCode, channel, sendDate, sendRange, 
 90                     sendRangValue, sponsorId, sponsornam, textContent, title });
 91             System.out.println("4");
 92             System.out.println(result.toString());
 93         } catch (Exception e) {
 94             e.printStackTrace();
 95         }
 96     
 97         return "";
 98     }
 99     
100     public static void main(String[] args) {
101         String url = "http://ip:8651/services/NoticeSendService?wsdl";
102         org.apache.axis.client.Service service = new Service();
103         try {
104             org.apache.axis.client.Call call = (Call) service.createCall();
105             call.setTargetEndpointAddress(new URL(url));
106             System.out.println("0");
107             // WSDL裡面描述的接口名稱(要調用的方法)
108             call.setOperationName(new QName("http://email.fotic.com", "executeProcedure"));
109             System.out.println("1");
110             //跨平台調用加上這個
111            call.setUseSOAPAction(true);
112            System.out.println("2");
113            //call.setSOAPActionURI("http://10.7.101.40:8651/services/NoticeSendService");
114             // 接口方法的參數名, 參數類型,參數模式 IN(輸入), OUT(輸出) or INOUT(輸入輸出)
115             call.addParameter("appId", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
116             call.addParameter("attachInfo", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
117             call.addParameter("businessCateGory", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
118             call.addParameter("businessType", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
119             call.addParameter("eDate", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
120             call.addParameter("htmlContent", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
121             call.addParameter("infoUrl", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
122             call.addParameter("projectId", XMLType.XSD_LONG, javax.xml.rpc.ParameterMode.IN);
123             call.addParameter("remindLevel", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
124             call.addParameter("roleRangeId", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
125             call.addParameter("sDate", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
126             call.addParameter("secCode", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
127             call.addParameter("channel", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
128             call.addParameter("sendDate", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
129             call.addParameter("sendRange", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
130             call.addParameter("sendRangValue", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
131             call.addParameter("sponsorId", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
132             call.addParameter("sponsornam", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
133             call.addParameter("textContent", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
134             call.addParameter("title", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
135             // 設定被調用方法的傳回值類型
136             call.setReturnType(XMLType.XSD_STRING);
137             
138             //輸出SOAP請求封包
139            // System.out.println("--SOAP Request: " + call.getMessageContext().getRequestMessage().getSOAPPartAsString());
140             //輸出SOAP傳回封包
141            // System.out.println("--SOAP Response: " + call.getResponseMessage().getSOAPPartAsString());
142             //輸出傳回資訊
143            // System.out.println("result==="+ret.toString());
144             
145             // 設定方法中參數的值
146             Object result = call.invoke(new Object[] { appId, attachInfo, businessCateGory, businessType, eDate, htmlContent, 
147                     infoUrl, projectId, remindLevel, roleRangeId, sDate, secCode, channel, sendDate, sendRange, 
148                     sendRangValue, sponsorId, sponsornam, textContent, title });
149             System.out.println("4");
150             System.out.println(result.toString());
151         } catch (Exception e) {
152             e.printStackTrace();
153         }
154     }
155     
156 }      

View Code