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

我创建了一个页面,要求用户填写一些表单字段,当他提交时,表单将被发送到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,映射您要保留的参数和其他数据。 然后使用Response.temporaryRedirect将重定向返回给客户端并将其传递给您构建的URI。

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

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

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

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

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

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

使用“WebApplicationException”来重定向请求不是一个好主意。 在Jersey(2.4.1)中,你应该能够通过正常的servlet方式重定向请求,(request.getServletContext()。getRequestDispatcher()。forward()或者只是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是该服务类中的函数。

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

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

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

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

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

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