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

时间:2021-09-21 20:16:19

I have a class generated with JAXB2 form a WSDL. The elements defined in the WSDL are NOT declared as XmlRootElement.

我有一个用JAXB2生成的类形成一个WSDL。 WSDL中定义的元素未声明为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();
}

}

}

The generated StringResult looks quiet good but I need to replace some parts in the resulting XML (for instance the prefix) and I need to add some stuff into the SoapBody which are not part of the base class (Problem) before sending the SOAP request to the remote service.

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

Furthermore I want to modify the header part of the envelope... How can I achieve this? My application is a SpringBoot application and in the configuration class being used in my service the un-/marshaller are defined this way:

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

@Bean
public Jaxb2Marshaller marshaller() {
    final Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    //setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setContextPath(contextPath);
    //marshaller.afterPropertiesSet();
    marshaller.setMarshallerProperties(new HashMap<String, Object>() {{
          put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
        }});

    return marshaller;
}

@Bean
public ProblemService problemService(final Jaxb2Marshaller marshaller) throws Exception {
    final ProblemService client = new ProblemService();
    client.setDefaultUri(this.endpoint);
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);

     final HttpsUrlConnectionMessageSender msgSender = new HttpsUrlConnectionMessageSender();
    client.setMessageSenders(new WebServiceMessageSender[] {msgSender, httpComponentsMessageSender()});
    //client.setMessageSender(msgSender);

     return client;
}

1 个解决方案

#1


0  

With this little piece of code I was able to add information to the SoapBody as demanded:

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

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

#1


0  

With this little piece of code I was able to add information to the SoapBody as demanded:

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

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