2

I want to send a byte array from a web service to a client that requests an operation exposed via the service. In my method, I read an image into a byte array. I think place this byte array into a wrapper POJO. This is the return type for the operation.

@Override
public ImageWrapper getImage() {
    File imageFile = new File("C:\\images\\car.jpg");
    ImageWrapper wrapper = null;
    try {
        BufferedImage img = ImageIO.read(imageFile);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
        ImageIO.write(img, "jpg", baos);
        baos.flush();
        byte[] result = baos.toByteArray();
        baos.close();
        wrapper = new ImageWrapper();
        wrapper.setContent(result);
        System.out.println("Service image wrapper: " + wrapper);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return wrapper;
}

I can receive the ImageWrapper object in the client ok. It has a different id to the ImageWrapper instance that is created by the web service on the server, as I would expect. But, the problem is that when I try to get the byte[] array from the ImageWrapper, it is null... Any ideas why? The wrapper class looks like:

package soap.service.model;

public class ImageWrapper {
    private byte[] content;

    public void setContent(byte[] content) {
        this.content = content;
    }

    public byte[] getImg() {
        return this.content;
    }
}

and the client looks like:

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import soap.service.model.ImageWrapper;
import soap.service.sei.ImageSei;

public class ImageClient {
    public static void main(String... args) throws MalformedURLException {
        URL url = new URL("http://localhost:8080/image?wsdl");
        QName qname = new QName("http://impl.service.soap/", "ImageImplService");
        Service service = Service.create(url, qname);
        ImageSei sei = service.getPort(ImageSei.class);
        ImageWrapper iw = sei.getImage();// This is ok
        System.out.println(iw.getImg()); // * This is null
    }
}

========================================================================

Update Even if I change the byte array in ImageWrapper to a String, it still comes back as 'null' in the client. I have my web service set to use 'Document' style also.

5
  • 1
    I might be mistaken here and not familiar with jax but your interface object (the one getting serialized and being transfered) does not contain public data (only a method to get private data). shouldn't your byte[] be a public field or property. Commented Jul 23, 2011 at 17:15
  • 1
    The fact that ImageWrapper iw = sei.getImage(); succeeded does not prove that iw isn't null; it only proves sei isn't null. Can you verify that iw != null? Commented Jul 23, 2011 at 17:18
  • Hi Eddy. You fixed my problem! Can you post this as an answer please so I can mark it as the correct answer? I didn't know that the property had to be public... Commented Jul 23, 2011 at 17:19
  • In fact, unless I am mistaken, an attempt to print a null object results in the String "null" being printed. So your problem isn't what you think. Commented Jul 23, 2011 at 17:21
  • Yes, I verified that the ImageWrapper iw was not null. This was coming across from the service to the client in one piece and was NOT null. It was only the internal byte array property that was null. Commented Jul 23, 2011 at 17:22

1 Answer 1

3

Your interface object (the one getting serialized and being transfered) does not contain public data (only a method to get private data). Your byte[] should be a public field or property to be included in the serialized data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.