我有一些crm软件,我需要集成太,只能提供在soap接口。这过去很好,但最近的情况证明,客户机很难做到这一点,因为大多数客户机已经转向rest体系结构。服务规模很大,我们无法在其他地方重新构建支持rest的逻辑平台。还有很多服务。
我想做并且已经开始做的是创建一个springboot微服务,它接受json格式的请求并将其封送到soap中,然后将其传递给crm软件,最后将soap响应转换回json以返回到客户端。由于对象太多,我不能采用传统的jaxb方法生成soap对象,然后将其转换为json对象,然后返回。这太难维持了。
下面是以原始格式处理请求/响应的方法的开始。我还没有找到任何直接的spring方法来从一个转换到另一个,所以我开始使用saaj。有了这个方法,我可以看到我需要做很多事情。我要找的是一个快速的回顾,说我要对它的wrtie方式和任何建议或功能需要实施或更好,如果有其他方式。
抱歉格式化,这是我的第一篇帖子。控制器从下面开始。
'''
package com.smcgow.crmapi.controller;
import javax.xml.soap.*;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
@RestController
@RequestMapping("/crm")
@Slf4j
public class CrmApiController {
public static final String CONTENT_TYPE = "content-type";
@GetMapping("/hello")
public String hello(){
return "hello";
}
@PostMapping(value = "/api/{operationName}", consumes = {APPLICATION_JSON_VALUE,APPLICATION_XML_VALUE}, produces = APPLICATION_XML_VALUE)
public String postToCrmApi(@PathVariable(name = "operationName") String operationName, HttpServletRequest request) throws IOException {
JSONObject jsonResponseObject = null;
SOAPConnection connection = null;
//Default server error on exception.
String jsonResponse =
"{\"code\" : \"500\", \"message\" : \"Internal server error\"}";
//Default
String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"+
"<soapenv:Body>%s</soapenv:Body>" +
"</soapenv:Envelope>";
//Use operationName to build url.this is dummied at the minute.
String webServiceUrl = "https://www.ebi.ac.uk/europepmc/webservices/soap";
log.info("The param value is " + operationName);
try {
//1. Take servlet random servlet json input, change to a string and make a Json object.
String requestBodyJson = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);
log.info("Greet incoming in json is : " + requestBodyJson);
JSONObject json = new JSONObject(requestBodyJson);
Enumeration<String> parameterNames = request.getParameterNames();
if(parameterNames.hasMoreElements()){
JSONObject parameters = new JSONObject();
json.put("requestParameters", parameters);
while(parameterNames.hasMoreElements()){
String parameterName = parameterNames.nextElement();
parameters.put(parameterName,request.getParameter(parameterName));
}
}
//2. Convert to XML and wrap in SOAP envelope. Print results
String xml = XML.toString(json);
log.info("Greet incoming in xml is : "+ xml);
soap = String .format(soap,xml);
log.info("Greet incoming in soap is : "+ soap);
log.info("Sys property is : " + System.getProperty("javax.xml.soap.MetaFactory"));
//Must have property -Djavax.xml.soap.MetaFactory=com.sun.xml.messaging.saaj.soap.SAAJMetaFactoryImpl
//3. Ceate SAAJ soap message with soap envelope names spaces
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
//3.1 Drive these based on operation name from properties
soapEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
soapEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
//4. Convert the soap request string to a w3c document and append as a Soap Part to the message.
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(soap));
Document document = db.parse(is);
DOMSource domSource = new DOMSource(document);
SOAPPart part = soapMessage.getSOAPPart();
part.setContent(domSource);
soapMessage.saveChanges();
//5 Create the connection and make the call.
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
connection = soapConnectionFactory.createConnection();
URL endpoint = new URL(webServiceUrl);
SOAPMessage response = connection.call(soapMessage, endpoint);
connection.close();
//6. Get the reposnes and convert it back to a string,
SOAPBody responseBody = response.getSOAPBody();
DOMSource source = new DOMSource(responseBody);
StringWriter stringResult = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(source, new StreamResult(stringResult));
String xmlResponse = stringResult.toString();
//7. Convert to a json response string.
jsonResponseObject = XML.toJSONObject(xmlResponse);
jsonResponse = jsonResponseObject.toString(4);
} catch (SOAPException | ParserConfigurationException | SAXException | TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} finally {
}
String xmlResponse;
return jsonResponse;
}
}
'''
暂无答案!
目前还没有任何答案,快来回答吧!