轻松休息响应状态+正文

10

我在我的REST服务中有以下方法:

    @POST
    @Path("/create")
    @ResponseStatus(HttpStatus.CREATED)
    @Consumes(MediaType.WILDCARD)
    public String create( .... ) {.... return json;}

我想要获得一个带有json响应体和状态码为CREATED的响应。

问题是:我不能获得CREATED状态的响应。

状态码总是OK,所以看起来"@ResponseStatus(HttpStatus.CREATED)"被忽略了...

有人能帮我解决吗?

我正在使用hibernate 4.1、spring 3.1和resteasy 2.3。


@ResponseStatus 的完整类名是什么? - eiden
它是org.springframework.web.bind.annotation.ResponseStatus。 - golinko
1个回答

15
据我所知,使用@org.springframework.web.bind.annotation.ResponseStatus无法实现此目的。
您可以从您的方法返回javax.ws.rs.core.Response
return Response
        .status(Response.Status.CREATED)
        .entity("ok")
        .build();

或者您可以注入org.jboss.resteasy.spi.HttpResponse,并直接设置状态码。

可能有更多的方法来实现这个目的,但我只知道这两种方法。

工作测试用例:

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.spi.HttpResponse;
import org.jboss.resteasy.spi.NotFoundException;
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.Response;

public class ResponseTest {


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

        @GET
        @Path("/1")
        public Response createdUsingResponse() throws NotFoundException {
            return Response
                    .status(Response.Status.CREATED)
                    .entity("ok")
                    .build();
        }

        @GET
        @Path("/2")
        public String created() throws NotFoundException {
            response.setStatus(Response.Status.CREATED.getStatusCode());
            return "ok";
        }
    }

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

        @Override
        public void postProcess(ServerResponse response) {
            if(this.response.getStatus() != 0){
                response.setStatus(this.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("/1");
            MockHttpResponse response = new MockHttpResponse();

            dispatcher.invoke(request, response);

            Assert.assertEquals(201, response.getStatus());
        }
        {
            MockHttpRequest request = MockHttpRequest.get("/2");
            MockHttpResponse response = new MockHttpResponse();

            dispatcher.invoke(request, response);

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接