简体   繁体   中英

Using webmethods from C# in a Java client

I have a C# webservice that a C#-client can access. I wish to create a java-client that can recieve data from the same Webservice. The Webservice has a data access layer that collects information from a foreign database. My problem is that the C#-webservice return type is not compatible with the java-client. Heres my example code:

    public DataTable EmployeeNames()
    {
        Connect();
        DataSet dataset = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand("SELECT [First Name], [Last Name] FROM [dbo].[CRONUS Sverige AB$Employee]", con);

        adapter.Fill(dataset);

        DataTable datatable = dataset.Tables[0];

        Close();
        return datatable;
    }

As you can see, this webmethod returns a DataTable, which as to my understanding, Java can not process. Same goes for DataSet. I've tried converting the data to a List/Array etc but then I get problems with the columns (what do I do with the column names for example?). I'm out of options here, any help is appreciated.

PS The C# client works perfectly.

You said that you have tried to convert he data to a list or array, thus I assume that changing the method signature is something you can do.

What you could do, would be to instead return Collection of Employee objects. The Employee object would wrap around some of the data you have in the database, and within the EmployeeNames() method you perform the required translations.

This should make it easier for Java to read, since Java has a concept of a collection, or maybe an iterable.

As per your comment, you could try and convert the DataSet to a JSON Object, as shown here .

As an FYI, you do not need to expose all the fields, just the ones you want.

You can call webservice from anywhere. Be it from C#, Java or anywhere else. The question is how you are exposing your web service. I recommend exposing it as a REST service, that way it will be accessible from everywhere. There is plenty of information available on the web on how to implement RESTful services. Once you implement the REST service, then you access it using HTTP GET, PUT, UPDATE, ETC... from your choice of environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM