[英]Error using SAAJ on WildFly 8.2
我正在處理使用SOAP和Java SAAJ將文件發送到Web服務的WilfFly Web應用程序。
我遇到此錯誤(在woodstox-core-asl-4.4.0.jar上):
12:02:46,927 WARNING [org.apache.cxf.phase.PhaseInterceptorChain] (default task-24)
Interceptor for {http://wscmis.pms.vr.it/}WSChemistry has thrown exception,
unwinding now: org.apache.cxf.binding.soap.SoapFault: Error reading XMLStreamReader.
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-'
(code 45) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]
這是我調用WS的方法:
public JSONObject invokeWS(String WSURL, String metodo, Map<String,String> params, String namespace){
MessageFactory factory;
StringBuilder sb = new StringBuilder();
JSONObject jsonResult=null;
try {
factory=MessageFactory.newInstance();
SOAPMessage message=factory.createMessage(); //Creo il messaggio SOAP
SOAPPart soapPart=message.getSOAPPart();
SOAPEnvelope envelope=soapPart.getEnvelope(); //Prendo l'envelope e poi il body (dell'header chissenefrega)
envelope.addNamespaceDeclaration("com", namespace); //Metto il namespace nell'envelope, Enrico's style
SOAPHeader header = envelope.getHeader();
header.detachNode();
SOAPBody body=envelope.getBody();
String elemento="com:"+metodo;
QName bodyName=new QName(elemento); //Mi preparo un elemento da mettere nel body
SOAPBodyElement bodyElement=body.addBodyElement(bodyName);
for(Map.Entry<String, String> entry : params.entrySet()){
if( !entry.getKey().equals("filedocpms") && !entry.getKey().equals("ctype") && !entry.getKey().equals("nomefile")) { //Se sono attributi che non hanno a che fare con l'allegato li metto normalmente nel messaggio SOAP
QName name=new QName( entry.getKey() ); //Due elementi figli da mettere nell'elemento pms:computer creato prima
SOAPElement symbol=bodyElement.addChildElement(name);
symbol.addTextNode( entry.getValue() );
}
}
byte[] binario=params.get("filedocpms").getBytes(Charset.forName("UTF-8"));
File f=new File("Test");
FileUtils.writeByteArrayToFile(f, binario);
FileDataSource fds=new FileDataSource(f);
DataHandler dataHandler = new DataHandler(fds);
AttachmentPart attachment = message.createAttachmentPart(dataHandler);
attachment.setContentId( params.get("nomefile") );
message.addAttachmentPart(attachment);
java.net.URL url=new java.net.URL(WSURL); //Preparo URL e stream per inviare il messaggio SOAP
java.net.URLConnection conn=url.openConnection();
conn.setDoOutput(true);
message.writeTo(conn.getOutputStream()); //Scrivo il messaggio SOAP nello stream
java.io.BufferedReader rd=new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream())); //Dalla connessione prendo l'input stream per leggere i risultati
String inline="";
while ((inline=rd.readLine()) != null) {
sb.append(inline);
}
JSONObject xmlJSONObj=XML.toJSONObject(sb.toString()); //Facccio un JSON Dall'XML ritornato dal WS
jsonResult=xmlJSONObj.getJSONObject("soap:Envelope").getJSONObject("soap:Body").getJSONObject("ns2:"+metodo+"Response"); //Prendo la parte del JSON che mi interessa
}
catch (SOAPException soape) {
soape.printStackTrace();
}
catch (MalformedURLException mue) {
mue.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
return jsonResult;
}
基本上,它從params Map對象接收一些參數。 參數filedocpms是字節數組中的二進制文件,在使用Apache Commons IO進行一些操作后,我將其放在附件部分中。
我被困住了,我不知道該怎么辦,非常感謝您的幫助。
編輯:我以這種方式使用SOAPConnection:
SOAPConnectionFactory soapConnectionFactory=SOAPConnectionFactory.newInstance();
SOAPConnection connection=soapConnectionFactory.createConnection();
java.net.URL endpoint=new URL(WSURL);
SOAPMessage response=connection.call(message, endpoint);
connection.close();
System.out.println( response.getSOAPHeader() );
System.out.println( response.getSOAPBody() );
但是,這兩個System.out向我顯示了該錯誤,但錯誤仍然存在:
09:41:12,758 INFO [stdout] (default task-24) null
09:41:12,759 INFO [stdout] (default task-24) [soap:Body: null]
我做錯了什么?
發生此錯誤的原因是,發送請求時未設置正確的Content-Type
。 如果要使用SAAJ,則應使用javax.xml.soap.SOAPConnection
API(而不是java.net.URL
),該API負責設置適當的HTTP標頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.