6, 修改配置文件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">
- <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>
<?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, 它定义了接收到的message与endpoint之间的mapping关系:将SOAP Body中包含的xml的根节点的QName为{http://www.fuxueliang.com/ws/hello}HelloRequest交给helloEndpoint处理.
SimpleWsdl11Definition这个bean则是定义了这个服务的wsdl, 访问地址是:http://localhost:8080/springws/hello.wsdl.
7, 客户端(saaj实现)的代码如下:
- /**
- *
- * @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();
- }
- }
/** * * @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处理