Spring+基于AXIS2 的 werservice
Web Service基本概念
Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。
XML:(Extensible Markup Language)扩展型可标记语言。面向短期的临时数据处理、面向万维网络,是Soap的基础。
Soap:(Simple Object Access Protocol)简单对象存取协议。是XML Web Service 的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,它可以支持不同的底层接口,像HTTP(S)或者SMTP。
WSDL:(Web Services Description Language) WSDL 文件是一个 XML 文档,用于说明一组 SOAP 消息以及如何交换这些消息。大多数情况下由软件自动生成和使用。
UDDI (Universal Description, Discovery, and Integration) 是一个主要针对Web服务供应商和使用者的新项目。在用户能够调用Web服务之前,必须确定这个服务内包含哪些商务方法,找到被调用的接口定义,还要在服务端来编制软件,UDDI是一种根据描述文档来引导系统查找相应服务的机制。UDDI利用SOAP消息机制(标准的XML/HTTP)来发布,编辑,浏览以及查找注册信息。它采用XML格式来封装各种不同类型的数据,并且发送到注册中心或者由注册中心来返回需要的数据。
接下来是将webservice 基于axis2 +spring 配置实战
spring 的配置就不细讲了
axis2 用到的 maven 依赖如下:
<properties >
<axis2.version > 1.6.2</axis2.version >
</properties >
<dependency >
<groupId > org.apache.axis2</groupId >
<artifactId > axis2</artifactId >
<version > ${axis2.version}</version >
</dependency >
<dependency >
<groupId > org.apache.axis2</groupId >
<artifactId > axis2-adb</artifactId >
<version > ${axis2.version}</version >
</dependency >
<dependency >
<groupId > org.apache.axis2</groupId >
<artifactId > axis2-jaxws</artifactId >
<version > ${axis2.version}</version >
</dependency >
<dependency >
<groupId > org.apache.axis2</groupId >
<artifactId > axis2-kernel</artifactId >
<version > ${axis2.version}</version >
</dependency >
<dependency >
<groupId > org.apache.axis2</groupId >
<artifactId > axis2-metadata</artifactId >
<version > ${axis2.version}</version >
</dependency >
<dependency >
<groupId > org.apache.axis2</groupId >
<artifactId > axis2-saaj</artifactId >
<version > ${axis2.version}</version >
</dependency >
<dependency >
<groupId > org.apache.axis2</groupId >
<artifactId > axis2-spring</artifactId >
<version > ${axis2.version}</version >
</dependency >
<dependency >
<groupId > org.apache.axis2</groupId >
<artifactId > axis2-transport-http</artifactId >
<version > ${axis2.version}</version >
</dependency >
<dependency >
<groupId > org.apache.axis2</groupId >
<artifactId > axis2-transport-local</artifactId >
<version > ${axis2.version}</version >
</dependency >
<dependency >
<groupId > commons-httpclient</groupId >
<artifactId > commons-httpclient</artifactId >
<version > 3.1</version >
</dependency >
<dependency >
<groupId > org.apache.neethi</groupId >
<artifactId > neethi</artifactId >
<version > 3.0.2</version >
</dependency >
<dependency >
<groupId > org.apache.woden</groupId >
<artifactId > woden-api</artifactId >
<version > 1.0M9</version >
</dependency >
<dependency >
<groupId > org.apache.woden</groupId >
<artifactId > woden-impl-commons</artifactId >
<version > 1.0M9</version >
</dependency >
<dependency >
<groupId > org.apache.woden</groupId >
<artifactId > woden-impl-dom</artifactId >
<version > 1.0M9</version >
</dependency >
<dependency >
<groupId > wsdl4j</groupId >
<artifactId > wsdl4j</artifactId >
<version > 1.6.2</version >
</dependency >
<dependency >
<groupId > org.apache.ws.commons.schema</groupId >
<artifactId > XmlSchema</artifactId >
<version > 1.4.7</version >
</dependency >
<dependency >
<groupId > org.apache.ws.commons.axiom</groupId >
<artifactId > axiom-api</artifactId >
<version > 1.2.13</version >
</dependency >
<dependency >
<groupId > org.apache.ws.commons.axiom</groupId >
<artifactId > axiom-dom</artifactId >
<version > 1.2.13</version >
</dependency >
<dependency >
<groupId > org.apache.ws.commons.axiom</groupId >
<artifactId > axiom-impl</artifactId >
<version > 1.2.13</version >
</dependency >
这时候分为producer(生产者即服务提供者),consumer(消费者即服务调用者)
producer配置
创建 services.xml (服务接口文件)
<?xml version="1.0" encoding="UTF-8"?>
<service name ="ITestWebService" >
<description > TestWebService</description >
<parameter name ="ServiceClass" > com.java.webservice.consumer.ITestWebService</parameter >
<parameter name ="ServiceObjectSupplier" > org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter >
<parameter name ="SpringBeanName" > testWebService</parameter >
<messageReceivers >
<messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-only" class ="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out" class ="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers >
</service >
spring配置文件中 配置producer的接口类的实现
<?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:dubbo ="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd" >
<bean id ="testWebServiceTarget" class ="com.java.webservice.consumer.impl.TestWebServiceImpl" > </bean >
<bean id ="testWebService" class ="org.springframework.aop.framework.ProxyFactoryBean" >
<property name ="proxyInterfaces" >
<value > com.java.webservice.consumer.ITestWebService</value >
</property >
<property name ="proxyTargetClass" >
<value > false</value >
</property >
<property name ="target" >
<ref bean ="testWebServiceTarget" />
</property >
</bean >
</beans >
org.springframework.aop.framework.ProxyFactoryBean这个类基于动态代理代理接口并生成实现类
ITestWebService类 与 实现类 TestWebServiceImpl
package com.java.webservice.consumer;
import java.io.Serializable;
/**
* Created by zhuangjiesen on 2017/1/15.
*/
public interface ITestWebService extends Serializable {
public String doTestWebService (String testParam);
}
package com.java.webservice.consumer.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.java.webservice.consumer.ITestWebService;
/**
* Created by zhuangjiesen on 2017/1/15.
*/
public class TestWebServiceImpl implements ITestWebService {
public String doTestWebService (String testParam) {
System.out.println(" TestWebServiceImpl : doTestWebService ! testParam : " +testParam);
return "我是庄杰森的 TestWebServiceImpl doTestWebService 方法 " ;
}
}
配置成功后启动tomcat,若tomcat启动成功
在浏览器输入
http:
若出现wsdl接口描述则配置成功
接下来就是consumer的配置
maven配置跟 producer 一致
consumer是基于spring mvc 请求调用webservice接口的
/**
Description: 跳转 webservice 测试 <p>
@author : zhuangjiesen@ssit-xm.com.cn 庄杰森 2016年4月19日
*/
@RequestMapping ("/common/indexToWebServiceTest.do" )
public String indexToWebServiceTest (HttpServletRequest request,HttpServletResponse response,ModelMap model){
WebServiceProducer webServiceProducer=(WebServiceProducer)BeanHelper.getApplicationContext().getBean("webServiceProducer" );
try {
webServiceProducer.invokeTestWebService_doTestWebService();
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("title" ,"webservice + axis2 测试!!@!!!" );
return "/common/index.html" ;
}
webServiceProducer类
package com.java.webservice.producer;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import javax.xml.namespace.QName;
/**
* Created by zhuangjiesen on 2017/1/15.
*/
public class WebServiceProducer {
public void invokeTestWebService_doTestWebService () throws Exception {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference("http://localhost:8080/dragsunTomDubboTest/services/ITestWebService?wsdl" );
options.setTo(targetEPR);
Object[] opAddEntryArgs = new Object[]{"超人--庄杰森!! " };
Class[] classes = new Class[]{String.class};
QName opAddEntry = new QName("http://consumer.webservice.java.com" ,"doTestWebService" );
Object[] objs=serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes);
System.out.println(" WebServiceProducer !!! doTestWebService : " +objs[0 ]);
}
}
接着两个服务器都跑起来,然后调用即可