Spring Web Service 学习之Hello World篇2

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>  

: 其中最主要的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. }  

几点看法:

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

posted @ 2008-11-03 15:32  Earl_86  阅读(614)  评论(0编辑  收藏  举报