发布并访问的基于Axis2的Web Service

一. 使用services.xml文件,发布.aar格式的Web Service,并编程访问。

   1.  编写一个服务类OperationService,实现加减乘除功能:

package service;

import java.text.DecimalFormat;

public class OperationService {
   public int getPlus(int a,int b) {
         return a+b;
   }
  
   public int getMinus(int a,int b) {
         return a-b;
   }
  
   public int getMulti(int a,int b) {
         return a*b;
   }
   public float getDivide(float a,float b) {
    if(b==0)
    {
     return -1;
    }
    DecimalFormat   df   =   new   DecimalFormat( "#.## ");
       return Float.parseFloat(df.format(a/b));
   }
   
}

 

2.建立一个service.xml文件

<service name="OperationService">
    <description>
        Web Service Example
    </description>
    <parameter name="ServiceClass">
        service.OperationService 
    </parameter>
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
            class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
            class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
    </messageReceivers>
</service>
注意红色类名与编写类一致。

 

3.建立文件夹

       D:/ws/service/OperationService.class

     D:/ws/META-INF/services.xml

windows控制台中进入ws目录,并输入如下的命令生成.aar文件,即输入jar cvf operation.aar .生成operation.arr包,把此包放到<tomcat安装路径>/webapps/axis2/WEB-INF/services/下,利用java2wsdl方法,编写客户端。

 

代码:

 

package client;

import java.util.Scanner;

public class StubClient {
    public static void main(String[] args) throws Exception {
     //
     OperationServiceStub stub = new OperationServiceStub();

     Scanner input = new Scanner(System.in);
     System.out.println("加法:");
        int a = Integer.parseInt(input.nextLine());
        int b = Integer.parseInt(input.nextLine());
     OperationServiceStub.GetPlus plus = new OperationServiceStub.GetPlus();
        plus.setA(a);
        plus.setB(b);
        System.out.println("结果:"+ stub.getPlus(plus).get_return());
       
        System.out.println("减法:");
        a = Integer.parseInt(input.nextLine());
        b = Integer.parseInt(input.nextLine());
     OperationServiceStub.GetMinus minus = new OperationServiceStub.GetMinus();
     minus.setA(a);
     minus.setB(b);
        System.out.println("结果:"+ stub.getMinus(minus).get_return());
       
        System.out.println("乘法:");
        a = Integer.parseInt(input.nextLine());
        b = Integer.parseInt(input.nextLine());
     OperationServiceStub.GetMulti multi = new OperationServiceStub.GetMulti();
     multi.setA(a);
     multi.setB(b);
        System.out.println("结果:"+ stub.getMulti(multi).get_return());
    
        System.out.println("除法:");
        float x = Float.parseFloat(input.nextLine());
        float y = Float.parseFloat(input.nextLine());
     OperationServiceStub.GetDivide divide = new OperationServiceStub.GetDivide();
     divide.setA(x);
     divide.setB(y);
        System.out.println("结果:"+ stub.getDivide(divide).get_return());
    }
}

 

 

 

 


 

 

 

 

 

2.使用services.xml文件,将项目打包成.war格式文件的形式,发布Web Service,并编程访问。

  首先在MyEclipse下建立一个Web项目WS_Student,axis2-web文件夹拷贝到WebRoot下。把axis2/WEB-INF/下的confmodules拷贝到WebRoot/WEB-INF/下,并在此目录下建立services文件夹,在services中添加student文件夹,在student下添加META-INF,在此目录下添加service.xmlwsdl文件。添加


  <servlet> 
        <servlet-name>AxisServlet</servlet-name> 
        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
         
    <servlet-mapping> 
        <servlet-name>AxisServlet</servlet-name> 
        <url-pattern>/services/*</url-pattern> 
    </servlet-mapping> 

 

web.xml中,文件结构如下:《--图片--》

 

   在此项目中添加服务类:

//输入:学号

//查询数据库,返回成绩

package service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class StudentService {
   public int getScore(String id) {
    int score = 0;
    Statement stmt;
    Connection con;
    try {
   Class.forName("com.mysql.jdbc.Driver");
            con =  DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=GBK","root","root");
            stmt = con.createStatement(); 
   ResultSet rs = stmt.executeQuery("select Score from student where Id="+id+"");
     
   while(rs.next())
      {
    score = rs.getInt(1);
      }
  }
  catch(Exception e)
  {
   e.printStackTrace();
   return 0;
  }
   
          return score;
   }  
}

 

在运行控制台,到当前的路径输入jar cvf Lab2.war .生成Lab2.war包,把此包放到<tomcat安装路径>/webapps/下。建立学生数据库,按照此实验第一步得到Lab2.arr,把服务Lab2.arr放到<tomcat安装路径>/webapps/Lab2/WEB-INF/services/,启动tomcat,浏览器中输入:

http://localhost:8080/Lab2/services/listServices,即可看到发布的war包中服务。

 

 

 

java2wsld编写简单的客户端,访问WebService,提供学生学号参数id,查询学生成绩。

//做了个java图形界面,不再是黑框程序

package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.axis2.AxisFault;

public class StudentStubClient extends JFrame {
 public StudentStubClient()
 {
  setSize(400, 80);
  this.add(new MyPanel());
     setLocation(400, 150);
  setTitle("查询成绩");
  setVisible(true);
 }
    public static void main(String[] args) throws Exception {
     StudentStubClient ssc = new StudentStubClient();
    }
    class MyPanel extends JPanel
    {
     public MyPanel()
     {
      JLabel lbId = new JLabel("学号:");
      this.add(lbId);
      final JTextField tfId = new JTextField(10);
      this.add(tfId);
            JButton btQuery = new JButton("查询");
      this.add(btQuery);
      JLabel lbScore = new JLabel("成绩:");
      this.add(lbScore);
            final JTextField tfScore = new JTextField(10);
      this.add(tfScore);
       
      btQuery.addActionListener(new ActionListener()
      {

    @Override
    public void actionPerformed(ActionEvent e) {
     // TODO Auto-generated method stub
     StudentServiceStub stub;
     try {
      
      stub = new StudentServiceStub();
         StudentServiceStub.GetScore plus = new StudentServiceStub.GetScore();

         plus.setId(tfId.getText());
         tfScore.setText(""+stub.getScore(plus).get_return());
      
     } catch (AxisFault e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     }catch (RemoteException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     }
    }
       
      });
     }
    }
}
   查询数据,
与数据库的一致,访问服务成功。

 

 

其中遇到的问题:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值