【问题标题】:Error using SAAJ on WildFly 8.2在 WildFly 8.2 上使用 SAAJ 时出错
【发布时间】:2015-07-03 10:28:18
【问题描述】:

我正在开发使用 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]

我做错了什么?

【问题讨论】:

    标签: java soap wildfly saaj


    【解决方案1】:

    这个错误的原因是你在发送请求时没有设置正确的Content-Type。如果您想使用 SAAJ,那么您应该使用 javax.xml.soap.SOAPConnection API(而不是 java.net.URL),它负责设置适当的 HTTP 标头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2015-10-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      相关资源
      最近更新 更多