gpt4 book ai didi

java - 我们可以使用Java中的 "new"关键字创建一个HttpResponse对象吗?

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:10 24 4
gpt4 key购买 nike

我正在编写 Martin Kalin 的 "Web Services Up and running" 的第 4 章书。

在 RestfulTeams 练习中,有一个方法可以为 Restful WS 返回一个简单的 HttpResponse,例如:

private Source response_to_client(String msg) {
HttpResponse response = new HttpResponse();
response.setResponse(msg);
ByteArrayInputStream stream = encode_to_stream(response);
return new StreamSource(stream);
}

但是我在Java中没有找到任何可以获取HttpResponse类的库(尽管我不认为我们可以像上面的方法那样直接创建这个对象)。

任何澄清都将有助于解决此问题。

最佳答案

我相信您正在考虑 HttpServletResponse 。您不能用它创建自己的响应对象;服务器创建它的实例并使它们可供您的应用程序使用。

但是示例中的类称为 HttpResponse,它要么是一个错误,要么是本书省略介绍的其他类。

写书很困难,所以错误可能会溜走。这就是为什么本书打印后发现的错误是 documented in an errata 。您通常会在那里找到解释,并且总是有 the source code for the book可供学习。

下载源代码,我想你会找到你要找的东西,主要是这个:

package ch04.team;

import java.io.Serializable;

// Serialized for responses on successful POSTs and PUTs
public class HttpResponse implements Serializable {
private String resp;
public void setResponse(String resp) { this.resp = resp; }
public String getResponse() { return this.resp; }
}

关于java - 我们可以使用Java中的 "new"关键字创建一个HttpResponse对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12190433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com