简体   繁体   中英

soap body blank when calling webservice using apache cxf api

Iam facing issue when appending soap body to the webservice. Iam using apache cxf api. When calling webservice I see soap body is generated as blank. Iam using below code :

 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();

Although when I print values from GetPDFDocumentRequest. All values : crid, resourceid, version and doctype has value, But I dont see it appended in soap body as follows:

<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>

Iam using below api's in maven :

<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> 

I even added cxf-rt-frontend-jaxrs, but no success after using this also.

Code for interceptor:

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();
            }
        }
    }
}

You won't be able to see SOAP body/wrapping elements by examining the request POJO in java code. This is because if CXF wraps the requests in the SOAP body at just before sending the request at the framework level. It is transparent to the programmer and cannot be viewed in the java code.

However, if you really want to see and intercept raw message then try using "JAXWS Handlers" or their equivalent "CXF Interceptors".

More importantly, you don't really need to intercept to see SOAP body unless you need to do some special pre/post processing or massaging of data. Otherwise, just working with the service method in Java class with data being read or written to CXF wsgen created POJOs is good enough for most practical purposes.

Update: I found a CXF JIRA issue with exactly the same issue as yours. It gives the solution write at the end for your problem - most likely I think that might be the solution for you as well. Here is the JIRA link - https://issues.apache.org/jira/browse/CXF-2405

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM