有没有办法将我的 SOAP Web 服务(spring-ws、java)用作虚拟的基于 XML 的 RESTful 服务?
我不想用 Java 从头开始将整个 SOAP Web 服务重写为 RESTful,但我需要使用 REST 通过 iPhone 访问它,他们已经有了简单的 native 支持。
XMLGateway、代理..?或者一些额外的java代码?由于我的 SOAP 请求和响应只是一个 XML 文件,为什么我不能修改它以供 REST 服务使用?
或者在我的应用程序中不更改任何逻辑和 xml 解析的情况下,添加 jax-rs 注释并创建休息请求 xml 是那么容易吗?
我的配置 spring 文件是这样的:
<bean id="webServicePluginDescriptor"
class="com.mysite.ws.configuration.MyWebservicePluginDescriptor" />
<bean id="payloadMapping"
class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="defaultEndpoint" ref="inferenceEndPoint" />
<property name="interceptors">
<list>
<ref local="validatingInterceptor" />
<ref local="payLoadInterceptor" />
</list>
</property>
</bean>
<bean id="payLoadInterceptor"
class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
<bean id="validatingInterceptor"
class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<property name="schema"
value="classpath:/wsdl/Request.xsd" />
<property name="validateRequest" value="true" />
<property name="validateResponse" value="false" />
</bean>
<bean id="PropertyResource" class="com.mysite.ws.im.PropertyResource">
<property name="resource"
value="/WEB-INF/client-specific/InferenceMachine.properties" />
</bean>
<bean id="inferenceEndPoint" class="com.mysite.ws.web.InferenceEndPoint">
<property name="messageWebService" ref="messageWebService" />
</bean>
<bean id="messageWebService" class="com.mysite.ws.service.MessageWebService"
scope="request">
<aop:scoped-proxy />
<property name="inferenceService" ref="inferenceService" />
</bean>
<bean id="Request" class="org.springframework.xml.xsd.SimpleXsdSchema">
<property name="xsd" value="classpath:/wsdl/Request.xsd" />
</bean>
<bean id="Response" class="org.springframework.xml.xsd.SimpleXsdSchema">
<property name="xsd" value="classpath:/wsdl/Response.xsd" />
</bean>
<bean id="Error" class="org.springframework.xml.xsd.SimpleXsdSchema">
<property name="xsd" value="classpath:/wsdl/Error.xsd" />
</bean>
<bean id="mwsid"
class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">
<constructor-arg value="classpath:/wsdl/mtchwsdl.wsdl" />
</bean>
<bean id="inferenceService" class="com.mysite.ws.im.InferenceService"
scope="request">
<aop:scoped-proxy />
<property name="webServiceConfiguration" ref="wsPlayerConfiguration" />
<property name="properties">
<bean class="com.mysite.ws.im.PropertyResource">
<property name="resource"
value="/WEB-INF/client-specific/InferenceMachine.properties" />
</bean>
</property>
</bean>
<!-- ~~~~~~~ Application beans ~~~~~~~ -->
<bean id="wsPlayerConfiguration"
class="com.mysite.ws.configuration.WebServiceConfiguration"
scope="request">
<aop:scoped-proxy />
<property name="playerConfiguration" ref="playerConfiguration"></property>
<property name="configurationSetup" ref="configurationSetup"></property>
</bean>
这是我的端点类:
/**
* The EndPoint of the Web Service Application. This class gets the raw
* SOAP-body message from the Spring Payload Dispatcher and sends the message to
* the @see MessageService class. After it has gotten the response XML message
* it returns this back to the Spring Payload Dispatcher.
*/
public class InferenceEndPoint extends AbstractJDomPayloadEndpoint {
private MessageWebService messageWebService;
public InferenceEndPoint() {
}
@Override
protected Element invokeInternal(Element inferenceRequest) throws Exception {
Element ret = messageWebService.handleRequest(inferenceRequest);
return ret;
}
/**
* @param messageWebService
*/
public void setMessageWebService(MessageWebService messageWebService) {
this.messageWebService = messageWebService;
}
}
有什么想法吗?
最佳答案
Spring-WS 只是向您的 bean 添加一些注释,然后由 Spring bean 完成大部分繁重的工作。假设您有一些用 @Endpoint、@PayloadRoot 等注释的类。您应该能够通过三种方式之一重用所有这些。
如果您的 Spring-WS 端点类是适配器模式样式(例如,您的端点类注入(inject)了执行实际工作的 POJO 服务),那么您可以执行类似的适配器样式 Spring MVC Controller (其中 Spring 中存在 REST) 3.0)。
如果您的注释直接在业务逻辑类上,那么理论上,您应该能够添加更多注释(可能看起来有点忙)。
如果您有 POX(而不是 SOAP)的 Spring-WS bean,那么您也许可以使用一些奇特的 URL 映射来为它们提供看起来更 RESTful 的 URL
要迁移到 Spring 3 以获取 REST 支持,请添加适当的 @RequestMapping 和其他注释,将它们公开为 REST 服务以匹配特定的 URL。在添加时,您也可以删除旧的 @PayloadRoots 和 @Endpoint,但这可能不是什么大问题。当然,如果您保留旧的 Spring-WS 注释,您的类路径中仍然需要 Spring-WS jar,但只要您不使用 Spring-WS servlet 或 Spring 文件中的任何其他 bean - 您应该没问题(理论上......)。
最大的问题是:
- 不要忘记从 Spring 文件中删除 Spring-WS bean
- 请记住将 Spring MVC bean 添加到 Spring 文件中,最重要的是不同的 Dispatcher servlet
- Spring 中的 REST 安全性将由 Spring Security 提供,而不是 Spring-WS 中的 SOAP 拦截器,因此这将是一次彻底的改革。好消息是 Spring Security 实际上非常容易使用
关于java - 将 SOAP 转换为虚拟 Restful 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6405517/