我正在使用REST服务进行测试,我需要从中获取一个HttpServletResponse对象。

当我这样调用函数时:

http://localhost:8080/wsService/api/servicetest/testify

我得到的HttpServletResponse始终为null

@Path("/testify")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response Testify(HttpServletResponse response) throws Exception {
    try {
        return Utilities.getSvc(true, "OK");

    } catch (Exception e) {
        return Utilities.getSvc(false, "ERROR");
    }
}


有什么我想念的吗?

最佳答案

我终于做到了,只需要将@Context批注放入HttpServletResponse对象并添加HttpServletRequest参数:

@Path("/testify")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response Testify(@Context HttpServletResponse response,
                        @Context HttpServletRequest request) throws Exception {
    try {
        return Utilities.getSvc(true, "OK");

    } catch (Exception e) {
        return Utilities.getSvc(false, "ERROR");
    }
}

10-06 13:50