【问题标题】:Send byte array from web service to client将字节数组从 Web 服务发送到客户端
【发布时间】:2011-10-11 17:31:06
【问题描述】:

我想将一个字节数组从 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;
}

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

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 中的字节数组更改为字符串,它 仍然在客户端返回为“null”。我设置了要使用的网络服务 '文档'样式也是。

【问题讨论】:

  • 我可能在这里弄错了,并且不熟悉 jax,但是您的接口对象(被序列化并被传输的对象)不包含公共数据(只是获取私有数据的一种方法)。您的 byte[] 不应该是公共字段或属性。
  • ImageWrapper iw = sei.getImage(); 成功的事实并不能证明iw 不为空;它只证明sei 不为空。你能验证iw != null吗?
  • 嗨,艾迪。你解决了我的问题!您能否将其发布为答案,以便我将其标记为正确答案?我不知道该物业必须是公开的......
  • 事实上,除非我弄错了,否则尝试打印null 对象会导致打印字符串“null”。所以你的问题不是你想的那样。
  • 是的,我确认ImageWrapper iw 不为空。这是从服务到客户端的一部分,并且不是空的。只有内部字节数组属性为空。

标签: soap jax-ws nullpointerexception


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多