lixiaoming0017 2008-12-07 09:53
浏览 255
已采纳

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

我是个初学者。我做了一个简单的例子,遇到点问题,希望大家可以帮忙看看
首先这里是个pojo类,名字为Product.java
[color=red] 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;
}
}[/color]
[b]接着我做了一个数据库连接的文件,,名字为:DBAccess.java
[/b][color=red]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;
}
}[/color]
[b]接着我又做了一个接口:Productoption.java[/b]
[color=red]import java.util.ArrayList;

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

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

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

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

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

public [color=red]java.lang.Object[] getAllProducts()[/color] throws java.rmi.RemoteException {
     Connection conn = null;    
      Statement stmt = null;    
      ResultSet rs = null;
conn = DBAccess.getConn();

[color=red] //上面主要负责数据库的连接[/color]
String sql ="select * from product";

[color=red]ArrayList<Product> proList = new ArrayList<Product>();    [/color]

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);
}

[color=red]return proList;[/color]//[color=red]这里肯定是错误的,可是我确实想让他返回一个ArrayList类型  啊,要不然在调用的时候怎么办,如下面我写的test.java来测试这个web 服务[/color] 

}

}

}
[b]test.java文件[/b]
import .....//[color=red]这里要倒入一些包[/color]
public class test {
public static void main(String args[]) throws ServiceException, MalformedURLException, RemoteException{

ProductoptionService s = new ProductoptionServiceLocator();
Productoption opt = s.getProductoption();
//[color=red]这里我想用ArrayList因为只有这样才能取出数据库的东西啊[/color]
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());

}
}
}
[b]请问各位大侠,这里怎么修改才可以解决这个问题啊;[/b]

[b]问题补充:[/b]
谢谢您,不过我也试了toArray了,只是以前没用过toArray,可能有点不对,这里是我重新写的关于上面[color=red]ProductoptionSoapBindingImpl.java[/color]
[b]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);
    }
   [color=red] java.lang.Object[] pro = null;
    pro = (proList).toArray(pro);
    return pro;[/color]  //这里是使用了toArray      

}

}[/b]
接着我又修改了[color=red]test.java文件[/color]
具体如下:
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();
[color=red]java.lang.Object[] l = opt.getAllProducts();
ArrayList myList = new ArrayList(l.length);[/color] //[color=red]这里我认为要再转换回来,要不然下面的代码无法用 [/color]

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());

}
}
}

  • 写回答

2条回答 默认 最新

  • catstiger 2008-12-10 08:08
    关注

    第一片红字,直接改为return proList.toArray();或者return proList.toArray(new Object[]{});

    第二片红字:
    java.lang.Object[] l = opt.getAllProducts();
    if(l != null && l.length > 0) {
    for(int i = 0; i < l.length; i ++) {
    Product product = (Product) l[i];
    //...
    }
    }
    另外,List的遍历这样写比较好:
    for(Iterator itr = myList.iterator(); itr.hasNext();) {
    Product product = (Product) itr.next();
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里