使用REST JAX-RS实现满足特定条件时如何重定向用户? [英] How to redirect user when certain condition is met using REST JAX-RS implementation?
问题描述
我以前只使用可以执行查询的Tomcat和JSP页面,然后将查询结果分配给数组或对象,然后通过响应将数据传递到客户端.
I used to just use Tomcat and JSP pages which I can execute query, then assign query result into the array or object then pass that data onto client side via response.
request.setAttribute("errorMessage", "this is error!!");
request.getRequestDispatcher("report.jsp").forward(request, response);
在客户端jsp代码中,我可以做类似的事情:
In client jsp code, I could do something like:
$ {errorMessage}
然后这是错误!"消息将会显示.
Then the "this is error!!" message would show up.
我想对REST JAX-RS GlassFish v3做同样的事情.
I want to do the same thing with REST JAX-RS GlassFish v3.
@Path("schedule/test")
@POST
@Consumes("application/x-www-form-urlencoded")
@Produces("application/vnd.ms-excel")
public Object tmpTest(String content) {
try {
//just my method to execute query and get result
Vector out = (Vector)QueryManager.executeQuery;
//if query result is empty, I want user to redirect to report.jsp page
if(out.isEmpty()) {
request.setAttribute("errorMessage", "This is error!!");
request.getRequestDispatcher("report.jsp").forward(request, response);
return null;
}
....continue code......
}
这会导致我从未见过的神秘异常.
This results in mysterious exception I've never seen.
java.lang.ClassCastException: $Proxy109 cannot be cast to org.apache.catalina.core.ApplicationHttpRequest
at org.apache.catalina.core.ApplicationHttpRequest.getRequestFacade(ApplicationHttpRequest.java:1001)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:472)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:379)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:336)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:314)
那我该如何将用户重定向到report.jsp并传递诸如这是错误"之类的消息?
客户端jsp希望错误msg变量具有一个值:
The client jsp expects the error msg variable to have a value:
<b>${errorMessage}</b>
推荐答案
那不是RESTful的.您需要使用 WebApplicationException
特定的状态代码,以便客户了解到底出了什么问题.例如.当实际上是服务器的错误时:
That's not RESTful. You need to throw a WebApplicationException
with a specific status code so that the client understands what exactly went wrong. E.g. when it's actually the server's mistake:
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
或者是在所有客户的错误之后:
Or when it was after all client's mistake:
throw new WebApplicationException(Response.Status.BAD_REQUEST);
有关概述,另请参见 HTTP状态代码定义.
您正面临的ClassCastException
之所以会发生,是因为分派的request
实际上不是servlet容器提供的实现的实例(在这种情况下,是Tomcat或Tomcat-fork之一) .毕竟,您不应该这样做.您正在开发REST Web服务,而不是JSP/Servlet网站.这是两个不同的世界.
The ClassCastException
which you're facing is by the way occurring because the dispatched request
is actually not an instance of the servletcontainer-provided implementation (in this particular case, the one of Tomcat or a Tomcat-fork). After all, you shouldn't be doing it this way. You're developing a REST webservice, not a JSP/Servlet website. It are two distinct worlds.
这篇关于使用REST JAX-RS实现满足特定条件时如何重定向用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!