【问题标题】:Getting HttpServletResponse Null In Restful Service在 Restful 服务中获取 HttpServletResponse Null
【发布时间】:2018-05-28 20:16:14
【问题描述】:

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

当我这样调用函数时:

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

我得到的HttpServletResponse 始终为空

@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");
    }
}

我有什么遗漏吗?

【问题讨论】:

  • This 可能会有所帮助。

标签: java rest servlets


【解决方案1】:

我终于做到了,只需要将@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");
    }
}

【讨论】:

    猜你喜欢
    • 2013-03-03
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 2019-04-17
    • 2011-03-31
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多