[英]Jersey Viewable with Json
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应用程序,该类是模板名称和模型对象的容器。 It is used like this:
它的用法如下:
@GET
@Template
@Produces({MediaType.TEXT_HTML})
public Viewable get() {
JsonObject response = null;
try{
response = service.getDetails(id);
}
catch(Exception ex) {
log.error("failed to get details", ex);
throw ex;
}
return new Viewable("/test", response);
}
this is right way to send the json from Viewable? 这是从Viewable发送json的正确方法吗? Is there a way to set a json object explicitly?
有没有一种方法可以显式设置json对象?
A few things: I don't have any experience using Viewable
in particular, but I am familiar with JAX-RS and can probably throw a couple of pointers your way. 几件事:我没有特别使用
Viewable
经验,但是我对JAX-RS很熟悉,可能会以自己的方式抛出几个指针。
JAX-RS defines a feature for mapping exceptions to responses. JAX-RS定义了将异常映射到响应的功能。 This functionality is nice for removing those exception blocks from your resource code.
此功能非常适合从资源代码中删除那些异常块。 Check out the Jersey docs on this topic for a tutorial on how to register these.
查看有关此主题的Jersey文档 ,以获取有关如何注册这些内容的教程。 A quick summary is: 1) implement
ExceptionMapper
and 2) register the class as a Provider. 一个简短的摘要是:1)实现
ExceptionMapper
和2)将类注册为提供程序。
For starters, I recommend creating a simple suite that maps to common HTTP codes . 对于初学者,我建议创建一个映射到常见HTTP代码的简单套件。 For example:
例如:
NotFoundException
- returns a 404 response and is used when a single entity is requested but not found. NotFoundException
返回404响应,并在请求但未找到单个实体时使用。 InvalidInputException
- returns a 422 response and is used when a request does not pass validation (like trying to save an phone number in an email field). InvalidInputException
返回422响应,并在请求未通过验证时使用(例如尝试将电话号码保存在电子邮件字段中)。 BadRequestException
- usually the framework will handle these situations for you, but if not, a Bad Request is one that is not formatted properly. BadRequestException
通常,框架将为您处理这些情况,但如果不是,则Bad Request的格式不正确。 So if a required header is missing, or if a client tries to save a collection when only a single entity is allowed. Exception
* - There is a star here because an unexpected exception is usually due to a server error, so 500 is an appropriate default response. Exception
*-这里有一个星号,因为意外异常通常是由于服务器错误引起的,因此500是适当的默认响应。 A reason you may want to create a global uncaught exception handler is to prevent the stacktrace from being returned in the response body. You should not need the @Template
annotation if you are using the Viewable
object. 你不应该需要
@Template
如果您使用的是标注Viewable
对象。 Also, Viewable
is expecting a template as the first argument and a model (map) as the second argument. 此外,
Viewable
期望模板作为第一个参数,而模型(映射)作为第二个参数。 The model should have keys that match variables in your JSP. 该模型应具有与JSP中的变量匹配的键。 Right now your method will look for a file called
test.jsp
in the root of whatever your template config is set to in web.xml. 现在,您的方法将在web.xml中设置模板配置的根目录中查找一个名为
test.jsp
的文件。 If you take all of that into consideration, your method could look something like this: 如果考虑到所有这些,您的方法可能看起来像这样:
@GET
@Produces(MediaType.TEXT_HTML)
public Viewable getMobileReport() {
return new Viewable("/test", service.getMobileReport(id));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.