具有多部分/混合响应的 HTTP 状态代码 201

发布于 2024-12-28 01:03:24 字数 339 浏览 0 评论 0原文

我正在尝试创建一个 REST 服务,定义为:

@POST
@Path("/path")
@Consumes(MediaType.APPLICATION_XML)
@Produces("multipart/mixed")
public MultipartOutput create(MyObject o) {}

输出的第一部分是 XML,第二部分是文本文件。在 XML 中,我有所需的状态代码,但如何使用它来创建 HTTP 状态代码?目前我收到 200,我需要 201。

(对于其他服务,没有 MultipartOutput,响应是使用 ResponseBuilder 创建的)。

I'm trying to create a REST service defined as:

@POST
@Path("/path")
@Consumes(MediaType.APPLICATION_XML)
@Produces("multipart/mixed")
public MultipartOutput create(MyObject o) {}

First part of the output will be an XML, second part a text file. In the XML I have the status code that I need, but how can I use it to create the HTTP status code? Currently I receive 200, I need 201.

(For other services, without MultipartOutput, the response is created using ResponseBuilder).

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

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

发布评论

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

评论(1

忘年祭陌 2025-01-04 01:03:24

<罢工>
您可以使用@Context访问HttpServletResponse

尝试更改您的方法签名并调用setStatus

public MultipartOutput create(MyObject o, @Context HttpServletResponse response) {
    MultipartOutput mpo = ...
    response.setStatus(201);

    return mpo;
}

编辑:

尝试将org.jboss.resteasy.spi.HttpResponsePostProcessInterceptor一起使用:

import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.interception.PostProcessInterceptor;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;

public class MultipartTest {
    @XmlRootElement(name = "MyObject")
    static class MyObject {}

    @Path("/")
    public static class Service {
        @Context HttpResponse response;

        @GET
        @Path("/mpo")
        public MultipartOutput create() {
            response.setStatus(201);

            final MultipartOutput mpo = new MultipartOutput();
            mpo.addPart(new MyObject(), MediaType.APPLICATION_XML_TYPE);

            return mpo;
        }
    }

    public static class Interceptor implements PostProcessInterceptor {
        @Context HttpResponse response;

        @Override
        public void postProcess(ServerResponse serverResponse) {
            serverResponse.setStatus(response.getStatus());
        }
    }

    @Test
    public void test() throws Exception {
        Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getRegistry().addSingletonResource(new Service());
        dispatcher.getProviderFactory().getServerPostProcessInterceptorRegistry().register(new Interceptor());

        MockHttpRequest request = MockHttpRequest.get("/mpo");
        MockHttpResponse response = new MockHttpResponse();

        dispatcher.invoke(request, response);

        Assert.assertEquals(201, response.getStatus());
    }
}


You can use the @Context to access the HttpServletResponse.

Try changing your method signature and call setStatus:

public MultipartOutput create(MyObject o, @Context HttpServletResponse response) {
    MultipartOutput mpo = ...
    response.setStatus(201);

    return mpo;
}

EDIT :

Try using org.jboss.resteasy.spi.HttpResponse instead together with a PostProcessInterceptor:

import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.interception.PostProcessInterceptor;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;

public class MultipartTest {
    @XmlRootElement(name = "MyObject")
    static class MyObject {}

    @Path("/")
    public static class Service {
        @Context HttpResponse response;

        @GET
        @Path("/mpo")
        public MultipartOutput create() {
            response.setStatus(201);

            final MultipartOutput mpo = new MultipartOutput();
            mpo.addPart(new MyObject(), MediaType.APPLICATION_XML_TYPE);

            return mpo;
        }
    }

    public static class Interceptor implements PostProcessInterceptor {
        @Context HttpResponse response;

        @Override
        public void postProcess(ServerResponse serverResponse) {
            serverResponse.setStatus(response.getStatus());
        }
    }

    @Test
    public void test() throws Exception {
        Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getRegistry().addSingletonResource(new Service());
        dispatcher.getProviderFactory().getServerPostProcessInterceptorRegistry().register(new Interceptor());

        MockHttpRequest request = MockHttpRequest.get("/mpo");
        MockHttpResponse response = new MockHttpResponse();

        dispatcher.invoke(request, response);

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