下载资源包:http://www.springsource.org/download
Spring Web Service被设计成契约式开发模式,他依据自顶向下的设计方式(XML/XSD to JAVA),不同于XFire。
如下图所示建立工程:
代码如下:
EchoClient.java
- package org.springframework.ws.samples.echo.client.sws;
- import java.io.IOException;
- import javax.xml.transform.Source;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.core.io.Resource;
- import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
- import org.springframework.xml.transform.ResourceSource;
- import org.springframework.xml.transform.StringResult;
- public class EchoClient extends WebServiceGatewaySupport {
- private Resource request;
- public void setRequest(Resource request) {
- this.request = request;
- }
- public void echo() throws IOException {
- Source requestSource = new ResourceSource(request);
- StringResult result = new StringResult();
- getWebServiceTemplate().sendSourceAndReceiveToResult(requestSource, result);
- System.out.println(result);
- }
- public static void main(String[] args) throws IOException {
- ApplicationContext applicationContext =
- new ClassPathXmlApplicationContext("applicationContext.xml", EchoClient.class);
- EchoClient echoClient = (EchoClient) applicationContext.getBean("echoClient");
- echoClient.echo();
- }
- }
applicationContext.xml
- <?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="echoClient" class="org.springframework.ws.samples.echo.client.sws.EchoClient">
- <property name="defaultUri" value="http://localhost:8080/echo/services" />
- <property name="request" value="classpath:org/springframework/ws/samples/echo/client/sws/echoRequest.xml" />
- </bean>
- </beans>
echoRequest.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <echoRequest xmlns="http://www.springframework.org/spring-ws/samples/echo">Hello</echoRequest>
EchoServiceImpl.java
- package org.springframework.ws.samples.echo.service.impl;
- import org.springframework.ws.samples.echo.service.EchoService;
- public class EchoServiceImpl implements EchoService {
- public String echo(String s) {
- return s;
- }
- }
EchoService.java
- package org.springframework.ws.samples.echo.service;
- public interface EchoService {
- String echo(String s);
- }
EchoEndpoint.java
- package org.springframework.ws.samples.echo.ws;
- import org.springframework.util.Assert;
- import org.springframework.ws.samples.echo.service.EchoService;
- import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.w3c.dom.Text;
- public class EchoEndpoint extends AbstractDomPayloadEndpoint {
- public static final String NAMESPACE_URI = "http://www.springframework.org/spring-ws/samples/echo";
- public static final String ECHO_REQUEST_LOCAL_NAME = "echoRequest";
- public static final String ECHO_RESPONSE_LOCAL_NAME = "echoResponse";
- private EchoService echoService;
- public void setEchoService(EchoService echoService) {
- this.echoService = echoService;
- }
- protected Element invokeInternal(Element requestElement, Document document) throws Exception {
- Assert.isTrue(NAMESPACE_URI.equals(requestElement.getNamespaceURI()), "Invalid namespace");
- Assert.isTrue(ECHO_REQUEST_LOCAL_NAME.equals(requestElement.getLocalName()), "Invalid local name");
- NodeList children = requestElement.getChildNodes();
- Text requestText = null;
- for (int i = 0; i < children.getLength(); i++) {
- if (children.item(i).getNodeType() == Node.TEXT_NODE) {
- requestText = (Text) children.item(i);
- break;
- }
- }
- if (requestText == null) {
- throw new IllegalArgumentException("Could not find request text node");
- }
- String echo = echoService.echo(requestText.getNodeValue());
- Element responseElement = document.createElementNS(NAMESPACE_URI, ECHO_RESPONSE_LOCAL_NAME);
- Text responseText = document.createTextNode(echo);
- responseElement.appendChild(responseText);
- return responseElement;
- }
- }
echo.xsd
- <?xml version="1.0" encoding="UTF-8"?>
- <schema xmlns="http://www.w3.org/2001/XMLSchema"
- elementFormDefault="qualified" attributeFormDefault="qualified"
- targetNamespace="http://www.springframework.org/spring-ws/samples/echo"
- xmlns:tns="http://www.springframework.org/spring-ws/samples/echo">
- <element name="echoRequest">
- <simpleType>
- <restriction base="string">
- <pattern value="([A-Z]|[a-z])+" />
- </restriction>
- </simpleType>
- </element>
- <element name="echoResponse" type="string" />
- </schema>
spring-ws-servlet.xml
- <?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 id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
- <description>
- This endpoint mapping uses the qualified name of the payload (body contents) to determine the endpoint for
- an incoming message. Every message is passed to the default endpoint. Additionally, messages are logged
- using the logging interceptor.
- </description>
- <property name="defaultEndpoint" ref="echoEndpoint"/>
- <property name="interceptors">
- <list>
- <ref local="validatingInterceptor"/>
- <ref local="loggingInterceptor"/>
- </list>
- </property>
- </bean>
- <bean id="validatingInterceptor"
- class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
- <description>
- This interceptor validates both incoming and outgoing message contents according to the 'echo.xsd' XML
- Schema file.
- </description>
- <property name="xsdSchema" ref="schema"/>
- <property name="validateRequest" value="true"/>
- <property name="validateResponse" value="true"/>
- </bean>
- <bean id="loggingInterceptor" class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor">
- <description>
- This interceptor logs the message payload.
- </description>
- </bean>
- <bean id="echoEndpoint" class="org.springframework.ws.samples.echo.ws.EchoEndpoint">
- <description>
- This endpoint handles echo requests.
- </description>
- <property name="echoService" ref="echoService"/>
- </bean>
- <bean id="echo" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
- <description>
- This bean definition represents a WSDL definition that is generated at runtime. It can be retrieved by
- going to /echo/echo.wsdl (i.e. the bean name corresponds to the filename).
- </description>
- <property name="schema" ref="schema"/>
- <property name="portTypeName" value="Echo"/>
- <property name="locationUri" value="http://localhost:8080/echo/services"/>
- </bean>
- <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
- <description>
- This bean definition contains the XSD schema.
- </description>
- <property name="xsd" value="/WEB-INF/echo.xsd"/>
- </bean>
- <bean id="echoService" class="org.springframework.ws.samples.echo.service.impl.EchoServiceImpl">
- <description>
- This bean is our "business" service.
- </description>
- </bean>
- </beans>
web.xml
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
- <web-app>
- <display-name>"Echo" WebService</display-name>
- <description>
- Returns a given string(only A-Z and a-z chars allowed). See
- echo.xsd file.
- </description>
- <servlet>
- <servlet-name>spring-ws</servlet-name>
- <servlet-class>
- org.springframework.ws.transport.http.MessageDispatcherServlet
- </servlet-class>
- <init-param>
- <param-name>transformWsdlLocations</param-name>
- <param-value>true</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring-ws</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- </web-app>
其他资料: