【问题标题】:soap body blank when calling webservice using apache cxf api使用 apache cxf api 调用 web 服务时肥皂正文空白
【发布时间】:2016-01-18 06:08:04
【问题描述】:

将肥皂正文附加到 Web 服务时遇到问题。我正在使用 apache cxf api。 调用 web 服务时,我看到肥皂体生成为空白。 我正在使用以下代码:

 GetPDFDocumentRequest request = new ObjectFactory().createGetPDFDocumentRequest();
    request.setCrid(reference);
    request.setResourceId(confirmationId);
    request.setVersion(Integer.parseInt(version));
    request.setDocType(pdfType);
    String encryptedString  = svc().getPDFDocument(request).getDocument();

虽然当我从 GetPDFDocumentRequest 打印值时。所有值:crid、resourceid、version 和 doctype 都有值,但我没有看到它附加在 soap 正文中,如下所示:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"></env:Header><soap:Body><ns2:GetPDFDocumentRequest xmlns="http://test.fm.com/Static/interface" xmlns:ns2="https://test.fm.grp.net/style/webservices/schemas/messages" xmlns:ns3="http://test.fm.com/EventML" xmlns:ns4="http://test.fm.com/StyleML" xmlns:ns5="http://test.fm.com/odc" xmlns:ns6="https://test.fm.rbsgrp.net/test/lifecycle/schemas/types" xmlns:ns7="https://test.fm.rbsgrp.net/style/persistence/schemas/types" xmlns:ns8="http://test.fm.com/Reporting/interface" xmlns:ns9="http://test.fm.com/ConfigML"/></soap:Body>

我在 maven 中使用下面的 api:

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.4</version>
</dependency> 

我什至添加了 cxf-rt-frontend-jaxrs,但使用后也​​没有成功。

拦截器代码:

import java.io.OutputStream;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.io.CacheAndWriteOutputStream;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.io.CachedOutputStreamCallback;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class LogInterceptor extends LoggingOutInterceptor {

     private Logger logger = Logger.getLogger(LogInterceptor.class);
    public LogInterceptor() {
        super(Phase.PRE_STREAM);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        OutputStream out = message.getContent(OutputStream.class);
        final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(out);
        message.setContent(OutputStream.class, newOut);
        newOut.registerCallback(new LoggingCallback());
    }

    public class LoggingCallback implements CachedOutputStreamCallback {
        public void onFlush(CachedOutputStream cos) {
        }

        public void onClose(CachedOutputStream cos) {
            try {
                StringBuilder builder = new StringBuilder();
                cos.writeCacheTo(builder, limit);
                String soapXml = builder.toString();
                logger.log(Level.INFO, "SOAP XML SENT TO STYLE" + soapXml);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

【问题讨论】:

    标签: java web-services soap wsdl cxf


    【解决方案1】:

    通过检查 Java 代码中的请求 POJO,您将无法看到 SOAP 主体/包装元素。这是因为如果 CXF 在框架级别发送请求之前将请求包装在 SOAP 主体中。它对程序员是透明的,不能在java代码中查看。

    但是,如果您真的想查看和拦截原始消息,请尝试使用“JAXWS 处理程序”或其等效的“CXF 拦截器”。

    更重要的是,除非您需要对数据进行一些特殊的前/后处理或按摩,否则您实际上并不需要拦截来查看 SOAP 主体。否则,仅使用 Java 类中的服务方法,将数据读取或写入到 CXF wsgen 创建的 POJO 就足以满足大多数实际用途。

    更新: 我发现了一个与您的问题完全相同的 CXF JIRA 问题。 它在最后为您的问题提供了解决方案 - 我认为这很可能也是您的解决方案。这是 JIRA 链接 - https://issues.apache.org/jira/browse/CXF-2405

    【讨论】:

    • 我只在输出拦截器中打印了这个肥皂体。目前我的问题是肥皂体是空白的。它应该像下面评论中提到的那样
    • 好的,你能在拦截器中分享你打印整个请求的代码吗(在没有 SOAP 正文的情况下打印它)...
    • 也只是为了确认在 web 服务端肥皂体也是空白的。此代码以其他方式工作,但从其中一个应用程序生成肥皂体。它部署在 JBOSS 5.1.2.GA 中。这不是拦截器的问题。相同的拦截器在其他应用程序上运行良好。
    • 在我看来这不是我的问题。在我的场景中,soap body 是生成的参数 id,未设置。例如:哭。我认为jboss有一些问题。因为否则我的代码在从 Windows 服务调用时工作正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多