简体   繁体   English

Spring Web服务流程

[英]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 应用程序在服务器上运行并生成一个请求 - >请求转到web.xml中定义的调度程序servlet - > dispatcher servlet查找[servlet-name] -servlet.xml - > dispatcher servlet然后查找找到的payloadroot右端点 - > xml请求转到结束点 - >端点生成响应


Now my doubts are: 现在我的怀疑是:

  1. How does the request that comes to the endpoint comes in XML form? 到端点的请求是如何以XML格式提供的? I know XSD helps to create xml but when does it do that? 我知道XSD有助于创建xml但是什么时候这样做?
  2. In this whole process when is wsdl constructed? 在构建wsdl的整个过程中?

Following are the bean definitions ie : [servlet-name]-servlet.xml file: 以下是bean定义,即: [servlet-name]-servlet.xml文件:

<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. XSD不生成xml,它用于验证它。 It is also used by people writing clients to understand how to form their xml to send to your service. 编写客户的人也会使用它来了解如何形成他们的xml以发送到您的服务。 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). “请求”是由某种类型的客户端发送到您的服务中的消息 - 它通常是通过http协议(万维网协议)进入您的服务的。

  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). 您在代码中提到这意味着契约优先 - 这意味着您应该在执行任何其他操作之前编写wsdl(尽管通常这与描述接口的xsd一起完成)。 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. 然后可以使用wsdl和一些注释配置Spring以处理消息 - 您甚至可以使用jaxb直接绑定到代码中的java对象中,这样您就不必手动解析传入的xml有效负载。

This is old , but it follows the same approach you're using, and even uses the same deprecated spring classes. 这是旧的 ,但它遵循您使用的相同方法,甚至使用相同的已弃用的spring类。

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. 如今,很多开发人员都避免使用WS- *风格的Web服务,转而使用基于REST的Web服务,这些服务使用spring-web和spring-mvc非常容易实现,在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. 如果愿意,您甚至可以让spring自动将xml有效负载绑定到从xsd生成的java对象,这样您再也不必在任何时候实际处理XML。

  1. spring uses JAXB to serialize to xml and parse from request. spring使用JAXB序列化为xml并从请求中解析。
  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. 如果您正在使用JAX-WS ,则WSDL将在运行时生成(默认情况下),但也可以提供预生成的WSDL

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. 如果你看一下spring-ws-core maven依赖项,你会发现它依赖于spring-oxm (对xml < - >对象映射的抽象),它依赖于jaxb-api项目。

Take a closer look on what you're actually using in your dependencies. 仔细查看您在依赖项中实际使用的内容。 JAXB might come from the app-server lib/ folder. JAXB可能来自app-server lib /文件夹。

And the second point. 第二点。 JAXB is not only used to serialize to xml, it can deserialize from the xml as well. JAXB不仅用于序列化为xml,它还可以从xml反序列化。

All the rest calls go through DispatcherServlet and Spring soap webservice requests go via MessageDispatcher. 所有其余的调用都通过DispatcherServlet和Spring soap webservice请求通过MessageDispatcher进行。 If you put a debugger inside that class you will find the flow. 如果您将调试器放在该类中,您将找到该流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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