Jersey 可通过状态代码查看

发布于 2024-09-11 13:51:33 字数 334 浏览 2 评论 0原文

JAX-RS 实现 Jersey 通过 Viewable 类支持 MVC 样式 Web 应用程序,该类是模板名称和模型对象的容器。它是这样使用的

@GET
public Viewable get() {
  return new Viewable("/index", "FOO");
}

我想知道如何用这种方法返回状态代码。上面的代码会隐式返回 200,但这在任何情况下都是不合适的。有没有办法显式设置状态代码?

The JAX-RS implementation Jersey supports MVC style web applications through the Viewable class, which is a container for a template name and a model object. It is used like this:

@GET
public Viewable get() {
  return new Viewable("/index", "FOO");
}

I wonder how a status code could be returned with this approach. The above would implicitly return 200, but that wouldn't be appropriate in any case. Is there a way to set a status code explicitly?

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

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

发布评论

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

评论(2

最偏执的依靠 2024-09-18 13:51:33

您必须返回一个 Response,其中设置了正确的状态代码和包含您的 Viewable 的标头,例如:

@GET
public Response get() {
  return Response.status(myCode).entity(new Viewable("/index", "FOO")).build();
}

You will have to return a Response set up with the correct status code and headers containing your Viewable, e.g.:

@GET
public Response get() {
  return Response.status(myCode).entity(new Viewable("/index", "FOO")).build();
}
夏末染殇 2024-09-18 13:51:33

嗯,您可以这样在球衣中创建自定义 Response 对象:
这将返回 200:

@GET
public Response get() {
    URI uri=new URI("http://nohost/context");
    Viewable viewable=new Viewable("/index", "FOO");
    return Response.ok(viewable).build();
}

要返回不同的内容,请使用此方法:

@GET
public Response get() {
    int statusCode=204;
    Viewable myViewable=new Viewable("/index","FOO");
    return Response.status(statusCode).entity(myViewable).build();
}

希望有帮助......

Hmm you can create custom Response object in jersey thusly:
this will return a 200:

@GET
public Response get() {
    URI uri=new URI("http://nohost/context");
    Viewable viewable=new Viewable("/index", "FOO");
    return Response.ok(viewable).build();
}

to return something different use this approach:

@GET
public Response get() {
    int statusCode=204;
    Viewable myViewable=new Viewable("/index","FOO");
    return Response.status(statusCode).entity(myViewable).build();
}

Hope that helped....

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