Jersey:将标头添加到服务器响应中

时间:2013-03-17 19:55:48

标签: java jersey jax-rs

我正在使用泽西岛,我有以下方法:

@POST
@Path("hello")
@Produces(MediaType.TEXT_HTML)
public String hello(@FormParam("username") String username)
{
    Gson gson = new Gson();
    CommunicationResponseM result = new CommunicationResponseM();

    String result = "hello";

    return gson.toJson(result);
}

到目前为止一切顺利,但现在我需要添加一些标题。我怎么能这样做?

谢谢!

PS:
我以这种方式启动Jersey服务器:

    final HttpServer server = HttpServerFactory.create(baseUrl);
    server.start();

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:2)

如果您正在寻找从http请求获取标头参数值的方法,那么您可以使用@HeaderParam注释。它类似于@FormParam注释。

如果您希望在回复中添加标题,可以采用多种方式。

对于Jersey 1,Jersey 1.18 user guide中有更多信息。见2.5和2.13节。

对于Jersey 2 user guide,请参阅第3章和第3.6节。

相关问题