简体   繁体   English

使用Apache Camel和Apache CXF进行Soap Web服务

[英]Soap Web services with Apache Camel and Apache CXF

I am implementing SOAP web services with Apache CXF. 我正在用Apache CXF实现SOAP Web服务。 I am using Jboss EAP server. 我正在使用Jboss EAP服务器。 I have used following code to expose SOAP web services. 我使用以下代码公开了SOAP Web服务。

CxfComponent cxfComponent = new CxfComponent(context);
            CxfEndpoint serviceEndpoint = new CxfEndpoint(FPSoapServiceConstants.WSDL_CONFIG_URI, cxfComponent);
            serviceEndpoint.setDataFormat(DataFormat.PAYLOAD);
        serviceEndpoint.setServiceClass(com.fp.en.webservices.fulfillment.FulfillmentService.class);
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("faultStackTraceEnabled", true);
        properties.put("exceptionMessageCauseEnabled", true);
        serviceEndpoint.configureProperties(properties);
        serviceEndpoint.setLoggingFeatureEnabled(true);
        context.addEndpoint(FPSoapServiceConstants.SOAP_ENDPOINT_FULFILLMENT_SERVICE, serviceEndpoint);

I am using apache camel to process incoming soap message 我正在使用apache骆驼来处理传入的肥皂消息

route.process(fpSOAPRequestProcessor).process(xyzProcessor).process(fpSOAPResponseProcessor)

I want to get all parameters in an object I created a class and try to get body 我想在创建类的对象中获取所有参数,然后尝试获取主体

BuyProductRequest buyRequest = message.getBody(BuyProductRequest.class);

but this is giving me null. 但这给了我空。 But when I try to get 但是当我试图得到

String buyRequest = message.getBody(String.class);

It is giving me SOAP message So I have to convert xml SOAP message to Object by JAXB Marshaller. 它给了我SOAP消息,所以我必须通过JAXB Marshaller将xml SOAP消息转换为Object。 Processor code is as follows 处理器代码如下

public class FPSoapRequestProcessor implements Processor{

    @Override
    public void process(Exchange exchange) throws Exception {
        Message message = exchange.getIn();
        String operation = String.valueOf(exchange.getIn().getHeader("operationName"));
        if(FPSoapServiceConstants.BUY_PRODUCT_SOAP_OPERATION.equalsIgnoreCase(operation)) {
            populateBuyProductOperationProperties(message);
        }


    }

    private void populateBuyProductOperationProperties(Message message) {
        String buyRequest = message.getBody(String.class);
        BuyProductRequest productInfo= parseRequest(buyRequest);
        message.setHeader("MSISDN", productInfo.getMsisdn());
        message.setHeader("iname", productInfo.getIname());
        message.setHeader("input", productInfo.getInput());
        message.setHeader("username", productInfo.getUserName());
        message.setHeader("password", productInfo.getPassword());
        message.setHeader("soapConversion", true);

    }

    private BuyProductRequest parseRequest(String soapRequest){
        try(InputStream is = new ByteArrayInputStream(soapRequest.getBytes())) {
            JAXBContext jaxbContext = JAXBContext.newInstance(BuyProductRequest.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            return (BuyProductRequest) jaxbUnmarshaller.unmarshal(is);
        } catch (Exception e) {
            throw new RuntimeException("SOAP Request Object Resolving Error",e);
        }
    }


}

So Is there any simple way to construct request object and Similarly at that time when I am done with processing, in fpSOAPResponseProcessor I have to convert my object into soap string then I am sending it. 因此,有没有一种简单的方法来构造请求对象,并且类似地,当我完成处理时,在fpSOAPResponseProcessor中,我必须将我的对象转换为soap字符串,然后再发送它。

fpSoapResponseProcessor code is as follows fpSoapResponseProcessor代码如下

public class FDPSoapResponseProcessor implements Processor{
    @Override
    public void process(Exchange exchange) throws Exception {
        Message message = exchange.getIn();

        FulfillmentResponse response = XmlUtil.unmarshall(message.getBody(String.class), FulfillmentResponse.class);
        BuyProductResponse buyProductResponse = new BuyProductResponse();

        buyProductResponse.setProductResponse(response);
        String soapResponse = parse(buyProductResponse);

        exchange.getOut().setBody(soapResponse);

    }

    private String parse(BuyProductResponse buyProductResponse) {
        try(StringWriter writer = new StringWriter()){
            JAXBContext jContext = JAXBContext.newInstance(BuyProductResponse.class);
            Marshaller marshallObj = jContext.createMarshaller();
            marshallObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshallObj.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            marshallObj.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshallObj.setProperty("com.sun.xml.bind.namespacePrefixMapper", new FulfillmentResponseMapper());
            marshallObj.marshal(buyProductResponse, writer);
            return writer.toString();
        } catch(Exception e) {
            throw new RuntimeException("SOAP Request String Parsing Error",e);
        }
    }

    private static class FulfillmentResponseMapper extends NamespacePrefixMapper {

        @Override
        public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
            if(FPSoapServiceConstants.SOAP_NAMESPACE_URI.equalsIgnoreCase(namespaceUri)) {
                return FPSoapServiceConstants.SOAP_PREFIX;
            } 
            return suggestion;
        }

        @Override
        public String[] getPreDeclaredNamespaceUris() {
            return new String[] { FPSoapServiceConstants.SOAP_NAMESPACE_URI};
        }

    }



}

Please suggest a proper simple way If there is? 请提出一个正确的简单方法如果存在?

AFAICS,这是简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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