使用spring-ws进行webservice服务器端的开发

Spring Web Services offer another endpoint with which you can aggregate multiple handling into one
controller, thus grouping functionality together. This model is based on annotations, so you can use it only with
Java 5 and higher. Here is an example that uses the same marshalled objects as above:
package samples;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
@Endpoint
public class AnnotationOrderEndpoint {
    private final OrderService orderService;
    public AnnotationOrderEndpoint(OrderService orderService) {
    this.orderService = orderService;
    }
    @PayloadRoot(localPart = "orderRequest", namespace = "http://samples")
    public Order getOrder(OrderRequest orderRequest) {
    return orderService.getOrder(orderRequest.getId());
    }
    @PayloadRoot(localPart = "order", namespace = "http://samples")
    public void order(Order order) {
    orderService.createOrder(order);
    }
}
By annotating the class with @Endpoint, you mark it as a Spring-WS endpoint. Because the endpoint class can
have multiple request handling methods, we need to instruct Spring-WS which method to invoke for which
request. This is done using the @PayloadRoot annotation: the getOrder method will be invoked for requests
with a orderRequest local name and a http://samples namespace URI; the order method for requests with a
order local name. For more information about these annotations, refer to Section 5.4.3,
“MethodEndpointMapping”.


 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 <description>
  This web application context contains Spring-WS beans. The beans
  defined in this context are automatically detected by Spring-WS,
  similar to the way Controllers are picked up in Spring Web MVC.
 </description>

 <bean
  class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
  <description>
   Detects @PayloadRoot annotations on @Endpoint bean methods.
   The MarshallingAirlineEndpoint has such annotations. It uses
   two interceptors: one that logs the message payload, and the
   other validates it accoring to the 'airline.xsd' schema
   file.
  </description>
 </bean>
 <bean id="echo"
  class="org.springframework.ws.wsdl.wsdl11.DynamicWsdl11Definition">
  <description>
   This bean definition represents a WSDL definition that is
   generated at runtime, based on the builder defined below. It
   can be retrieved by going to /echo/echo.wsdl (i.e. the bean
   name corresponds to the filename).
  </description>
  <property name="builder">
   <description>
    The builder creates a WSDL from the 'echo.xsd' schema.
    It detects all elements that ends with 'Request', finds
    corresponding 'Response' messages, and creates an
    operation based on that. All operations are put in a
    portType with name 'Echo', and binding and service are
    created.
   </description>
   <bean
    class="org.springframework.ws.wsdl.wsdl11.builder.XsdBasedSoap11Wsdl4jDefinitionBuilder">
    <property name="schema" value="/WEB-INF/echo.xsd" />
    <property name="portTypeName" value="Echo" />
    <property name="locationUri"
     value="http://localhost:8080/echoweb/services" />
   </bean>
  </property>
 </bean>
 <bean id="echoEndpoint" class="com.vanad.EchoEndpoint">
  <description>This endpoint handles echo requests.</description>
  <property name="echoService" ref="echoService" />
 </bean>
 <bean id="echoService" class="com.vanad.EchoServiceImpl">
  <description>This bean is our "business" service.</description>
 </bean>

 <bean id="payloadLoggingInterceptor"
  class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor">
 </bean>

 <bean id="soapEnvelopeLoggingInterceptor"
  class="org.springframework.ws.soap.server.endpoint.interceptor.SoapEnvelopeLoggingInterceptor">
 </bean>
 <bean
  class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
  <constructor-arg ref="marshaller" />
 </bean>
 <bean id="marshaller"
  class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  <property name="contextPath" value="com.vanad.schema" />
 </bean>
</beans>


 

也就是说用spring开发webservice需要配置一下文件:
1.EndPoint,如果使用标记的话,就可以不用继承任何类
2.EndpointMapping,Detects @PayloadRoot annotations on @Endpoint bean methods.
3.DynamicWsdl11Definition。动态生成WSDL
如果使用JAXB作为Object和XML的映射,那么还需要
4.Marshaller一般为Jaxb2Marshaller
5.如果是使用的标记的话,还需要MarshallingMethodEndpointAdapter,一般现在使用
    GenericMarshallingMethodEndpointAdapter

posted on 2007-12-25 17:48 刘铮 阅读(6177) 评论(0)  编辑  收藏 所属分类: WebService


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论