用axis开发java web service中关于服务返回复杂类型的问题

2019-03-25 13:47|来源: 网路

我是个初学者。我做了一个简单的例子,遇到点问题,希望大家可以帮忙看看
      首先这里是个pojo类,名字为Product.java
  public class Product {
  private String pid;
  private String pname;
  private double price;
  private int cnt;
  private String ext;
 
  public int getCnt() {
    return cnt;
  }
  public void setCnt(int cnt) {
    this.cnt = cnt;
  }
  public String getExt() {
    return ext;
  }
  public void setExt(String ext) {
    this.ext = ext;
  }
  public String getPid() {
    return pid;
  }
  public void setPid(String pid) {
    this.pid = pid;
  }
  public String getPname() {
    return pname;
  }
  public void setPname(String pname) {
    this.pname = pname;
  }
  public double getPrice() {
    return price;
  }
  public void setPrice(double price) {
    this.price = price;
  }
}

接着我做了一个数据库连接的文件,,名字为:DBAccess.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBAccess {
  private static Connection _conn = null;

  public static Connection getConn() {
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      _conn = DriverManager.getConnection(
          "jdbc:oracle:thin:@127.0.0.1:1521:ICSS", "PRODUCT", "icss");
    } catch (Exception e) {
      e.printStackTrace();
    }
    return _conn;
  }

  public static void closeConn(Statement stmt, ResultSet rs, Connection conn) {
    try {
      if (rs != null) {
        rs.close();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {
      if (stmt != null) {
        stmt.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    try {
      if (conn != null) {
        conn.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void closeConn() {
    try {
      if (_conn == null || _conn.isClosed()) {
        return;
      } else {
        _conn.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public void execUpdate() {
  }

  public ResultSet exceQuery(String sql) {
    ResultSet rs = null;
    try {
      if (_conn.isClosed()) {
        System.out.println("Conn is closed!!");
      }
      Statement stmt = _conn.createStatement();
      rs = stmt.executeQuery(sql);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return rs;
  }
}

接着我又做了一个接口:Productoption.java
import java.util.ArrayList;

public interface Productoption {
public ArrayList getAllProducts();  }
// 里面有一个返回为ArrayList的方法


将这几个文件通过MyEclipse+Tomcat发布之后,进入发布后classes所在的文件夹:我的是C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\SimpleExample\WEB-INF\classes
在cmd中输入命令: C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\SimpleExample\WEB-INF\classes> java org.apache.axis.wsdl.Java2WSDL  -o mb.wsdl -l "http://localhost:80/axis/services/Productoption" -n "urn:Productoption" -p"com.icss.oa.opt" "urn:Produtoption"  com.icss.oa.opt.Productoption
生成了mb.wsdl文件,接着在输入命令: java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -S true  -Nurn:Productoption com.icss.oa.opt mb.wsdl
生成了许多的.java文件,
最后就是在 ProductoptionSoapBindingImpl.java中添加业务代码了,也就是说具体实现上面接口中的getAllProducts()方法了, 可是在这里就有问题了

这里是 ProductoptionSoapBindingImpl.java的代码
public class ProductoptionSoapBindingImpl implements com.icss.oa.opt.Productoption{


    public java.lang.Object[] getAllProducts() throws java.rmi.RemoteException {
    return null;
    }

}
这里返回类型怎么成了 java.lang.Object[]而我的业务代码应该是
import .... 这里也要倒入一些包
public class ProductoptionSoapBindingImpl implements com.icss.oa.opt.Productoption{


    public java.lang.Object[] getAllProducts() throws java.rmi.RemoteException {
    Connection conn = null;   
          Statement stmt = null;   
          ResultSet rs = null;
    conn = DBAccess.getConn();
  //上面主要负责数据库的连接
    String sql ="select * from product";
   
    ArrayList<Product> proList = new ArrayList<Product>();   
   
    try {
      stmt = conn.createStatement();
      rs = stmt.executeQuery(sql);
     
      Product product;
     
      while(rs.next()){
        product = new Product();
       
        product.setPid(rs.getString(1));
        product.setPname(rs.getString(2));
        product.setPrice(rs.getDouble(3));
        product.setCnt(rs.getInt(4));
        product.setExt(rs.getString(5));
       
        proList.add(product);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }finally{
      DBAccess.closeConn(stmt,rs,conn);
    }
   
    return proList;// 这里肯定是错误的,可是我确实想让他返回一个ArrayList类型  啊,要不然在调用的时候怎么办,如下面我写的test.java来测试这个web 服务
}                                    
  
    }

}
test.java文件
import .....// 这里要倒入一些包
public class test {
public static void main(String args[]) throws ServiceException, MalformedURLException, RemoteException{  
       ProductoptionService s = new ProductoptionServiceLocator();
       Productoption opt = s.getProductoption();
       // 这里我想用ArrayList因为只有这样才能取出数据库的东西啊
       ArrayList list = opt.getAllProducts();
        for (int i = 0; i < list.size(); i++){
                 Product product = (Product) list.get(i);
      
               System.out.println("pid is" +product.getPid());
        System.out.println("cnt is" +product.getCnt());
        System.out.println("ext is" +product.getExt());
        System.out.println("pname is" +product.getPname());
        System.out.println("price is" +product.getPrice());
       
       
}
}
}
请问各位大侠,这里怎么修改才可以解决这个问题啊;


问题补充:
谢谢您,不过我也试了toArray了,只是以前没用过toArray,可能有点不对,这里是我重新写的关于上面 ProductoptionSoapBindingImpl.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.icss.oa.entity.Product;
public class ProductoptionSoapBindingImpl implements com.icss.oa.opt.Productoption{
    public java.lang.Object[] getAllProducts() throws java.rmi.RemoteException {
    Connection conn = null;   
    Statement stmt = null;   
    ResultSet rs = null;
    conn = DBAccess.getConn();
      String sql ="select * from product";
     
      ArrayList<Product> proList = new ArrayList<Product>();   
     
      try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
       
        Product product;
       
        while(rs.next()){
          product = new Product();
         
          product.setPid(rs.getString(1));
          product.setPname(rs.getString(2));
          product.setPrice(rs.getDouble(3));
          product.setCnt(rs.getInt(4));
          product.setExt(rs.getString(5));
         
          proList.add(product);
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }finally{
        DBAccess.closeConn(stmt,rs,conn);
      }
     java.lang.Object[] pro = null;
      pro = (proList).toArray(pro);
      return pro;
  //这里是使用了toArray  
}
   
}

接着我又修改了 test.java文件
具体如下:
import java.net.MalformedURLException;  
import java.rmi.RemoteException;  
import java.util.ArrayList;

import javax.xml.rpc.ServiceException;  

import com.icss.oa.entity.*;
import com.icss.oa.opt.*;  
 
public class test {
public static void main(String args[]) throws ServiceException, MalformedURLException, RemoteException{  
       ProductoptionService s = new ProductoptionServiceLocator();
       Productoption opt = s.getProductoption();
java.lang.Object[] l = opt.getAllProducts();
   ArrayList myList = new ArrayList(l.length);
// 这里我认为要再转换回来,要不然下面的代码无法用     
for (int i = 0; i < myList.size(); i++) {
Product product = (Product) myList.get(i);
System.out.println("pid is"  +product.getPid());
System.out.println("cnt is" +product.getCnt());
System.out.println("ext is" +product.getExt());
System.out.println("pname is" +product.getPname());
System.out.println("price is" +product.getPrice());
       
       
}
}
}

相关问答

更多
  • java web开发[2023-10-31]

    JAVA WEB框架技术很多啊struts,spring,hibernate,webwork等等··· 具体说明请参考http://www.open-open.com/07.htm。 建议你把webservice学习一下,很有用的
  • 好的,当你在java项目中使用外部jar时,你必须将它添加到轴lib文件夹(路径:/ web_server_local_folder / axis / WEB-INF / lib)。 我解决了我在该文件夹中添加gson jar的问题。 Ok, when you use an external jar in your java project you have to add it in the axis lib folder (path: /web_server_local_folder/axis/WEB-I ...
  • 您正在尝试实例化抽象类或接口。 这就是抛出InstantiationError的原因。 根据文档,它陈述如下 公共类InstantiationError扩展了IncompatibleClassChangeError 当应用程序尝试使用Java新构造来实例化抽象类或接口时抛出。通常,编译器会捕获此错误; 如果类的定义不兼容地更改,则此错误只能在运行时发生。 you are trying to instantiate an abstract class or interface. that's why Inst ...
  • 您已经使用WSDL生成了所需的客户端文件,因此接下来创建自己的类,创建由WSDL生成的代理类对象,通过使用该对象,您可以在应用程序中调用Web服务方法。 You already generated required client files with the WSDL ,so next create your own class in that create an object for proxy class which is generated by WSDL, by using that object ...
  • 哇,我觉得这对那里的人来说是一个简单的问题...... 我最终决定ADB可能不是为了支持我拥有的WSDL而设计的,并且转而使用来自JAX-WS的wsimport实用程序,这似乎正在工作。 Wow, I thought this would have been an easy question for someone out there... I ended up deciding that ADB was probably not designed to support the WSDL I had, an ...
  • 我认为在Delphi 6中引入了Web服务(SOAP)支持。 升级到更新的Delphi版本肯定是最简单的方法。 如果你不能,那么你需要一个实现SOAP支持的外部库。 您可以一直使用HTTP协议(实质上,SOAP Web服务通过HTTP或HTTPS发送特定形式的XML),但这需要很多工作。 你可能对RemObjects SDK很幸运:它在Delphi中支持SOAP ,但我不确定它是否支持Delphi 5。 给RemObjects的人打电话或发电子邮件:他们真的很有帮助,你可能很幸运。 --jeroen I t ...
  • 在Web服务上使用ArrayList的方式 1.它在Set上支持更多,然后在集合Class中支持一个数组列表。 2.您需要在服务器端使用@XmlRootElement创建Serialized Class,并在客户端和用户端提供此数组getter和setter以进行序列化... the way of using the ArrayList on web service 1.It support more on Set then an Array list in collection Class. 2.you ...
  • 要在Web上发布,您需要将其放在支持Web的服务器上,您可以通过以下几种方式进行: 将您的计算机变为服务器 (这是一篇文章,只需在谷歌搜索“如何将我的计算机转为网络服务器”)。 将文件上传到免费主机 将您的文件上传到付费主机(通常只需很少的费用) REST与SOAP(apache-axis): 我建议使用REST,因为它更轻量级,更灵活(它使您能够获得响应,如xml,json,html,而soap通常只是XML)。 JAX-RS用于创建REST Web服务的java API。 看看球衣框架 。 AJAX是一 ...
  • 经过2天的调查后,我发现我们从客户端的Web服务获得的响应与他们提供给我们的模式不匹配。 升级之后,我们知道他们已经推出了它而没有经过测试。 其中一个元素中存在拼写错误,WCF不会给出任何错误而只返回NULL,这非常烦人! 它在SOUP UI中工作的原因是因为SOUP UI与模式的响应不匹配。 真是一团糟! 希望,这可能对某人有所帮助。 After 2 days of investigation I found that the response which we are getting from clie ...
  • 如果您只想实现以下过滤器:services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq'Redmond' 请查看http://aspnet.codeplex.com/SourceControl/changeset/view/903afc4e11df#Samples%2fNet4%2fCS%2fWebApi%2fODataServiceSample%2fReadMe.txt上的示例。 它有一个具有可查询属性的供应商和地址模型。 它应该 ...