个简单的SpringWebService示例

上传人:ji****72 文档编号:39572109 上传时间:2018-05-17 格式:DOCX 页数:10 大小:21.70KB
返回 下载 相关 举报
个简单的SpringWebService示例_第1页
第1页 / 共10页
个简单的SpringWebService示例_第2页
第2页 / 共10页
个简单的SpringWebService示例_第3页
第3页 / 共10页
个简单的SpringWebService示例_第4页
第4页 / 共10页
个简单的SpringWebService示例_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《个简单的SpringWebService示例》由会员分享,可在线阅读,更多相关《个简单的SpringWebService示例(10页珍藏版)》请在金锄头文库上搜索。

1、一个简单的一个简单的 SpringSpring WebWeb ServiceService 示例示例刚接触 web service,好不容易找到一篇 spring-ws 的例子,还琢磨了好长一段时间,很多概念性的问题都没弄清楚。只能依葫芦画瓢,照搬过来,稍微修改 了一下,使结构更加清晰基本环境:JDK6、Tomcat 6.0、MyEclipse 6.6、spring 2.0、spring-ws-1.5.51、spring-ws-servlet.xml这个地方出现了一段插曲,hello.wsdl 放在 WEB-INF 下老是报错,说 hello.wsdl 找不到,后来放到 classpath 下

2、才 OK。创建一个 Web 项目, 由于 Spring Web Service 是基于 Spring MVC 的, 在web.xml 中添加如下 servlet, 并在 WEB-INF 下建立 SpringMVC 的默认配置文件spring-ws-servlet.xml:1. 2. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. - - 19. 20. 21. 22. 23. 24. 25. 26. 27. 28.其中最主要的 bean 就是 payloadMapping, 它定义了接收到的 message 与endpoint 之间的 ma

3、pping 关系:将 SOAP Body 中包含的 xml 的根节点的 QName 为http:/ 交给 helloEndpoint 处理.SimpleWsdl11Definition 这个 bean 则是定义了这个服务的 wsdl, 访问地址是:http:/localhost:8080/springws/hello.wsdl.2、web.xml1. 2. 7. 8. 10. contextConfigLocation 11. 12. /WEB-INF/spring-ws-servlet.xml, 13. 14. 15. 16. 17. org.springframework.web.cont

4、ext.ContextLoaderListener 18. 19. 20. 21. 22. org.springframework.web.util.IntrospectorCleanupListener 23. 24. 25. 26. 27. 28. spring-ws 29. org.springframework.ws.transport.http.MessageDispatcherServle t 30. 31. 32. spring-ws 33. /* 34. 35. 36. 37. index.jsp 38. 39. 40. 41.3、hello.wsdl1. 2. 8. 9. 1

5、0. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49.4、HelloService.java1. package com.sws; 2. 3. public interface HelloService 4. 5. String sayHello(String name); 6. 7. 8.5、HelloServiceImpl.java1. p

6、ackage com.sws; 2. 3. public class HelloServiceImpl implements HelloService 4. 5. Override 6. public String sayHello(String name) 7. return “Hello, “ + name + “!“; 8. 9. 10. 11.6、HelloEndPoint.java实现一个 EndPoint 来处理接收到的 xml 及返回 xml.当然, Spring Web Service提供了很多抽象的实现, 包括 Dom4j, JDom 等等.这里我们使用 JDK 自带的, 需

7、要继承org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint.1. package com.sws; 2. 3. import org.springframework.util.Assert; 4. import org.springframework.ws.server.endpoint.AbstractDomPayloadEn dpoint; 5. import org.w3c.dom.Document; 6. import org.w3c.dom.Element; 7. import org.w3c.dom.No

8、de; 8. import org.w3c.dom.NodeList; 9. import org.w3c.dom.Text; 10. 11.public class HelloEndPoint extends AbstractDomPayloadEndpoint 12. 13. /* 14. * Namespace of both request and response 15. */ 16. public static final String NAMESPACE_URI = “http:/www.ispring.co m/ws/hello“; 17. 18. /* 19. * The l

9、ocal name of the expected request 20. */ 21. public static final String HELLO_REQUEST_LOCAL_NAME = “eReque st“; 22. 23. /* 24. * The local name of the created response 25. */ 26. public static final String HELLO_RESPONSE_LOCAL_NAME = “fRespo nse“; 27. 28. private HelloService helloService; 29. 30.31

10、. Override 32. protected Element invokeInternal(Element requestElement, Docume nt document)throws Exception 33. Assert.isTrue(NAMESPACE_URI.equals(requestElement.getNames paceURI(), “Invalid namespace“); 34. Assert.isTrue(HELLO_REQUEST_LOCAL_NAME.equals(requestEle ment.getLocalName(), “Invalid local

11、 name“); 35. 36. NodeList children = requestElement.getChildNodes(); 37. Text requestText = null; 38. for(int i=0; ichildren.getLength(); i+) 39. if(children.item(i).getNodeType() = Node.TEXT_NODE) 40. requestText = (Text) children.item(i); 41. 42. 43. 44. if(requestText = null) 45. throw new Illega

12、lArgumentException(“Could not find request t ext node“); 46. 47. 48. String response = helloService.sayHello(requestText.getNodeValu e(); 49. Element responseElement = document.createElementNS(NAMESP ACE_URI, HELLO_RESPONSE_LOCAL_NAME); 50. Text responseText = document.createTextNode(response); 51. responseElement.appendChild(responseText); 52. return responseElement; 53. 54. 55. 56. public HelloService getHelloService() 57. return helloService; 58. 59. 60. 61. public void setHelloService(HelloService helloService)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号