WebService(1)-AXIS入门

webService 是一种基于http协议的组件
webService 开发框架:AXIS(Apache eXtensible Interaction System),xFire,
WSDD就是WEB服务分布描述(Web Service Deployment Descriptor), 它定义了WEB服务的接口,如服务名、提供的方法、方法的参数等信息。
AXIS 发布的两种方式
即时发布 jws java web service  只要把.java 文件改成.jws文件,发布就可以了,但是不支持带包类
定制发布 wsdd web service Deployment Descriptor 这个发布需要配置文件*.wsddserver-config.wsdd文件

一个简单的AXIS应用


 (1):需要的jar 包
 activation.jar
 axis.jar
 commons-discovery.jar
 commons-logging.jar
 jaxrpc.jar
 log4j-1.2.15.jar
 mail.jar
 wsdl4j.jar
 spring.jar

<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>javax.activation</groupId>
			<artifactId>activation</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>axis</groupId>
			<artifactId>axis</artifactId>
			<version>1.3</version>
		</dependency>
		<dependency>
			<groupId>commons-discovery</groupId>
			<artifactId>commons-discovery</artifactId>
			<version>0.5</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>xfire</groupId>
			<artifactId>xfire-jaxrpc</artifactId>
			<version>20041222</version>
		</dependency>

		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.9</version>
		</dependency>
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.3.2</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
			<version>1.5.1</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
		</dependency>
	</dependencies>


 

 (2) web.xml配置
 <!-- ================= webservice开始 ===================================== -->
  

 <listener>
  <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
     </listener>
     <!-- 这个servlet会加载server-config.wsdd -->
   <servlet>
     <servlet-name>AxisServlet</servlet-name>
     <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
     <load-on-startup>2</load-on-startup>
   </servlet>
   <servlet-mapping>
     <servlet-name>AxisServlet</servlet-name>
     <url-pattern>/services/*</url-pattern>
   </servlet-mapping>
  <!-- 对../services/.wsdd请求和.../jws 都交由webService来处理 -->
    <servlet-mapping>
     <servlet-name>AxisServlet</servlet-name>
     <url-pattern>*.jws</url-pattern>
   </servlet-mapping>


 (3) 在WEB-INFO 下增加server-config.wsdd文件,其配置如下
  

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
        xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
        <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" />   
        <!-- 自定义服务 -->
        <service name="ws" provider="java:RPC">
        <parameter name="className" 
        value="cn.com.chenlly.ssh.webservice.axis.WSTestServiceImpl"/>
        <parameter name="allowedMethods" value="*" />
         <parameter name="scope" value="request"/>
        </service>
        <transport name="http">
        <requestFlow>
        <handler type="URLMapper" />
        </requestFlow>
        </transport>
 </deployment>

 

 (4) 定义如下service 服务

 

package cn.com.chenlly.ssh.webservice.axis;
 public interface WSTestService  {
  
  public String readStr();
 }

 package cn.com.chenlly.ssh.webservice.axis;
 import javax.xml.rpc.ServiceException;
 import org.springframework.context.ApplicationContext;
 import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
 import cn.com.chenlly.ssh.test.manager.LoginManager;
 public class WSTestServiceImpl extends ServletEndpointSupport implements WSTestService {
  //Spring 上下文
  private ApplicationContext context;
  //登录服务
  private LoginManager loginManager;
  @Override
  protected void onInit() throws ServiceException {
   System.out.println("webService init.....");
   //拿到Spring 上下文
   context = super.getApplicationContext();
   loginManager = (LoginManager) context.getBean("loginManager");
   loginManager.test();
  }
  
  @Override
  public String readStr() {
   return "HelloWorld";
  }
  
  @Override
  public void destroy() {
   super.destroy();
  }
 }

 注意:登录服务在spring 配置文件里面已经注入了,在onInit()方法里会自动加载给webservice服务类使用。Spring使用ServletEndpointSupport来暴露WebService。
 ServletEndpointSupport AXIS 在每次请求时都会产生一个新实例,每次都会调用onInit()方法。

 (5) 客户端调用Webservice
 

package cn.com.chenlly.ssh.webservice.axis;
 import java.net.MalformedURLException;
 import javax.xml.rpc.ServiceException;
 import javax.xml.namespace.QName;
 import org.apache.axis.client.Call;
 import org.apache.axis.client.Service;
 public class WSTestServiceClient{
  public static void main(String []args){
   Service service = new Service();
   try {
    Call call = (Call) service.createCall();
    String url = "http://192.168.1.98:8082/SSHProject/services/ws?wsdl";
    QName qname = new QName(url,"readStr");
    call.setOperationName(qname);
    call.setTargetEndpointAddress(new java.net.URL(url));
    String result = (String) call.invoke(new Object[]{});
    System.out.println("===="+result);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }

 属性讲解:<parameter name="scope" value="request"/>
 request:这个选项会让AXIS为每个SOAP请求产生一个服务对象,
 session:会给每个调用WebService 的客户端创造一个服务对象
 application:这个选项程序只会在内存里面new 一个服务对象,然后为所有的webService 客户端服务

你可能感兴趣的:(WebService(1)-AXIS入门)