将字节数组从Web服务发送到客户端

时间:2011-07-23 17:00:45

标签: soap jax-ws nullpointerexception

我想将一个字节数组从Web服务发送到请求通过该服务公开的操作的客户端。在我的方法中,我将图像读入字节数组。我认为将这个字节数组放入包装器POJO中。这是操作的返回类型。

@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;
}

我可以在客户端ok中收到ImageWrapper对象。正如我所料,它与服务器上的Web服务创建的ImageWrapper实例具有不同的ID。但是,问题是当我尝试从ImageWrapper获取byte []数组时,它是null ...任何想法为什么?包装器类看起来像:

package soap.service.model;

public class ImageWrapper {
    private byte[] content;

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

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

,客户端看起来像:

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
    }
}

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

更新即使我将ImageWrapper中的字节数组更改为String,也是如此 在客户端仍然以'null'的形式返回。我有我的Web服务设置使用 '文档'风格也。

1 个答案:

答案 0 :(得分:2)

您的接口对象(被序列化和被转移的对象)不包含公共数据(只有获取私有数据的方法)。您的byte []应该是要包含在序列化数据中的公共字段或属性

相关问题