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

时间:2012-06-20 09:35:54

标签: java rest redirect

我创建了一个页面,要求用户填写一些表单字段,当他提交时,表单会被发送到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?

5 个答案:

答案 0 :(得分:13)

像这样更改你的代码,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()

}

答案 1 :(得分:9)

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

答案 2 :(得分:4)

最后,我得出的结论是,除了我所做的之外别无他法: 所以这是我的解决方案:

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


通过将此块添加到我的代码中,我得到了我需要的东西。希望它也能帮到你。

答案 3 :(得分:2)

请参阅下面在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();
    }
}

答案 4 :(得分:1)

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

以下是泽西岛处理请求的方式

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服务类,方法是该服务类中的函数。

重定向页面的步骤变得简单

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

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

  3. 返回应用程序后(只是null),Jersey将尝试在“Responder.process(ContainerResponse)”中处理结果。在此步骤中,它将使用响应来设置状态(204没有内容为您的null返回)。

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

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