我是 spring web 服务的新手,在为阶乘服务编写示例程序后,我有些疑惑。我认为这就是 spring 网络服务的工作方式:
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
现在我的疑惑是:
- 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?
- In this whole process when is wsdl constructed?
以下是 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>
最佳答案
XSD 不生成 xml,它用于验证它。它也被编写客户端的人用来了解如何形成他们的 xml 以发送到您的服务。 “请求”是由某种客户端发送到您的服务中的消息——它进入您的服务的方式通常是通过 http 协议(protocol)(万维网协议(protocol))。
您在您的代码中提到这意味着契约优先——这意味着您应该在执行任何其他操作之前编写 wsdl(尽管通常这是与描述接口(interface)的 xsd 一起完成的).然后可以使用 wsdl 和一些注释配置 Spring 以处理消息——您甚至可以使用 jaxb 自动绑定(bind)到代码中的 java 对象,这样您就不必手动解析传入的 xml 有效负载。
This is old ,但它遵循您正在使用的相同方法,甚至使用相同的已弃用的 spring 类。
现在很多开发人员都避开 WS-* 风格的网络服务,转而使用基于 REST 的网络服务,使用 spring-web 和 spring-mvc 可以很容易地实现这些服务,在 java pojo 上有几个简单的注释.如果您愿意,您甚至可以让 spring 自动将您的 xml 有效负载绑定(bind)到从 xsd 生成的 java 对象,这样您就不必在任何时候实际处理 XML。
关于java - Spring Web 服务流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16850741/