简体   繁体   中英

Spring web services flow

I am new to spring web services and after writing a sample program for a factorial service I am left with some doubts. I think this is how spring web-services work:


Application run on server and generates a request --> Request goes to dispatcher servlet as defined in web.xml --> dispatcher servlet looks for [servlet-name]-servlet.xml --> dispatcher servlet then looks for payloadroot which finds the right endpoint --> the xml request goes to the end point --> response is generated by the endpoint


Now my doubts are:

  1. How does the request that comes to the endpoint comes in XML form? I know XSD helps to create xml but when does it do that?
  2. In this whole process when is wsdl constructed?

Following are the bean definitions ie : [servlet-name]-servlet.xml file:

<beans ...>
    <bean id="findFactorialService" class="springws.findFactorial.FindFactorialServiceImpl"/>

    <bean id="findFactorialServiceEndpoint" class="springws.findFactorial.endpoint.FindFactorialServiceEndpoint">
        <property name="findFactorialService" ref="findFactorialService" />
    </bean>

    <bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
            <property name="defaultEndpoint" ref="findFactorialServiceEndpoint" />
        </bean>

        <bean id="findFactorialSchema" class="org.springframework.xml.xsd.SimpleXsdSchema">
            <property name="xsd" value="/WEB-INF/findFactorialService.xsd"  />
        </bean>

        <bean id="findFactorial" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
            <property name="schema" ref="findFactorialSchema" />
            <property name="portTypeName" value="hello" />
            <property name="locationUri" value="http://localhost:7070/find-factorial-using-contractfirst/services" />
        </bean>
    </beans>
  1. The XSD doesn't generate xml, it is used to validate it. It is also used by people writing clients to understand how to form their xml to send to your service. A 'request' is a message sent into your service by a client of some sort -- how it gets into your service is, usually, via the http protocol (the protocol of the world-wide-web).

  2. You mention in your code that this is meant to be contract-first -- which means that you should write the wsdl before you do anything else (although typically this is done in conjunction with the xsd that describes the interface). Spring can then be configured with the wsdl and some annotations in order to process the message -- you can even bind automatically, using jaxb, directly into java objects in your code so that you don't have to manually parse the incoming xml payload.

This is old , but it follows the same approach you're using, and even uses the same deprecated spring classes.

A lot of developers these days shun WS-* style web-services in favor of REST based web-services, which are realized very easily using spring-web and spring-mvc, with a couple of simple annotations on a java pojo. You can even have spring automatically bind your xml payload to java objects generated from the xsd, if you wish, so that again you don't have to actually deal with the XML at any point.

  1. spring uses JAXB to serialize to xml and parse from request.
  2. if you're using the JAX-WS , the WSDL will be generated at runtime (by default), but pre-generated WSDL can be also provided.

To address your comment:

If you take a look at spring-ws-core maven dependencies, you will observe that it has a dependency to spring-oxm (an abstraction over xml<-> object mappings), which has a dependency on jaxb-api project.

Take a closer look on what you're actually using in your dependencies. JAXB might come from the app-server lib/ folder.

And the second point. JAXB is not only used to serialize to xml, it can deserialize from the xml as well.

All the rest calls go through DispatcherServlet and Spring soap webservice requests go via MessageDispatcher. If you put a debugger inside that class you will find the flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM