`

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处理

分享到:
评论

相关推荐

    JBoss ESB 学习笔记

    自己辛苦整理的网上的JBoss ESB学习笔记 ,非常详细,代码操作都有截图; 希望大家多支持! 学习笔记PDF的目录如下: 1——搭建ESB开发环境 2 2——第一个ESB代码...16——第十五个ESB代码Web Service Consumer 1 151

    Struts2SpringHibernate整合,一个HelloWorld版的在线书店(项目源码+详尽注释+单元测试)

    Struts2,Spring,Hibernate是Java Web开发中最为常见的3种框架,掌握这3种框架是每个Java Web开发人员的基本功。 然而,很多初学者在集成这3个框架的时候,总是会遇到各种各样的问题。 我在读大学刚刚学习SSH的...

    java web技术开发大全(最全最新)

    涵盖更多内容,列举了大量典型实例具有超强的实用性,另外,《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》各篇独立,适合读者全面学习或对部分内容重点学习。 读者对象 有Java基础,想进一步...

    Spring.3.x企业应用开发实战(完整版).part2

    2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在...

    java web开发技术大全

    涵盖更多内容,列举了大量典型实例具有超强的实用性,另外,《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》各篇独立,适合读者全面学习或对部分内容重点学习。 读者对象 有Java基础,想...

    spring加载restful(文档+程序源码)

     本节中的例子将演示Spring 3环境的创建过程,并创建一个可以部署到Tomcat中的“Hello World”应用。然后我们再完成一个更复杂的应用来了解Spring 3 REST支持的重要概念,如多种MIME类型表示支持和JAXB支持。另外,...

    spring-rest-service

    本指南将引导您完成使用Spring创建“ Hello World” 。 你会建立什么 您将在以下位置构建将接受HTTP GET请求的服务: http://localhost:8080/greeting 并以问候语的表示形式进行响应: { " id " : 1 , " content ...

    rest-service:WCCI模块9Spring指南RESTful Web服务练习

    构建RESTful Web服务Spring入门指南 Spring入门-构建RESTful Web服务 按照学习使用创建“ Hello World” RESTful Web服务的过程。

    Spring3.x企业应用开发实战(完整版) part1

    2.1.1 比Hello World更适用的实例 2.1.2 实例功能简介 2.2 环境准备 2.2.1 创建库表 2.2.2 建立工程 2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在...

    xfire与spring集成案例

    使用XFire+Spring构建Web Service。自己参考网上的讲解写的例子。里面有help.txt 介绍了我参考的xfire+spring集成的网址。

    gs-rest-service:Spring Guides建立一个RESTful Web服务

    标签专案休息弹簧框架 目录本指南将引导您完成使用Spring创建“ Hello World” 。你会建立什么您将在以下位置构建将接受HTTP GET请求的服务: http://localhost:8080/greeting 并以问候语的表示形式进行响应: { " ...

    gs-actuator-service:使用Spring Boot Actuator构建RESTful Web服务

    本指南将指导您使用Spring Boot Actuator创建一个“世界,您好”的RESTful Web服务。 您将构建一个接受以下HTTP GET请求的服务: $ curl http://localhost:9000/hello-world 它使用以下JSON进行响应: { " id " : 1...

    RestDemo:RESTful Web服务演示,包括Jersey,Hibernate,Mysql,SQLserver,jQuery,AangularJS,Boostrap。 (REST案例大全)

    RESTful Web Service DemoS with Jersey,Hibernate,Mysql,Spring,JQuery,AngularJS ...跟随我创建一个完整的Web应用程序。 随便去吧! 这是有关如何逐步构建项目的文章,每个演示都是一个小项目: 1.用泽西...

    gs-rest-service-cors:为RESTful Web服务启用跨源请求

    目录本指南将引导您完成使用Spring创建“ Hello,World” RESTful Web服务的过程,该服务在响应中包括跨域资源共享(CORS)的标头。 您可以在此找到有关Spring CORS支持的更多信息。你会建立什么您将构建一个服务,...

    Spring.Boot.in.Action.2015.12.pdf

    Indeed, the fact that a simple Spring Boot Hello World application can fit into a tweet is a radical departure from what the same functionality required on the vm only a few short years ago. Out-of-...

    Jetty中文手册

    Jetty和Maven HelloWorld教程 Jetty(6)入门 (www.itjungle.com) Jetty Start.jar 配置Jetty 如何设置上下文(Context Path) 如何知道使用了那些jar包 如何配置SSL 如何使用非root用户监听80端口 如何配置连接器...

    MyEclipse6教程0

    0: 下载 安装 运行 HelloWorld 1 安装运行 Mysql, MySQL-Front 管理, JDBCHelloWorld 开发 2 用 MyEclipse Database Explorer 管理 MySQL 数据库 3 MyEclipse Hibernate 快速入门开发 4 MyEclipse JPA 快速入门开发 ...

    gs-rest-service:弹簧靴

    标签专案休息弹簧框架 目录本指南将引导您完成使用Spring创建“ Hello World” 。你会建立什么您将在以下位置构建将接受HTTP GET请求的服务: http://localhost:8080/greeting 并以问候语的表示形式进行响应: { " ...

    从Java走向Java+EE+.rar

    17.3 实例——分布式的HelloWorld 260 17.4 小结 264 第18章 Java消息服务 265 18.1 消息系统和JMS 265 18.1.1 JMS API 265 18.1.2 点对点消息模式 266 18.1.3 发布者/订阅者模式 267 18.1.4 同步和...

Global site tag (gtag.js) - Google Analytics