使用客户端上的Java XmlType带注释的对象,使用Web服务访问服务器端的数据库

时间:2014-02-21 20:27:32

标签: java database web-services soap webmethod

我正在为服务开发一个java Web服务以及一个java客户端,我想知道我的设计是否可以在基于SOAP的文档样式Web服务的约束下完成。在服务器端,我有一个数据库,我使用实用程序类连接到几个其他数据库“处理程序”类来操作数据库。我的Web服务具有返回处理程序类的WebMethods。每个处理程序类都使用@XmlType批注进行批注,以便它们可以绑定到XML类型。我的问题是,如果我的客户端调用其中一个webmethods并获得一个数据库处理程序,我可以通过处理程序的方法(它们没有注释@WebMethod)依次操作服务器端的数据库。基于SOAP的Web服务是否可以这样?

我在下面有示例代码。

以下是我的实用程序类的功能。

    public class DBUtil {
        public static getConnection() {
            return DriverManager.getConnection(url, username, password);
        }
    }

以下是我的一个数据库处理程序的示例。

    @XmlType
    public class Handler1 {

        public LinkedList<String> listDatabases() {
            Connection con = DBUtil.getConnection();
            LinkedList<String> list = new LinkedList<String>();
            //use con to query database and fill list with know database names
            return list;
        }

        public LinkedList<UserData> listUser(String database) {
            //UserData is a user defined type that store information about user names
            //and their privileges

            Connection con = DBUtil.getConnection();
            //query database and fill a LinkedList list with one UserData object
            //for each database user
            return list;
        }
    }

我的其他处理程序类包含用于查询和修改表的类似方法。

以下是我的网络服务。

@WebService()
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
public class WSDatabaseImp implements WSDatabase {

    //WSDatabase is my SEI

    public WSDatabaseImp() {}

    @WebMethod
    public Handler1 getHandler1() {
        return new Handler1();
    }
    //more get HandlerX methods
}

我想要做的是在我的客户端使用Handler1等操作数据库。下面的代码演示了我想要做的事情,并假设我运行了运行wsimport来生成工件。

public class Client {

    public static void main(String[] args) {
        WSDatabaseImpService service = new WSDatabaseImpService();
        WSDatabaseImp port = service.getWSDatabaseImpPort();
        Handler1 h = service.getHandler1();
        LinkedList<String> list = h.listDatabases();
        for(String s: list)
            System.out.println(s);
    }
}

这种设计可行吗?非常感谢你的帮助。

0 个答案:

没有答案