首先,定義一個XSD(XML Schema Definition)來描述你的數據結構。在你的Maven項目的src/main/resources
目錄下,創建一個名為schemas
的文件夾,并在其中創建一個名為scriptService.xsd
的文件,內容如下:
scriptService.wsdl
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:tns="http://yournamespace.com"targetNamespace="http://yournamespace.com"><wsdl:types><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://yournamespace.com"xmlns="http://yournamespace.com"elementFormDefault="qualified"><!-- Import the XSD schema --><xs:import namespace="http://yournamespace.com" schemaLocation="scriptService.xsd"/><!-- Define input and output message elements --><xs:element name="ExecuteScriptRequest" type="tns:ScriptInfo"/><xs:element name="ExecuteScriptResponse" type="tns:ScriptResult"/></xs:schema></wsdl:types><!-- Define the portType --><wsdl:portType name="ScriptServicePortType"><wsdl:operation name="ExecuteScript"><wsdl:input message="tns:ExecuteScriptRequest"/><wsdl:output message="tns:ExecuteScriptResponse"/></wsdl:operation></wsdl:portType><!-- Define the binding --><wsdl:binding name="ScriptServiceBinding" type="tns:ScriptServicePortType"><wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="ExecuteScript"><wsdl:input><wsdlsoap:body use="literal"/></wsdl:input><wsdl:output><wsdlsoap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><!-- Define the service --><wsdl:service name="ScriptService"><wsdl:port name="ScriptServicePort" binding="tns:ScriptServiceBinding"><wsdlsoap:address location="http://your-service-endpoint"/></wsdl:port></wsdl:service>
</wsdl:definitions>
然后,創建一個名為scriptService.wsdl
的WSDL文件,也在schemas
文件夾中,內容如下:
scriptService.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://yournamespace.com"xmlns="http://yournamespace.com"elementFormDefault="qualified"><xs:complexType name="ScriptInfo"><xs:sequence><xs:element name="projectName" type="xs:string"/><xs:element name="scriptName" type="xs:string"/><xs:element name="args01" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="ScriptResult"><xs:sequence><xs:element name="executionId" type="xs:string"/><xs:element name="exitStatus" type="xs:int"/><xs:element name="timestamp" type="xs:dateTime"/></xs:sequence></xs:complexType><xs:element name="ScriptRequest" type="ScriptInfo"/><xs:element name="ScriptResponse" type="ScriptResult"/>
</xs:schema>
創建一個用于定義SOAP服務的Endpoint
類。這個類將會處理SOAP請求和響應。
SoapEndpoint.java
import org.springframework.ws.server.endpoint.annotation.*;@Endpoint
public class SoapEndpoint {private static final String NAMESPACE_URI = "http://yournamespace.com";@PayloadRoot(namespace = NAMESPACE_URI, localPart = "ExecuteScriptRequest")@ResponsePayloadpublic ExecuteScriptResponse executeScript(@RequestPayload ExecuteScriptRequest request) {// 處理請求并生成響應ExecuteScriptResponse response = new ExecuteScriptResponse();ScriptResult scriptResult = new ScriptResult();// 設置響應內容response.setScriptResult(scriptResult);return response;}
}
接下來,創建一個配置類,用于配置Spring Web Services。
Spring Web Services.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.soap.server.endpoint.interceptor.PayloadLoggingInterceptor;
import org.springframework.ws.transport.http.MessageDispatcherServlet;@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {@Beanpublic ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {MessageDispatcherServlet servlet = new MessageDispatcherServlet();servlet.setApplicationContext(applicationContext);servlet.setTransformWsdlLocations(true);return new ServletRegistrationBean<>(servlet, "/ws/*");}@Beanpublic PayloadLoggingInterceptor payloadLoggingInterceptor() {return new PayloadLoggingInterceptor();}
}
application.properties
spring.webservices.mapping.path=/ws
spring.webservices.servlet.init.wsdlPath=classpath:/schemas/scriptService.wsdl
application.yml
server:port: 8080spring:webservices:mapping:path: /wsservlet:init:wsdlPath: classpath:/schemas/scriptService.wsdl
在這個示例中,我們配置了服務器端口為8080,映射了SOAP服務的路徑為/ws
。同時,我們指定了WSDL文件的路徑為classpath:/schemas/scriptService.wsdl
,這意味著WSDL文件應該放在src/main/resources/schemas
目錄下。
如果你有其他需要配置的屬性,你可以在這個application.yml
文件中添加。記得根據你的實際項目情況來進行相應的配置。
請確保修改命名空間、路徑和其他屬性,以便與你的項目和數據結構匹配。配置完成后,當你啟動應用程序時,它將使用這些配置項來設置Spring Boot SOAP服務。