Web Service & serialization

H

hogcia

Hi,
I want to serialize an object into a byte array to return it from a
Web Service and save it to a file (XML) at the client side. I've done
serialization and deserialization with a regular class and it works.
But when I send the byte array through the Web Service and try to save
it, I then discover, that the file is empty... However the service
client receives some data, because it writes:
Result = [B@13576a2

Here is my code:
------------------------------------------------------------------------
Web Service:
------------------------------------------------------------------------
package ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.io.*;
import java.beans.*;
import ws.*;
/**
*
* @author agata
*/
@WebService()
public class byte64Serwer {
/**
* Web service operation
*/
@WebMethod
public byte[] getXML() {
XMLEncoder encoder = null;
XMLDecoder decoder = null;
ByteArrayOutputStream byteStream = new
ByteArrayOutputStream();
byte[] byteArray = null;

Adres annapolis = new Adres("Annapolis", 35, 53, "30-170",
"Washington, D.C.");
Osoba agata = new Osoba("Agata", "Krawcewicz", annapolis,
1982);
Osoba tomek = new Osoba("Tomasz", "Staniszewski", annapolis,
1980);

//do XML
try
{
encoder = new XMLEncoder(
new BufferedOutputStream(
byteStream));
System.out.println(agata);
System.out.println(tomek);
encoder.writeObject(agata);
encoder.writeObject(tomek);
byteArray = byteStream.toByteArray();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if(encoder != null) encoder.close();
return byteArray;
}
}

}

------------------------------------------------------------------------------
Client
------------------------------------------------------------------------------

package byte64client;

import java.io.*;

/**
*
* @author agata
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String plik;
FileOutputStream fstream = null;
String home_path = System.getProperty( "user.home" );
if(home_path.contains("/"))
{
plik = home_path + "/lista.xml";
}
else
plik = home_path + "\\lista.xml";
System.out.println("Bede zapisywal do pliku: " + plik);

try { // Call Web Service Operation
wsc.Byte64SerwerService service = new
wsc.Byte64SerwerService();
wsc.Byte64Serwer port = service.getByte64SerwerPort();
// TODO process result here
byte[] result = port.getXML();
fstream = new FileOutputStream(plik);
fstream.write(result);
fstream.close();
System.out.println("Result = " + result);
} catch (Exception ex) {
ex.printStackTrace();
}
}

}
==========================================
Am I taking a right approach? Or is it totally wrong? Sorry, but I'm a
newbie...
I can post the code for Adres and Osoba classes if it helps you if you
could run it all.
Anyway thanks for all your posts! :)
 
H

hogcia

I have just changed the 'finally' statement in Web Service to this:
finally
{
if(encoder != null) encoder.close(); //trzeba zamknac
encoder, aby czytac z byteStream
System.out.println("stream size: " + byteStream.size());
byteArray = byteStream.toByteArray();
return byteArray;
}
Seems that the stream was open and that's why it returned a 0 value.
Now it's ok.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top