SOAP(Simple Object Access Protocol)和REST(Representational State Transfer)是两种不同的Web服务通信协议,因此它们的接口工具类实现也有一些差异。
以下是一个简单的Java代码实现SOAP接口工具类的示例:
import javax.xml.namespace.QName;
import javax.xml.soap.*;
public class SoapClientUtil {
private final String endpoint;
private final String namespace;
private boolean validate = false;
public SoapClientUtil(String endpoint, String namespace) {
this.endpoint = endpoint;
this.namespace = namespace;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public SOAPMessage sendMessage(String methodName, Object... params) throws Exception {
// Create a SOAP message
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage soapMessage = factory.createMessage();
// Add SOAP envelope, header, and body elements
SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();
if (validate) {
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
envelope.addNamespaceDeclaration("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
envelope.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "noNamespaceSchemaLocation", "uss.xsd");
}
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();
// Create the SOAP body message
QName bodyName = new QName(namespace, methodName);
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
if (params != null) {
for (Object param : params) {
QName paramName = new QName(param.getClass().getName());
SOAPElement soapElement = bodyElement.addChildElement(paramName);
soapElement.setTextContent(param.toString());
}
}
// Send the SOAP message to the endpoint and receive a response
SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = connectionFactory.createConnection();
SOAPMessage response = connection.call(soapMessage, endpoint);
connection.close();
return response;
}
}
以下是一个简单的Java代码实现REST接口工具类的示例:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class RestClientUtil {
private final String endpoint;
public RestClientUtil(String endpoint) {
this.endpoint = endpoint;
}
public String sendRequest(String method, String... params) throws Exception {
// Build the request URL
StringBuilder urlBuilder = new StringBuilder(endpoint);
if (params != null && params.length > 0) {
urlBuilder.append("?");
for (int i = 0; i < params.length; i += 2) {
urlBuilder.append(params[i]);
urlBuilder.append("=");
urlBuilder.append(params[i+1]);
if (i < params.length - 2) {
urlBuilder.append("&");
}
}
}
URL url = new URL(urlBuilder.toString());
// Open a connection using the specified HTTP method
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
// Read the response from the server
StringBuilder response = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
}
rd.close();
// Close the HTTP connection
conn.disconnect();
return response.toString();
}
}
需要注意的是,这些示例代码只实现了最基本的功能,实际使用时可能需要根据具体情况进行修改和扩展。同时还需要注意安全性和性能等方面的问题,例如SSL证书验证、连接池管理和结果缓存等。