我想在JAX-WS中直接传递BufferedInputStream
我创建了这个网络服务
@WebMethod(operationName = "upload")
public String upload(@WebParam(name = "file") BufferedInputStream file) {
//TODO write your implementation code here:
return null;
}
但是,当我添加Web服务客户端时,IDE会在ws包中生成一个自己的Web服务类。
package com.ws;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "bufferedInputStream")
public class BufferedInputStream
extends FilterInputStream
{
}
并且服务中的上传方法只接受此类,而不接受java.io.BufferedInputStream,因此我无法将文件传递给服务。
然后我将 Web 服务更改为此,并创建自己的类来包含 BufferedInputStream,以便我可以将 java.io.BufferedInputStream 设置到 myFile 中。
@WebMethod(operationName = "upload")
public String upload(@WebParam(name = "myFile") MyFile myFile) {
//TODO write your implementation code here:
return null;
}
package com.ws;
import java.io.BufferedInputStream;
public class MyFile {
private java.io.BufferedInputStream bis;
public MyFile(BufferedInputStream bis)
{
this.bis=bis;
}
public BufferedInputStream getBis() {
return bis;
}
public void setBis(BufferedInputStream bis) {
this.bis = bis;
}
}
但是,当我添加 Web 服务客户端时,IDE 会生成 com.ws.BufferedInputStream 并在 MyFile 中使用它。
package com.ws;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "myFile", propOrder = {
"bis"
})
public class MyFile {
protected BufferedInputStream bis;
public BufferedInputStream getBis() {
return bis;
}
public void setBis(BufferedInputStream value) {
this.bis = value;
}
}
所以我手动更正类型。
package com.ws;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "myFile", propOrder = {
"bis"
})
public class MyFile {
protected java.io.BufferedInputStream bis;
public java.io.BufferedInputStream getBis() {
return bis;
}
public void setBis(java.io.BufferedInputStream value) {
this.bis = value;
}
}
但是,当我重建项目时。所有java.io.BufferedInputStream都会变回com.ws.BufferedInputStream
如何修改代码以将 BufferedInputStream 传递给 jax-ws?
<小时/>现在我按照本教程创建 JAXB 类
https://netbeans.org/kb/74/websvc/jaxb.html
我使用它作为 WSDL
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb">
<bindings scd="x-schema::tns" xmlns:tns="http://khameleon.anosym.com/service">
<bindings scd="~tns:bufferedInputStream">
<class ref="java.io.BufferedInputStream"/>
</bindings>
</bindings>
</bindings>
但是当我生成Java代码时,它提示我“编译架构时出错”。有没有什么工具可以帮助我正确生成WSDL文件?
最佳答案
在这种情况下,您需要定义 jaxb 绑定(bind)并在生成 ws 工件时将此绑定(bind)提供给 wsimport。
ws 绑定(bind)的一个示例是:
如果您使用maven,则可以使用jaxws-maven-plugin。
https://github.com/marembo2008/khameleon/blob/master/khameleon-core/pom.xml#LC44
因此,您还可以将 BufferedInputStream 的绑定(bind)文件声明为:
<bindings scd="~tns:bufferedInputStream">
<class ref="java.io.BufferedInputStream"/>
</bindings>
关于java - 如何传递 BufferedInputStream 或包含 JAX-WS 中其他对象的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24629269/