Java 如何使用为服务器定义的相同接口编写Restful客户端

Java 如何使用为服务器定义的相同接口编写Restful客户端,java,scala,resteasy,Java,Scala,Resteasy,我正在使用Scala编写一个Restful服务 在服务器端,它有一个接口: trait ICustomerService { @GET @Path("/{id}") @Produces(Array("application/xml")) def getCustomer(@PathParam("id") id: Int): StreamingOutput } class CustomerServiceProxy(url : String) { RegisterBuiltin.

我正在使用Scala编写一个Restful服务

在服务器端,它有一个接口:

trait ICustomerService {
  @GET
  @Path("/{id}")
  @Produces(Array("application/xml"))
  def getCustomer(@PathParam("id") id: Int): StreamingOutput
}
class CustomerServiceProxy(url : String) {
  RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
  val proxy = ProxyFactory.create(classOf[ICustomerService], url)

  def getCustomer(id: Int): Customer = {
    val streamingOutput = proxy.getCustomer(id)
    <Problem here>
  }
}
该服务运行良好,我使用web浏览器对其进行了测试

现在我想为这个接口编写一些自动测试。我需要使用相同的接口编写RESTEasy客户端:

trait ICustomerService {
  @GET
  @Path("/{id}")
  @Produces(Array("application/xml"))
  def getCustomer(@PathParam("id") id: Int): StreamingOutput
}
class CustomerServiceProxy(url : String) {
  RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
  val proxy = ProxyFactory.create(classOf[ICustomerService], url)

  def getCustomer(id: Int): Customer = {
    val streamingOutput = proxy.getCustomer(id)
    <Problem here>
  }
}
class CustomerServiceProxy(url:String){
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
val proxy=ProxyFactory.create(类[ICCustomerService],url)
def getCustomer(id:Int):客户={
val streamingOutput=proxy.getCustomer(id)
}
}
此代码不工作,因为流式输出仅允许写入

如何编写这个测试类,以便从客户端获得服务器写入streamingoutput的内容


非常感谢

流输出不允许写入,它执行写入。您只需制作自己的OutputStream来捕获它:

/**
 * Re-buffers data from a JAXRS StreamingOutput into a new InputStream
 */
def rebuffer(so: StreamingOutput): InputStream = {
  val os = new ByteArrayOutputStream
  so.write(os)
  new ByteArrayInputStream(os.toByteArray())
}


def getCustomer(id: Int): Customer = {
  val streamingOutput = proxy.getCustomer(id)
  val inputStream = rebuffer(streamingOutput)
  inputStream.read() // or pass it to an XML parser or whatever
}
希望这有帮助