Spring Web Service自带Demo浅析(一)

来源:互联网 发布:xampp mysql 编辑:程序博客网 时间:2024/05/17 09:18

下载资源包:http://www.springsource.org/download

Spring Web Service被设计成契约式开发模式,他依据自顶向下的设计方式(XML/XSD to JAVA),不同于XFire。

如下图所示建立工程:

代码如下:

EchoClient.java

  1. package org.springframework.ws.samples.echo.client.sws;
  2. import java.io.IOException;
  3. import javax.xml.transform.Source;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import org.springframework.core.io.Resource;
  7. import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
  8. import org.springframework.xml.transform.ResourceSource;
  9. import org.springframework.xml.transform.StringResult;
  10. public class EchoClient extends WebServiceGatewaySupport {
  11.     private Resource request;
  12.     public void setRequest(Resource request) {
  13.         this.request = request;
  14.     }
  15.     public void echo() throws IOException {
  16.         Source requestSource = new ResourceSource(request);
  17.         StringResult result = new StringResult();
  18.         getWebServiceTemplate().sendSourceAndReceiveToResult(requestSource, result);
  19.         System.out.println(result);
  20.     }
  21.     public static void main(String[] args) throws IOException {
  22.         ApplicationContext applicationContext =
  23.                 new ClassPathXmlApplicationContext("applicationContext.xml", EchoClient.class);
  24.         EchoClient echoClient = (EchoClient) applicationContext.getBean("echoClient");
  25.         echoClient.echo();
  26.     }
  27. }

applicationContext.xml

  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="echoClient" class="org.springframework.ws.samples.echo.client.sws.EchoClient">
  7.         <property name="defaultUri" value="http://localhost:8080/echo/services" />
  8.         <property name="request" value="classpath:org/springframework/ws/samples/echo/client/sws/echoRequest.xml" />
  9.     </bean>
  10.     
  11. </beans>

echoRequest.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <echoRequest xmlns="http://www.springframework.org/spring-ws/samples/echo">Hello</echoRequest>

EchoServiceImpl.java

  1. package org.springframework.ws.samples.echo.service.impl;
  2. import org.springframework.ws.samples.echo.service.EchoService;
  3. public class EchoServiceImpl implements EchoService {
  4.     public String echo(String s) {
  5.         return s;
  6.     }
  7. }

EchoService.java

  1. package org.springframework.ws.samples.echo.service;
  2. public interface EchoService {
  3.     String echo(String s);
  4. }

EchoEndpoint.java

  1. package org.springframework.ws.samples.echo.ws;
  2. import org.springframework.util.Assert;
  3. import org.springframework.ws.samples.echo.service.EchoService;
  4. import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
  5. import org.w3c.dom.Document;
  6. import org.w3c.dom.Element;
  7. import org.w3c.dom.Node;
  8. import org.w3c.dom.NodeList;
  9. import org.w3c.dom.Text;
  10. public class EchoEndpoint extends AbstractDomPayloadEndpoint {
  11.     public static final String NAMESPACE_URI = "http://www.springframework.org/spring-ws/samples/echo";
  12.     public static final String ECHO_REQUEST_LOCAL_NAME = "echoRequest";
  13.     public static final String ECHO_RESPONSE_LOCAL_NAME = "echoResponse";
  14.     private EchoService echoService;
  15.     public void setEchoService(EchoService echoService) {
  16.         this.echoService = echoService;
  17.     }
  18.     protected Element invokeInternal(Element requestElement, Document document) throws Exception {
  19.         
  20.         Assert.isTrue(NAMESPACE_URI.equals(requestElement.getNamespaceURI()), "Invalid namespace");
  21.         Assert.isTrue(ECHO_REQUEST_LOCAL_NAME.equals(requestElement.getLocalName()), "Invalid local name");
  22.         NodeList children = requestElement.getChildNodes();
  23.         Text requestText = null;
  24.         for (int i = 0; i < children.getLength(); i++) {
  25.             if (children.item(i).getNodeType() == Node.TEXT_NODE) {
  26.                 requestText = (Text) children.item(i);
  27.                 break;
  28.             }
  29.         }
  30.         if (requestText == null) {
  31.             throw new IllegalArgumentException("Could not find request text node");
  32.         }
  33.         String echo = echoService.echo(requestText.getNodeValue());
  34.         Element responseElement = document.createElementNS(NAMESPACE_URI, ECHO_RESPONSE_LOCAL_NAME);
  35.         Text responseText = document.createTextNode(echo);
  36.         responseElement.appendChild(responseText);
  37.         return responseElement;
  38.     }
  39. }

echo.xsd

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema"
  3.     elementFormDefault="qualified" attributeFormDefault="qualified"
  4.     targetNamespace="http://www.springframework.org/spring-ws/samples/echo"
  5.     xmlns:tns="http://www.springframework.org/spring-ws/samples/echo">
  6.     <element name="echoRequest">
  7.         <simpleType>
  8.             <restriction base="string">
  9.                 <pattern value="([A-Z]|[a-z])+" />
  10.             </restriction>
  11.         </simpleType>
  12.     </element>
  13.     <element name="echoResponse" type="string" />
  14.     
  15. </schema>

spring-ws-servlet.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  4.     <description>
  5.         This web application context contains Spring-WS beans. The beans defined in this context are automatically
  6.         detected by Spring-WS, similar to the way Controllers are picked up in Spring Web MVC.
  7.     </description>
  8.     <bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
  9.         <description>
  10.             This endpoint mapping uses the qualified name of the payload (body contents) to determine the endpoint for
  11.             an incoming message. Every message is passed to the default endpoint. Additionally, messages are logged
  12.             using the logging interceptor.
  13.         </description>
  14.         <property name="defaultEndpoint" ref="echoEndpoint"/>
  15.         <property name="interceptors">
  16.             <list>
  17.                 <ref local="validatingInterceptor"/>
  18.                 <ref local="loggingInterceptor"/>
  19.             </list>
  20.         </property>
  21.     </bean>
  22.     <bean id="validatingInterceptor"
  23.           class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
  24.         <description>
  25.             This interceptor validates both incoming and outgoing message contents according to the 'echo.xsd' XML
  26.             Schema file.
  27.         </description>
  28.         <property name="xsdSchema" ref="schema"/>
  29.         <property name="validateRequest" value="true"/>
  30.         <property name="validateResponse" value="true"/>
  31.     </bean>
  32.     <bean id="loggingInterceptor" class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor">
  33.         <description>
  34.             This interceptor logs the message payload.
  35.         </description>
  36.     </bean>
  37.     <bean id="echoEndpoint" class="org.springframework.ws.samples.echo.ws.EchoEndpoint">
  38.         <description>
  39.             This endpoint handles echo requests.
  40.         </description>
  41.         <property name="echoService" ref="echoService"/>
  42.     </bean>
  43.     <bean id="echo" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
  44.         <description>
  45.             This bean definition represents a WSDL definition that is generated at runtime. It can be retrieved by
  46.             going to /echo/echo.wsdl (i.e. the bean name corresponds to the filename).
  47.         </description>
  48.         <property name="schema" ref="schema"/>
  49.         <property name="portTypeName" value="Echo"/>
  50.         <property name="locationUri" value="http://localhost:8080/echo/services"/>
  51.     </bean>
  52.     <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
  53.         <description>
  54.             This bean definition contains the XSD schema.
  55.         </description>
  56.         <property name="xsd" value="/WEB-INF/echo.xsd"/>
  57.     </bean>
  58.     <bean id="echoService" class="org.springframework.ws.samples.echo.service.impl.EchoServiceImpl">
  59.         <description>
  60.             This bean is our "business" service.
  61.         </description>
  62.     </bean>
  63. </beans>

web.xml

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!DOCTYPE web-app PUBLIC
  3.         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  4.         "http://java.sun.com/dtd/web-app_2_3.dtd">
  5. <web-app>
  6.     <display-name>"Echo" WebService</display-name>
  7.     
  8.     <description>
  9.         Returns a given string(only A-Z and a-z chars allowed). See
  10.         echo.xsd file.
  11.     </description>
  12.     <servlet>
  13.         <servlet-name>spring-ws</servlet-name>
  14.         <servlet-class>
  15.             org.springframework.ws.transport.http.MessageDispatcherServlet
  16.         </servlet-class>
  17.         <init-param>
  18.             <param-name>transformWsdlLocations</param-name>
  19.             <param-value>true</param-value>
  20.         </init-param>
  21.     </servlet>
  22.     <servlet-mapping>
  23.         <servlet-name>spring-ws</servlet-name>
  24.         <url-pattern>/*</url-pattern>
  25.     </servlet-mapping>
  26. </web-app>

其他资料:

http://www.javaeye.com/topic/152556

http://www.blogjava.net/bluesky/archive/2008/09/19/Writing_Contract-First_Web_Services_Use_Spring-ws_and_Xmlbeans.html

原创粉丝点击