I am writing a springboot application to convert SOAP to REST by using apache camel -
private String transformSoapToRest(String soapRequest) throws ParserConfigurationException, IOException, SAXException { soapRequest = soapRequest.trim(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(soapRequest))); String requestData = document.getElementsByTagName("arg0").item(0).getTextContent(); return "{ \"requestData\": \"" + requestData +"\" }"; } private String transformRestToSoap(String restResponse) { restResponse = restResponse.trim(); String soapResponse = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +"<soap:Body>" +"<ser:processRequestResponse xmlns:ser=\"http://Service.project.Soap2Rest.com/\">" +"<return>" + restResponse +"</return>" +"</ser:processRequestResponse>" +"</soap:Body>" +"</soap:Envelope>"; return soapResponse; }
this is my logic for the same and this is my route -
from("cxf:bean:soapCxfEndpoint") .log("Received SOAP request: ${body}") .process(exchange -> { String soapRequest = exchange.getIn().getBody(String.class).trim(); exchange.getIn().setBody(soapRequest); String restResponse = transformSoapToRest(soapRequest); log.info("Transformed rest request: " + soapRequest); exchange.getIn().setBody(restResponse.trim()); }) .log("Transformed to REST request: ${body}") .to("http://localhost:8080/rest/api") .log("Received REST response: ${body}") .process(exchange -> { String restRequest = exchange.getIn().getBody(String.class).trim(); String soapResponse = transformRestToSoap(restRequest); log.info("Transformed rest request: " + restRequest); exchange.getIn().setBody(soapResponse.trim()); }) .log("Transformed to SOAP response: ${body}") .setHeader(Exchange.CONTENT_TYPE, constant("text/xml")) .setHeader(Exchange.HTTP_METHOD, constant("POST")) .to("direct:log"); from("direct:log") .log("Log entry: ${body}"); }
i entered the following on my postman with the url http://localhost:8080/services/soap
and the follwing xml -
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://Service.project.Soap2Rest.com/"><soapenv:Header/><soapenv:Body><ser:processRequest><arg0>Sample Request</arg0></ser:processRequest></soapenv:Body></soapenv:Envelope>
and i am continuously getting the error -
2024-07-17T18:24:05.447+05:30 DEBUG 21736 --- [project] [nio-8080-exec-1] o.a.c.p.e.DefaultErrorHandler : Failed delivery for (MessageId: DEA1ACD506C7924-0000000000000000 on ExchangeId: DEA1ACD506C7924-0000000000000000). On delivery attempt: 0 caught: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.2024-07-17T18:24:05.449+05:30 INFO 21736 --- [project] [nio-8080-exec-1] route1 : Exception occurred: Content is not allowed in prolog.
I have made sure that there are no trailing whitespaces in the xml, rechecked everything.Even used tools like a text editor that can show non-printable characters, still no solution.
Now I am at my wits end about what the problem might be, please help.