如何向JAXBElement SOAP请求添加信息?

时间:2018-05-16 11:27:58

标签: spring-boot soap jaxb jaxb2 jaxbelement

我有一个用JAXB2生成的类形成一个WSDL。 WSDL中定义的元素是 NOT ,声明为XmlRootElement。

@Service
public class ProblemService extends WebServiceGatewaySupport {
    public ProblemResponse addProblem(final Problem problem, final String aNumber) {
    final String namespacePrefix = "soapenv";
    final String action = "Problem";
    final ObjectFactory factory = new ObjectFactory();

    final JAXBElement<Problem> request = factory.createProblem(problem);
    try {
        StringResult result = new StringResult();
        getMarshaller().marshal(request, result);
        System.out.println(result.toString());
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }

    final WebServiceTemplate wst = this.getWebServiceTemplate();
    @SuppressWarnings("unchecked")
    final JAXBElement<ProblemResponse> response = (JAXBElement<ProblemResponse>) wst
            .marshalSendAndReceive(abcConfiguration.getEndpoint(), request, new WebServiceMessageCallback() {
                @Override
                public void doWithMessage(final WebServiceMessage message) {
                    try {

                        prepareSoapHeader(message, namespacePrefix, action);
                        final SaajSoapMessage ssMessage = (SaajSoapMessage) message;
                        final SOAPEnvelope envelope = ssMessage.getSaajMessage().getSOAPPart().getEnvelope();
                        envelope.getBody().setPrefix(namespacePrefix);

                        final NodeList nl = ssMessage.getSaajMessage().getSOAPPart().getEnvelope().getBody().getChildNodes();

                        ssMessage.getSaajMessage().getSOAPPart().getEnvelope().getBody().removeChild(nl.item(0));
                        final SOAPElement se = ssMessage.getSaajMessage().getSOAPPart().getEnvelope().getBody()
                                .addBodyElement(new QName(action));
                        se.setPrefix(NAMESPACE_PREFIX_V2);
                        addUserAuthentification(se);

                        try {
                            StringResult result = new StringResult();
                            getAbcConfiguration().marshaller().marshal(request, result);
                            System.out.println(result.toString());
                        } catch (Exception e) {
                            e.printStackTrace(System.err);
                        }
                        System.out.println();
                    } catch (SoapFaultClientException e) {
                        logger.error("Error on client side during marshalling of the SOAP request for {}.", action, e);
                    } catch (SOAPException e) {
                        logger.error("Error during marshalling of the SOAP request for {}.", action, e);
                    }
                }
            });

    return response.getValue();
}

}

生成的StringResult看起来很安静,但我需要替换生成的XML中的一些部分(例如前缀),我需要在SoapBody中添加一些不属于基类的东西({ {1}})在将SOAP请求发送到远程服务之前。

此外,我想修改信封的标题部分... 我怎样才能做到这一点?我的应用程序是一个SpringBoot应用程序,在我的服务中使用的配置类中,un- / marshaller以这种方式定义:

Problem

1 个答案:

答案 0 :(得分:0)

通过这段代码,我可以按要求向SoapBody添加信息:

try {
    getKpmConfiguration().marshaller().marshal(request, ssMessage.getPayloadResult());
    ssMessage. writeTo(System.out);
} catch (/*JAXB*/Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}