如何使用 REST JAX-RS 实现在满足特定条件时重定向用户?

发布于 2024-10-20 02:19:21 字数 1888 浏览 2 评论 0原文

我过去只使用 Tomcat 和 JSP 页面来执行查询,然后将查询结果分配到数组或对象中,然后通过响应将该数据传递到客户端。

request.setAttribute("errorMessage", "this is error!!");
request.getRequestDispatcher("report.jsp").forward(request, response);

在客户端 jsp 代码中,我可以执行以下操作:

${errorMessage}

然后“这是错误!!”消息就会出现。

我想对 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......
   }

这会导致我从未见过的神秘异常。

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期望错误消息变量有一个值:

<b>${errorMessage}</b>

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);

In client jsp code, I could do something like:

${errorMessage}

Then the "this is error!!" message would show up.

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)

So how can I redirect a user to report.jsp and also pass message like "This is error" ?

The client jsp expects the error msg variable to have a value:

<b>${errorMessage}</b>

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

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

发布评论

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

评论(2

谈下烟灰 2024-10-27 02:19:21

那不是宁静的。您需要抛出 WebApplicationException 带有特定的状态代码,以便客户端了解到底出了什么问题。例如,当它实际上是服务器的错误时:

throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);

或者当它是所有客户端的错误时:

throw new WebApplicationException(Response.Status.BAD_REQUEST);

另请参阅 HTTP 状态代码定义以获取概述。


顺便说一句,您所面临的 ClassCastException 是因为分派的 request 实际上不是 servletcontainer 提供的实现的实例(在这种特殊情况下,是Tomcat 或 Tomcat-fork)。毕竟,你不应该这样做。您正在开发 REST Web 服务,而不是 JSP/Servlet 网站。这是两个截然不同的世界。

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);

See also HTTP status code definitions for an overview.


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.

我的影子我的梦 2024-10-27 02:19:21

如前所述,您应该尝试 WebApplicationException。

我相信这会给您想要的答案:
试试这个:

        if(out.isEmpty()) {
             java.net.URI location = new java.net.URI("report.jsp");
             throw new WebApplicationException(Response.seeOther(location).build());
         }

As mentioned before, you should try WebApplicationException.

I believe you this would give you your desired answer:
Try this:

        if(out.isEmpty()) {
             java.net.URI location = new java.net.URI("report.jsp");
             throw new WebApplicationException(Response.seeOther(location).build());
         }

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