aspose file tools*
The moose likes Web Services and the fly likes How to return data from a database table using Apache CXF web service? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "How to return data from a database table using Apache CXF web service?" Watch "How to return data from a database table using Apache CXF web service?" New topic
Author

How to return data from a database table using Apache CXF web service?

Wap Rau
Greenhorn

Joined: Dec 01, 2011
Posts: 11
I am using Apache CXF (apache-cxf-2.5.0) to create Web Services using bottum up approach (Java first approach). I want to return some data / records (for example username, email) from a database table using web services created by bottum up approach (Java first approach). I can write Java class which returns single response. But I am not able to find way to return a response such as data / records (for example username, email) from a database table. How to do that?

Thanks.
Arun Kumar
Ranch Hand

Joined: Oct 10, 2007
Posts: 30
you can use jdbc/hibernate to get the data from database and pass it as POJO/String response via apache-cxf
Wap Rau
Greenhorn

Joined: Dec 01, 2011
Posts: 11
Arun Kumar wrote:you can use jdbc/hibernate to get the data from database and pass it as POJO/String response via apache-cxf

Thanks Arun Kumar for your answer. I am new to Web Services. I am having three doubts:

1. If I want to send data as a String data, how to send that from the function (say sendData()) in my Java class as examples on the internet have shown sending only one String as a response?

2. Is it possible/OK to send/return POJO as a response?

3. If I want to send data as POJO, how client which is a Android application can know which type of data is this and retrieve data from POJO?

Thanks.
Arun Kumar
Ranch Hand

Joined: Oct 10, 2007
Posts: 30
Answers in-line


1. If I want to send data as a String data, how to send that from the function (say sendData()) in my Java class as examples on the internet have shown sending only one String as a response?
Yes, you can send it (String can have values with some delimiter)

2. Is it possible/OK to send/return POJO as a response?
Yes, you can send POJO.

3. If I want to send data as POJO, how client which is a Android application can know which type of data is this and retrieve data from POJO?
You can use XML to send the data

Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
I don't know the size of your service or the system it exists in, but here are some general advise:
- Consider using some method of describing the input and output to/from the service.
The way of describing the data going in and out of the service of course depend on what kind of data it is.
If the service accepts XML, then use an XML schema. For JSON there are some attempts at JSON schema.
Alternatively, a written document of some kind will also suffice.
- When designing the format of the data going in and out of the service, consider what will happen if you need to extend, or in some other way modify, the data.
Perhaps you want to be able to have different versions of one kind of request etc etc.
- Write your code with request and response handling modular and extensible.

Best wishes!


My free books and tutorials: http://www.slideshare.net/krizsan
Wap Rau
Greenhorn

Joined: Dec 01, 2011
Posts: 11
Thanks Arun Kumar, Ivan Krizsan for your answers.

I am creating Apache CXF (apache-cxf-2.5.0) SOAP based Web Services. It creates WSDL from Java class (Java first approach). I have to use SOAP and can't use REST.

I am retrieving data from database using ResultSet. Data is about 5 columns and over 100 rows. Now I want to send this data to Android client. I am thinking of converting ResultSet into ArrayList and sending ArrayList / List object as a response from SOAP Web Service. My problem is - is it possible to send List object as a reponse from SOAP Web Service. If yes, please provide me link to such an examples.

Thank you.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Generally, Java-specific types such as List and Collection should be avoided.
It is better to use plain arrays.
Best wishes!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return data from a database table using Apache CXF web service?
 
Similar Threads
DAO pattern with link tables - looking for canonical way forward
Web Service Program Design
apache-cxf-2.4.3 : Print SOAP request resonse
wsdl schema validation and validation message
contract first web service