如何创建一个空的虚拟 HttpResponse

发布于 2024-09-05 11:30:56 字数 129 浏览 4 评论 0原文

我正在使用 org.apache.http.HttpResponse

我想创建一个空的虚拟响应,我将使用它在发生错误时返回而不是传回 null。

我尝试创建一个,但它丢失了奇怪的参数。有人可以告诉我如何创建一个。

I am using 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.

I tried to create one and it has lost of weird params. Can someone tell me how to create one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

根据您使用的 commons 版本,您可能需要尝试 默认HttpResponseFactory。这是图书馆在内部创建一些响应的方式,因此它可能会或可能不会满足您的目的。

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);

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.

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);
没企图 2024-09-12 11:30:56

只需使用无操作方法实现 HttpResponse 即可。

Just implement HttpResponse with no-op methods.

娇纵 2024-09-12 11:30:56

此版本添加了实体内容,并且更加紧凑:

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion.HTTP_1_1;
import org.apache.http.entity.ContentType.TEXT_HTML;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpResponse;
import org.springframework.http.HttpStatus;

public static HttpResponse buildResponse(HttpStatus status, String text){
    
       HttpResponse response = new BasicHttpResponse(HTTP_1_1, status.value(), status.reasonPhrase);
       response.setEntity(new StringEntity(text, TEXT_HTML));
       return response;
    }

请注意,我使用 Spring HttpStatus 只是为了方便,但可能会通过参数进行更改

This version add the entity content and is a little bit more compact:

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion.HTTP_1_1;
import org.apache.http.entity.ContentType.TEXT_HTML;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpResponse;
import org.springframework.http.HttpStatus;

public static HttpResponse buildResponse(HttpStatus status, String text){
    
       HttpResponse response = new BasicHttpResponse(HTTP_1_1, status.value(), status.reasonPhrase);
       response.setEntity(new StringEntity(text, TEXT_HTML));
       return response;
    }

Note that I am using the Spring HttpStatus just for my convenience but may be changed with parameters

绮筵 2024-09-12 11:30:56

事实上,实现 HttpServletResponse 似乎需要大量工作,并且您将依赖于实际的 Servlet 实现。

我想我会使用 request.getRequestDispatcher("/dummy.html").forward(request, response)

Indeed, implementing a HttpServletResponse seems quite a lot work, and you'll be dependent on the actual Servlet implementation.

I think I'd use request.getRequestDispatcher("/dummy.html").forward(request, response)

Spring初心 2024-09-12 11:30:56

您可以创建一个返回带有状态代码的 HttpResponse 的方法:

public HttpResponse<String> createDummyResponse(int statusCode) {
    return new HttpResponse<>() {
        @Override
        public int statusCode() {
            return statusCode;
        }

        @Override
        public HttpRequest request() {
            return null;
        }

        @Override
        public Optional<HttpResponse<String>> previousResponse() {
            return Optional.empty();
        }

        @Override
        public HttpHeaders headers() {
            return null;
        }

        @Override
        public String body() {
            return null;
        }

        @Override
        public Optional<SSLSession> sslSession() {
            return Optional.empty();
        }

        @Override
        public URI uri() {
            return null;
        }

        @Override
        public HttpClient.Version version() {
            return null;
        }
    };
}

您可以将其他属性(例如正文等)作为参数传递给方法 createDummyResponse。

You can create a method that returns HttpResponse with the status code:

public HttpResponse<String> createDummyResponse(int statusCode) {
    return new HttpResponse<>() {
        @Override
        public int statusCode() {
            return statusCode;
        }

        @Override
        public HttpRequest request() {
            return null;
        }

        @Override
        public Optional<HttpResponse<String>> previousResponse() {
            return Optional.empty();
        }

        @Override
        public HttpHeaders headers() {
            return null;
        }

        @Override
        public String body() {
            return null;
        }

        @Override
        public Optional<SSLSession> sslSession() {
            return Optional.empty();
        }

        @Override
        public URI uri() {
            return null;
        }

        @Override
        public HttpClient.Version version() {
            return null;
        }
    };
}

Other attributes (e.g. body, etc.) you could pass as parameters to the method createDummyResponse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文