Java SOAP Web服务示例:服务端和客户端实现

本教程展示了使用Java创建SOAP Web服务的步骤,包括服务端和客户端实现。通过示例代码,您将了解如何构建SOAP请求,解析SOAP响应以及如何测试服务。

协议和标准

  • SOAP协议版本: 1.1、1.2
  • 传输层协议: HTTP
  • 安全标准: Web Service的安全策略
  • 方法名: 'ckts_bgdInfo'
  • 命名空间: 'http://ws.dc.com'
  • 接口URL自定义, 例如 'http://ip:port/services/recv?wsdl'

示例代码

服务端代码

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class SoapService {
    
    @WebMethod
    public String ckts_bgdInfo(String xml) {
        // 处理接收到的报文
        return "处理结果";
    }
    
    public static void main(String[] args) {
        String url = "http://localhost:8080/soap";
        Endpoint.publish(url, new SoapService());
        System.out.println("SOAP服务已启动,URL:" + url);
    }
}

客户端代码

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class SoapClient {
    
    public static void main(String[] args) {
        try {
            String endpoint = "http://localhost:8080/soap?wsdl";
            String namespace = "http://ws.dc.com";
            String methodName = "ckts_bgdInfo";
            String xml = "报文";
            
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            
            SOAPMessage soapMessage = createSOAPRequest(namespace, methodName, xml);
            
            SOAPMessage soapResponse = soapConnection.call(soapMessage, endpoint);
            
            String result = parseSOAPResponse(soapResponse);
            
            System.out.println("服务端返回结果:" + result);
            
            soapConnection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static SOAPMessage createSOAPRequest(String namespace, String methodName, String xml) throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        
        SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();
        envelope.addNamespaceDeclaration("ws", namespace);
        
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapElement = soapBody.addChildElement(methodName);
        SOAPElement xmlElement = soapElement.addChildElement("xml");
        xmlElement.addTextNode(xml);
        
        soapMessage.saveChanges();
        
        System.out.println("发送的SOAP请求:");
        soapMessage.writeTo(System.out);
        System.out.println();
        
        return soapMessage;
    }
    
    private static String parseSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        
        DOMSource source = new DOMSource(soapResponse.getSOAPPart().getEnvelope().getBody().getFirstChild());
        StreamResult result = new StreamResult(System.out);
        
        System.out.println("接收到的SOAP响应:");
        transformer.transform(source, result);
        System.out.println();
        
        SOAPBody soapBody = soapResponse.getSOAPBody();
        SOAPElement responseElement = (SOAPElement) soapBody.getChildElements().next();
        SOAPElement resultElement = (SOAPElement) responseElement.getChildElements(new QName("return")).next();
        
        return resultElement.getTextContent();
    }
}

注意事项

  • 替换服务端和客户端代码中的URL和命名空间
  • 确保在运行客户端代码之前,服务端已经启动

总结

本示例代码演示了如何使用Java创建SOAP请求和处理SOAP响应。您可以根据自己的需求进行修改和扩展。

Java SOAP Web服务示例:服务端和客户端实现

原文地址: https://www.cveoy.top/t/topic/S2j 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录