Java 如何创建一个空的虚拟 HttpResponse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2996755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create an empty dummy HttpResponse
提问by jax
I am using org.apache.http.HttpResponse
我正在使用 org.apache.http.HttpResponse
I want to create an empty dummy resposne, I am going to use this to return when errors occur instead of passing back null.
我想创建一个空的虚拟 resposne,我将使用它在发生错误时返回而不是传回 null。
I tried to create one and it has lost of weird params. Can someone tell me how to create one.
我试图创建一个,但它丢失了奇怪的参数。谁能告诉我如何创建一个。
采纳答案by Bozho
Just implement HttpResponse
with no-op methods.
只需HttpResponse
使用无操作方法实现。
回答by Gabor Farkas
Indeed, implementing a HttpServletResponse seems quite a lot work, and you'll be dependent on the actual Servlet implementation.
实际上,实现 HttpServletResponse 似乎需要做很多工作,而且您将依赖于实际的 Servlet 实现。
I think I'd use request.getRequestDispatcher("/dummy.html").forward(request, response)
我想我会用 request.getRequestDispatcher("/dummy.html").forward(request, response)
回答by Brian
Depending on what version of commons you're using, you might want to try DefaultHttpResponseFactory. This is the way that the library creates some of it's responses internally so it may or may not serve your purposes.
根据您使用的公共版本,您可能想尝试DefaultHttpResponseFactory。这是库在内部创建一些响应的方式,因此它可能会也可能不会满足您的目的。
import org.apache.http.HttpStatus;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseFactory;
import org.apache.http.HttpVersion;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.message.BasicStatusLine;
HttpResponseFactory factory = new DefaultHttpResponseFactory();
HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null);