SoFunction
Updated on 2025-03-08

How to publish a webservice service using spring-ws

spring-ws

Add dependencies, plug-ins

existAdd to

        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>
        <!-- /artifact/wsdl4j/wsdl4j -->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <!-- /artifact//jaxb-api -->
        <dependency>
            <groupId></groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
&lt;!--            Open the following plug-in when you want to use it, No need to remember to comment--&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;&lt;/groupId&gt;
                &lt;artifactId&gt;jaxb2-maven-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.1.0&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;xjc&lt;/id&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;xjc&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                    &lt;sources&gt;
                        &lt;source&gt;${}/src/main/resources/xsd/&lt;/source&gt;
                    &lt;/sources&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;

Create XSD file

Comparing the actual input and output reference modification xsd

Actual input, output input

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:xsd="http:///2001/XMLSchema" xmlns:soapenv="/soap/envelope/" xmlns:snr="snr">
<soapenv:Header/>
<soapenv:Body>
<snr:bindRequest soapenv:encodingStyle="/soap/encoding/">
<RequestInfo xsi:type="soapenc:string" xmlns:soapenc="/soap/encoding/">
<Root>
<Domain>host</Domain>
<Passwd>pasdasjidojoi</Passwd>
<SrvCode>489489489445645648</SrvCode>
<Content>
<![CDATA[<?xml version='1.0' encoding='UTF-8'?><FakePassQuery><FakeCode>admin</FakeCode><Password><![CDATA[dsaiodas54545]]]]><![CDATA[></Password><FakeType>1000</FakeType></FakePassQuery>]]>
</Content>
</Root>
</RequestInfo></snr:bindRequest></soapenv:Body></soapenv:Envelope>

Output

<SOAP-ENV:Envelope xmlns:SOAP-ENV="/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <FakePassQueryResponse>
            <ExchangeId>a79bc02ea21a4a13a7c58108cc864a9d</ExchangeId>
            <ErrorCode>00000</ErrorCode>
            <IsSuccess>T</IsSuccess>
            <FakeContent>
                <FakeId>1</FakeId>
                <FakeCode>admin</FakeCode>
                <StaffName>admin</StaffName>
                <OrgId/>
                <EffDate>2024-01-01 00:00:00</EffDate>
                <ExpDate>2125-01-08 13:28:52</ExpDate>
                <StatusCd>0</StatusCd>
                <ContactTel/>
                <SmsTel/>
            </FakeContent>
        </FakePassQueryResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

xsd

<xs:schema xmlns:xs="http:///2001/XMLSchema" xmlns:tns="/guides/gs-producing-web-service"
           targetNamespace="/guides/gs-producing-web-service" elementFormDefault="qualified">
    <xs:element name="bindRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="RequestInfo" type="tns:RequestInfo"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="RequestInfo">
        <xs:sequence>
            <xs:element name="Root" type="tns:Root"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Root">
        <xs:sequence>
            <xs:element name="Domain" type="xs:string"/>
            <xs:element name="Passwd" type="xs:string"/>
            <xs:element name="SrvCode" type="xs:string"/>
            <xs:element name="Content" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="FakePassQuery">
        <xs:sequence>
            <xs:element name="FakeCode" type="xs:string"/>
            <xs:element name="Password" type="xs:string"/>
            <xs:element name="FakeType" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="FakePassQueryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="ErrorInfo" type="xs:string"/>
                <xs:element name="ResultCode" type="xs:string"/>
                <xs:element name="ExchangeId" type="xs:string"/>
                <xs:element name="ErrorCode" type="xs:string"/>
                <xs:element name="IsSuccess" type="xs:string"/>
                <xs:element name="FakeContent" type="tns:FakeContent"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="FakeContent">
        <xs:sequence>
            <xs:element name="FakeId" type="xs:string"/>
            <xs:element name="FakeCode" type="xs:string"/>
            <xs:element name="StaffName" type="xs:string"/>
            <xs:element name="OrgId" type="xs:string"/>
            <xs:element name="EffDate" type="xs:string"/>
            <xs:element name="ExpDate" type="xs:string"/>
            <xs:element name="StatusCd" type="xs:string"/>
            <xs:element name="ContactTel" type="xs:string"/>
            <xs:element name="SmsTel" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Generate xsd entity

idea -> maven -> Plugin -> jaxb2 -> jaxb2:xjc
The generated file is:target\generated-sources\jaxb

Configuration

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean&lt;MessageDispatcherServlet&gt; messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        (applicationContext);
        (true);
        return new ServletRegistrationBean&lt;&gt;(servlet, "/ws/*");
    }
    // The BeanName here is the actual access path, the current service request path: ip:port/ws/fakeBindUrl    @Bean(name = "fakeBindUrl")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema subAcctInfoForSelfBindSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        ("CountriesPort");
        ("/ws");
        ("snr");
        (subAcctInfoForSelfBindSchema);
        return wsdl11Definition;
    }
    @Bean
    public XsdSchema subAcctInfoForSelfBindSchema() {
        return new SimpleXsdSchema(new ClassPathResource("xsd/"));
    }
    // Multiple webservice services will register multiple wsdl11Definition and XsdSchema    // The BeanName here is the actual access path, and the current one is /ws/fakeBindUrl2    // @Bean(name = "fakeBindUrl2")
    // public DefaultWsdl11Definition defaultWsdl11Definition2(XsdSchema subAcctInfoForSelfBindSchema2) {
    // 	DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    // 	("CountriesPort");
    // 	("/ws");
    // 	("snr");
    // 	(subAcctInfoForSelfBindSchema);
    // 	return wsdl11Definition;
    // }
    // @Bean
    // public XsdSchema subAcctInfoForSelfBindSchema2() {
    // 	return new SimpleXsdSchema(new ClassPathResource("xsd/"));
    // }
}

Define Endpoint

@Endpoint
@Slf4j
public class bindRequestEndpoint {
    private static final String NAMESPACE_URI = "snr";
    @Autowired
    private SubAcctBindingService subAcctBindingService;
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "bindRequest")
    @ResponsePayload
    public FakePassQueryResponse fakeBindUrl(@RequestPayload bindRequest requestInfo) {
        try {
            ("Received a request: {}", requestInfo);
            // parse requestInfo's XML content for business processing            FakePassQuery FakePassQuery = parseInnerXML(().getRoot().getContent());
            FakePassQueryResponse result = (FakePassQuery);
            return result;
        } catch (Exception e) {
            ("Exception occurred while processing the request: ", e);
            // Assembly error response            FakePassQueryResponse response = new FakePassQueryResponse();
            (());
            ("-1");
            ("F");
            return response;
        }
    }
}

Start the service

The IP port number of the current service
ip:port/ws/fakeBindUrl?wsdl

other

If an error occurs, you can try adding namespace to the entry parameter

@XmlRootElement(namespace="", ...)

1 counts of IllegalAnnotationExceptions

There is a problem with the definition of the incoming or outgoing parameters
@XmlTypeofpropOrderNot consistent with the actual attributes, etc.

This is the end of this article about using spring-ws to publish webservice services. For more related Spring AI Alibaba Bailian Platform big model content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!