[英]Jersey Viewable with status code
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. JAX-RS实现Jersey通过
Viewable
类支持MVC样式的Web应用程序, Viewable
类是模板名称和模型对象的容器。 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. 上述内容将隐含地返回
200
,但这在任何情况下都不合适。 Is there a way to set a status code explicitly? 有没有办法明确设置状态代码?
You will have to return a Response
set up with the correct status code and headers containing your Viewable
, eg: 您必须返回一个
Response
设置,其中包含正确的状态代码和包含Viewable
标题,例如:
@GET
public Response get() {
return Response.status(myCode).entity(new Viewable("/index", "FOO")).build();
}
Hmm you can create custom Response object in jersey thusly: this will return a 200: 嗯,你可以在泽西岛创建自定义的Response对象:这将返回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.... 希望有帮助....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.