Java 使用restful方法重定向到页面?

Java 使用restful方法重定向到页面?,java,rest,redirect,Java,Rest,Redirect,我创建了一个页面,要求用户填写一些表单字段,当用户提交时,表单将发送到一个Restful方法,您可以在下面看到: @POST @Path("addUser") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public void addUser(@FormParam("username") String username, @FormParam("password") String password, @Form

我创建了一个页面,要求用户填写一些表单字段,当用户提交时,表单将发送到一个Restful方法,您可以在下面看到:

@POST
@Path("addUser")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void addUser(@FormParam("username") String username,
        @FormParam("password") String password,
        @FormParam("id") String id,
        @FormParam("group_name") String groupName,
        @FormParam("authority_name") String authorityName,
        @FormParam("authority_id") String authorityId
        )
{
    //Something will be done here
}


如何将此函数末尾的用户重定向到(比如)index.jsp?

使用
javax.ws.rs.core.UriBuilder
创建一个URI,该URI映射要保留的参数和其他数据。然后使用
Response.temporaryRedirect
将重定向返回到客户端,并将您构建的URI传递给它。

最后,我得出这样的结论:除了我所做的以外,没有其他方法了:
因此,我的解决方案如下:

try {
        java.net.URI location = new java.net.URI("../index.jsp?msg=A_User_Added");
        throw new WebApplicationException(Response.temporaryRedirect(location).build());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }


通过将这个块添加到我的代码中,我得到了我所需要的。希望它也能帮助您。

使用“WebApplicationException”来重定向请求不是个好主意。在Jersey(2.4.1)中,您应该能够通过普通的servlet方式重定向请求,(request.getServletContext().getRequestDispatcher().forward()或just response.sendRedirect())

以下是Jersey如何处理该请求

org.glassfish.jersey.servlet.ServletContainer.service(HttpServletRequest request, HttpServletResponse response)
      requestScope.runInScope
           final ContainerResponse response = endpoint.apply(data)
                  methodHandler.invoke(resource, method, args);
           Responder.process(ContainerResponse);
methodHandler是您的REST服务类,method是该服务类中的函数

重定向页面的步骤变为straitforward

  • 通过类字段或函数参数中的Jersey注入(@Context HttpServletRequest,@Context HttpServletResponse)获取(请求,响应)

  • 调用request.getServletContext().getRequestDispatcher()获取“转发”的调度程序 或者使用Response.sendRedirect(url)

  • 一旦应用程序返回(仅为null),Jersey将尝试在“Responder.process(ContainerResponse)”中处理结果。在这一步中,它将使用response来设置状态(您的null返回没有内容)

    所以这里的关键点是,在从服务函数返回之前,必须完成/关闭响应对象。否则,Jersey可能会覆盖您的输出


    关于为什么“WebApplicationException”会覆盖Jersey响应的小提示。这是因为org.glassfish.jersey.server.ServerRuntime.mapException()将使用“webApplicationException.getResponse()”作为返回响应结果

    像这样更改代码,addUser()应该返回一个响应对象

    public Response addUser(@FormParam("username") String username,
            @FormParam("password") String password,
            @FormParam("id") String id,
            @FormParam("group_name") String groupName,
            @FormParam("authority_name") String authorityName,
            @FormParam("authority_id") String authorityId
            )
    {
        //Something will be done here
    
        java.net.URI location = new java.net.URI("../index.jsp?msg=A_User_Added");
        return Response.temporaryRedirect(location).build()
    
    }
    

    请参见以下web服务中重定向的用法:

    public class LoginWebService {
    
        @POST
        @Path("/check")
        public Response checkDetails(@FormParam("name") String name,@FormParam("pass") String pass ) throws URISyntaxException  {
    
            URI uri = new URI("/login/success");
            URI uri2= new URI("http://localhost:9090/NewWebServiceproject/new/login/failure");
    
            if(name.equals("admin") && pass.equals("pass"))
        //@Path("http://localhost:8010/NewWebServiceproject/new/login/success");
                {
                return Response.temporaryRedirect(uri).build();
                //Response.seeOther(uri);
                //return Response.status(200).entity("user successfully login").build();
                }
            else
            {
                return Response.temporaryRedirect(uri2).build();
                //Response.seeOther(uri2);
                //return Response.status(200).entity("user logon failed").build();
                }
        }
        @POST
        @Path("/success")
        public Response successpage()
        {
        return Response.status(200).entity("user successfully login").build();
        }
        @POST
        @Path("/failure")
        public Response failurepage()
        {
        return Response.status(200).entity("user logon failed").build();
        }
    }
    

    您引入的这个类“UriBuilder”有太多未实现的方法。不能简单地从中实例化实例。我使用了'java.net.URI location=new java.net.URI(“../index.jsp”);响应。临时重定向(位置);'我认为我们应该添加这样的代码行:抛出新的WebApplicationException(Response.temporaryRedirect(location.build());没关系。这个问题现在已经很长了。这个问题对我来说也是一个java RS初学者,在我通过数小时的搜索、研究源代码和调试得出上述答案之前。这就是为什么我列出了我的发现,以节省别人的时间,避免错误的方法,因为我以前找不到一个非常清楚的答案。