`
zzc1684
  • 浏览: 1190513 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

Spring Webservice实现过程

阅读更多

1、创建service接口

 

package com.liul.test.spring_ws_test.service;

 

public interface SayHello {
 public String sayHello(String name);
}

 

2、创建接口实现(一定要加上service注解)

 

package com.liul.test.spring_ws_test.service.impl;

 

import org.springframework.stereotype.Service;

 

import com.liul.test.spring_ws_test.service.SayHello;
@Service
public class SayHelloImpl implements SayHello {

 

 public String sayHello(String name) {
  // TODO Auto-generated method stub
  return name+":Hello!";
 }

 

}

 

3、创建endpoint

 

package com.liul.test.spring_ws_test.ws;

 

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
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;

 

import com.liul.test.spring_ws_test.service.SayHello;

 

@Endpoint
public class SayHelloEndPoint {
 public static final String NAMESPACE_URI="http://www.liul.com/sayHello";
 public static final String REQUEST_LOCAL_NAME="sayRequest";
 public static final String RESPONSE_LOCAL_NAME = "sayResponse";
 private final SayHello service;
 private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
 @Autowired
 public SayHelloEndPoint(SayHello service) {
  this.service = service;
 }
 @PayloadRoot(localPart=REQUEST_LOCAL_NAME,namespace=NAMESPACE_URI)
 @ResponsePayload

 public Element handleRequest(@RequestPayload Element requestElement) throws ParserConfigurationException{
     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 = service.sayHello(requestText.getNodeValue());

 

         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
         Document document = documentBuilder.newDocument();
         Element responseElement = document.createElementNS(NAMESPACE_URI, RESPONSE_LOCAL_NAME);
         Text responseText = document.createTextNode(echo);
         responseElement.appendChild(responseText);
         return responseElement;
 }
}
4、定义schema,say.xsd文件

 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified"
        targetNamespace="http://www.liul.com/sayHello"
        xmlns:tns="http://nouse.org">
    <element name="sayRequest" type="string"/>

 

    <element name="sayResponse" type="string"/>
</schema>

 

5、修改web.xml

 

    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <!-- Transform the location attributes in WSDLs -->
            <param-name>transformWsdlLocations</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

 

    <!-- Map all requests to this servlet -->
    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

 

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" xmlns:sws="http://www.springframework.org/schema/web-services"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.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>
 <context:component-scan base-package="com.liul.test.spring_ws_test" />
 <sws:annotation-driven />
 <sws:interceptors>
  <bean
   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="schema" value="/WEB-INF/say.xsd" />
   <property name="validateRequest" value="true" />
   <property name="validateResponse" value="true" />
  </bean>
  <bean
   class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor">
   <description>
    This interceptor logs the message payload.
            </description>
  </bean>
 </sws:interceptors>
 <sws:dynamic-wsdl id="say" portTypeName="Echo"
  locationUri="http://localhost:8080/abcd">
  <sws:xsd location="/WEB-INF/say.xsd" />
 </sws:dynamic-wsdl>
</beans>

 

至此,服务器端配置完成

 

要点:

 

1、endpoint类的NAMESPACE_URI与客户端请求文件的xmlns值一致(sayRequest.xml)

 

     示例定义为:http://www.liul.com/sayHello

 

2、endpoint里的REQUEST_LOCAL_NAME要和客户端请求文件的根标记一致

 

     示例定义为:sayRequest

 

3、客户端的defaultUri必须是可访问的web工程路径

 

      示例定义为:http://localhost:8080/abcd

4、注:在spring-ws-servlet.xml配置的sws:dynamic-wsdl可不用配置,只是用来在浏览器中查看wsdl,不影响webservice访问

 

客户端的请求文件(sayRequest.xml)

 

<?xml version="1.0" encoding="UTF-8"?>
<sayRequest xmlns="http://www.liul.com/sayHello">dabao大宝</sayRequest>         

 

客户端代码

 

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();
        System.out.println("response1:"+result);
        System.out.println();
    }

 

    public static void main(String[] args) throws IOException {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml", EchoClient.class);
        EchoClient echoClient = (EchoClient) applicationContext.getBean("sayClient");
        echoClient.echo();
    }

}

分享到:
评论

相关推荐

    运用SpringDM和CXF来实现WebService的动态发布

    它实现了JCP与WebService2.1中一些重要标准。CXF简化了构造,集成,面向服务架构(SOA)业务组件与技术的灵活复用。在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式(或binding)和网络协议(t

    自己弄的三层框架Spring.net,Remoting

    3, Web 实现UI, 组织数据提交WebService处理. 层与层之间是独立的. 声明: 本框架仅做为个人研究使用, 若有任何问题或因使用产生的任何损失, 一概与本人无关. 不喜勿下!((((记得评价拿回积分)))). 开发环境要求: ...

    如何用Java SpringBoot实现调用OpenAI ChatGPT的相关接口

    如何用Java SpringBoot实现调用OpenAI ChatGPT的相关接口

    基于 SpringBoot+ +Mybatis+ Apache velocity 开发 webservice +源代码+文档说明

    基于SpringBoot + Spring + Apache CXF +Mybatis 开发SOAP的 WebService 服务 # 备注 | **版本** | **说明**| | ------ |:------:| | 1.0.0版本| | | 2.0.0版本|springboot启动| # 原理 Mybatis基于动态代理实现...

    WebServiceConfig java springboot利用Apache CXF创建webserice接口配置类

    默认Bus实现基于Spring架构, * 通过依赖注入,在运行时将组件串联起来。BusFactory负责Bus的创建。默认的BusFactory是SpringBusFactory,对应于默认 * 的Bus实现。在构造过程中,SpringBusFactory会搜索META-INF/...

    P2P网络借贷平台项目SSH+Redis+ActiveMQ+POI+Shiro+AngularJS+Nginx+Quartz等

    3、该项目采用了struts2 hibernate spring和 spring data jpa 开源框架完成,并融入了cxf开源webservice框架的应用,而这些技术都是当下流行的技术。 4、在缓存方面运用了互联网的流行技术redis实现缓存存贮,...

    Java思维导图xmind文件+导出图片

    WebService/ApacheCXF RMI/Spring RMI Hession 传统RPC技术在大型分布式架构下面临的问题 分布式架构下的RPC解决方案 Zookeeper 分布式系统的基石 从0开始搭建3个节点额度zookeeper集群 深入分析Zookeeper在...

    智能制造专家-智能工厂解决方案.pdf

    后台配置开发MAP,把业务处理抽象成元动作,短元动作用存储过程实现,长元动作用Java/Python 实现。通过元动作,为配置计算、现场管控、接口处理和告警等,配置后台处理程序,并配合DRO实现复杂报表和打印。 MQX是...

    最新Java面试题视频网盘,Java面试题84集、java面试专属及面试必问课程

    简单介绍一下Spring或者Spring的两大核心.mp4 │ Java面试题53.AOP是什么?都用它做什么?.mp4 │ Java面试题54.Spring事务的传播特性和隔离级别.mp4 │ Java面试题55.ORM是什么?ORM框架是什么?.mp4 │ Java面试题...

    2021年最新java面试题--视频讲解(内部培训84个知识点超详细).rar

    Java面试题59.webservice的使用场景 Java面试题60.activiti的简单介绍 Java面试题61.linux的使用场景 Java面试题62.linux常用命令 Java面试题63:怎么操作linux服务器 Java面试题64:有没有使用过云主机 Java面试题...

    Java EE常用框架.xmind

    WebService 介绍 基于Web的服务。它使用Web(HTTP)方式,接收和响应外部系统的某种请求。从而实现远程调用 术语 XML. Extensible Markup Language -扩展性标记语言 WSDL – WebService ...

    asp.net知识库

    存储过程中实现类似split功能(charindex) 通过查询系统表得到纵向的表结构 将数据库表中的数据生成Insert脚本的存储过程!!! 2分法-通用存储过程分页(top max模式)版本(性能相对之前的not in版本极大提高) 分页存储...

    java学习重点

    1 目前国内比较流行的技术组合:spring(www.springframework.com) + hibernate技术,还有webservice +XML技术; 2 J2EE的相关技术:EJB,SEVILET,JSP等; 3 源文件(*.java) ---------&gt;类文件(*.class) ---------...

    涵盖了90%以上的面试题

    谈谈你对webservice和dubbo的理解 谈谈你的SOA的理解。 谈谈你对freemarker的理解 谈谈你对springMVC的理解 谈谈你对mybatis的理解 hibernate和mybatis的区别 同步接口和异步接口的区别 为什么要加入锁机制 如何确定...

    Java面试宝典2010版

    58、Collection框架中实现比较要实现什么接口 59、ArrayList和Vector的区别 60、HashMap和Hashtable的区别 61、List 和 Map 区别? 62、List, Set, Map是否继承自Collection接口? 63、List、Map、Set三个接口,...

    最新Java面试宝典pdf版

    58、Collection框架中实现比较要实现什么接口 43 59、ArrayList和Vector的区别 44 60、HashMap和Hashtable的区别 44 61、List 和 Map 区别? 45 62、List, Set, Map是否继承自Collection接口? 45 63、List、Map、Set...

    Java面试笔试资料大全

    58、Collection框架中实现比较要实现什么接口 43 59、ArrayList和Vector的区别 44 60、HashMap和Hashtable的区别 44 61、List 和 Map 区别? 45 62、List, Set, Map是否继承自Collection接口? 45 63、List、Map、Set...

    JAVA面试宝典2010

    58、Collection框架中实现比较要实现什么接口 43 59、ArrayList和Vector的区别 44 60、HashMap和Hashtable的区别 44 61、List 和 Map 区别? 45 62、List, Set, Map是否继承自Collection接口? 45 63、List、Map、Set...

    Java面试宝典-经典

    58、Collection框架中实现比较要实现什么接口 43 59、ArrayList和Vector的区别 44 60、HashMap和Hashtable的区别 44 61、List 和 Map 区别? 45 62、List, Set, Map是否继承自Collection接口? 45 63、List、Map、Set...

Global site tag (gtag.js) - Google Analytics