将数据POST到其他URI,并在REST中更改URL(POST和重定向)

时间:2016-03-01 09:47:55

标签: java rest jax-rs

我需要POST数据,同时在REST环境中重定向到该URL。我可以为普通字符串执行此操作,但要求是POST特定对象。

我对普通字符串的处理方式是 -

public Response homePage(@FormParam("username") String username,
        @FormParam("passwordhash") String password) {
    return Response.ok(PreparePOSTForm(username)).build();
}

private static String PreparePOSTForm(String username)
{
    //Set a name for the form
    String formID = "PostForm";
    String url = "home";

    //Build the form using the specified data to be posted.
    StringBuilder strForm = new StringBuilder();
    strForm.append("<form id=\"" + formID + "\" name=\"" + 
                    formID + "\" action=\"" + url + 
                    "\" method=\"POST\">");

   strForm.append("<input type=\"hidden\" name=\"" + "username" + 
                        "\" value=\"" + username + "\">");

    strForm.append("</form>");
    //Build the JavaScript which will do the Posting operation.
    StringBuilder strScript = new StringBuilder();
    strScript.append("<script language=\"javascript\">");
    strScript.append("var v" + formID + " = document." + 
                        formID + ";");
    strScript.append("v" + formID + ".submit();");
    strScript.append("</script>");
    //Return the form and the script concatenated.
    //(The order is important, Form then JavaScript)
    return strForm.toString() + strScript.toString();
}

但是这种方法不会发送对象。我需要一个解决方案来发送复杂的对象。请帮我解决这个问题。

提前致谢。

0 个答案:

没有答案