我创建了一个页面,要求用户填写一些表单字段,当他提交时,表单将发送到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?
像这样更改代码,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()
}
使用映射要保留的参数和其他数据的javax.ws.rs.core.UriBuilder
创建 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();
}
通过将这个块添加到我的代码中,我得到了我需要的东西。希望它也能帮助你。
请参阅下面 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"来重定向请求不是好主意。 在泽西岛(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);
该方法处理程序是您的 REST 服务类,方法是该服务类中的函数。
重定向页面的步骤变得简单明了
通过类字段或函数参数中的 Jersey 注入(@Context HttpServletRequest 请求,@Context HttpServletResponse 响应)获取(请求、响应)
调用request.getServletContext().getRequestDispatcher()以获取"forward"的调度程序或使用 Response.sendRedirect(url)
一旦您的应用程序被返回(只是空),Jersey将尝试在"Responder.process(ContainerResponse)"中处理结果。在此步骤中,它将使用响应来设置状态(204 null 返回没有内容)。
因此,这里的关键点是您必须在从服务函数返回之前完成/关闭响应对象。否则,泽西岛可能会覆盖您的输出。
关于为什么"WebApplicationException"可以覆盖Jersey repsponse的小提示。这是因为org.glassfish.jersey.server.ServerRuntime.mapException()将使用"webApplicationException.getResponse()"作为返回响应结果。