Spring Web Service 学习之Hello World篇2

本文介绍如何使用Spring WS搭建SOAP Web服务,包括配置文件spring-ws-servlet.xml的详细解析及客户端实现。重点讲解了payloadMapping配置及客户端SOAP消息构造。

6, 修改配置文件spring-ws-servlet.xml.

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.   
  6.      <bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">  
  7.          <property name="endpointMap">  
  8.              <map>  
  9.                  <entry key=”{http://www.fuxueliang.com/ws/hello}HelloRequest” />        
  10.                      <ref bean="helloEndpoint" />  
  11.                  </entry>  
  12.              </map>  
  13.          </property>  
  14.      </bean>  
  15.      <bean id="hello" class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">  
  16.          <property name="wsdl" value="/WEB-INF/hello.wsdl"/>  
  17.      </bean>  
  18.      <bean id="helloEndpoint" class="org.rondy.ws.HelloEndPoint">  
  19.          <property name="helloService" ref="helloService" />  
  20.      </bean>  
  21.      <bean id="helloService" class="org.rondy.service.HelloServiceImpl" />  
  22. </beans>  
<?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">   <bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">   <property name="endpointMap">    <map>     <entry key=”{http://www.fuxueliang.com/ws/hello}HelloRequest” />        <ref bean="helloEndpoint" />     </entry>    </map>   </property>  </bean>  <bean id="hello" class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">   <property name="wsdl" value="/WEB-INF/hello.wsdl"/>  </bean>  <bean id="helloEndpoint" class="org.rondy.ws.HelloEndPoint">   <property name="helloService" ref="helloService" />  </bean>  <bean id="helloService" class="org.rondy.service.HelloServiceImpl" /> </beans>

: 其中最主要的bean就是payloadMapping, 它定义了接收到的messageendpoint之间的mapping关系:SOAP Body中包含的xml的根节点的QName{http://www.fuxueliang.com/ws/hello}HelloRequest交给helloEndpoint处理.
SimpleWsdl11Definition这个bean则是定义了这个服务的wsdl, 访问地址是:http://localhost:8080/springws/hello.wsdl.

7, 客户端(saaj实现)的代码如下:

Java代码 复制代码
  1. /**
  2. *
  3. * @author Rondy.F
  4. *
  5. */  
  6. public class HelloWebServiceClient {  
  7.   
  8.     public static final String NAMESPACE_URI = "http://www.fuxueliang.com/ws/hello";  
  9.   
  10.     public static final String PREFIX = "tns";  
  11.   
  12.     private SOAPConnectionFactory connectionFactory;  
  13.   
  14.     private MessageFactory messageFactory;  
  15.   
  16.     private URL url;  
  17.   
  18.     public HelloWebServiceClient(String url) throws SOAPException, MalformedURLException {  
  19.          connectionFactory = SOAPConnectionFactory.newInstance();  
  20.          messageFactory = MessageFactory.newInstance();  
  21.         this.url = new URL(url);  
  22.      }  
  23.   
  24.     private SOAPMessage createHelloRequest() throws SOAPException {  
  25.          SOAPMessage message = messageFactory.createMessage();  
  26.          SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();  
  27.          Name helloRequestName = envelope.createName("HelloRequest", PREFIX, NAMESPACE_URI);  
  28.          SOAPBodyElement helloRequestElement = message.getSOAPBody().addBodyElement(helloRequestName);  
  29.          helloRequestElement.setValue("Rondy.F");  
  30.         return message;  
  31.      }  
  32.   
  33.     public void callWebService() throws SOAPException, IOException {  
  34.          SOAPMessage request = createHelloRequest();  
  35.          SOAPConnection connection = connectionFactory.createConnection();  
  36.          SOAPMessage response = connection.call(request, url);  
  37.         if (!response.getSOAPBody().hasFault()) {  
  38.              writeHelloResponse(response);  
  39.          } else {  
  40.              SOAPFault fault = response.getSOAPBody().getFault();  
  41.              System.err.println("Received SOAP Fault");  
  42.              System.err.println("SOAP Fault Code :" + fault.getFaultCode());  
  43.              System.err.println("SOAP Fault String :" + fault.getFaultString());  
  44.          }  
  45.      }  
  46.   
  47.     @SuppressWarnings("unchecked")  
  48.     private void writeHelloResponse(SOAPMessage message) throws SOAPException {  
  49.          SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();  
  50.          Name helloResponseName = envelope.createName("HelloResponse", PREFIX, NAMESPACE_URI);  
  51.          Iterator childElements = message.getSOAPBody().getChildElements(helloResponseName);  
  52.          SOAPBodyElement helloResponseElement = (SOAPBodyElement) childElements.next();  
  53.          String value = helloResponseElement.getTextContent();  
  54.          System.out.println("Hello Response [" + value + "]");  
  55.      }  
  56.   
  57.     public static void main(String[] args) throws Exception {  
  58.          String url = "http://localhost:8080/springws";  
  59.          HelloWebServiceClient helloClient = new HelloWebServiceClient(url);  
  60.          helloClient.callWebService();  
  61.      }  
  62. }  
/**  *   * @author Rondy.F  *   */ public class HelloWebServiceClient {   public static final String NAMESPACE_URI = "http://www.fuxueliang.com/ws/hello";   public static final String PREFIX = "tns";   private SOAPConnectionFactory connectionFactory;   private MessageFactory messageFactory;   private URL url;   public HelloWebServiceClient(String url) throws SOAPException, MalformedURLException {   connectionFactory = SOAPConnectionFactory.newInstance();   messageFactory = MessageFactory.newInstance();   this.url = new URL(url);  }   private SOAPMessage createHelloRequest() throws SOAPException {   SOAPMessage message = messageFactory.createMessage();   SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();   Name helloRequestName = envelope.createName("HelloRequest", PREFIX, NAMESPACE_URI);   SOAPBodyElement helloRequestElement = message.getSOAPBody().addBodyElement(helloRequestName);   helloRequestElement.setValue("Rondy.F");   return message;  }   public void callWebService() throws SOAPException, IOException {   SOAPMessage request = createHelloRequest();   SOAPConnection connection = connectionFactory.createConnection();   SOAPMessage response = connection.call(request, url);   if (!response.getSOAPBody().hasFault()) {    writeHelloResponse(response);   } else {    SOAPFault fault = response.getSOAPBody().getFault();    System.err.println("Received SOAP Fault");    System.err.println("SOAP Fault Code :" + fault.getFaultCode());    System.err.println("SOAP Fault String :" + fault.getFaultString());   }  }   @SuppressWarnings("unchecked")  private void writeHelloResponse(SOAPMessage message) throws SOAPException {   SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();   Name helloResponseName = envelope.createName("HelloResponse", PREFIX, NAMESPACE_URI);   Iterator childElements = message.getSOAPBody().getChildElements(helloResponseName);   SOAPBodyElement helloResponseElement = (SOAPBodyElement) childElements.next();   String value = helloResponseElement.getTextContent();   System.out.println("Hello Response [" + value + "]");  }   public static void main(String[] args) throws Exception {   String url = "http://localhost:8080/springws";   HelloWebServiceClient helloClient = new HelloWebServiceClient(url);   helloClient.callWebService();  } }

几点看法:

1, 从上面代码可以看出, 比较麻烦的部分就是客户端和服务端对xml处理

转载于:https://www.cnblogs.com/earl86/archive/2008/11/03/1666483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值