2014-09-28 38 views
0

我不斷收到空值,而發送這兩個參數通與阿賈克斯一POST(試圖與海報以及):在REST接收空值資源

@POST 
@Path("/update") 
@Produces(MediaType.APPLICATION_JSON) 
public void update(String Path, String Content) { 

updateURI(Path,Content); 


    } 

路徑:http://essam.ldm.io/stor...amblog/ChannelList/ch1/post2

內容:<http://essam.ldm.io/storage/essamblog/ChannelList/ch1/post2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://crosscloud/mblog/Post>. <http://essam.ldm.io/storage/essamblog/ChannelList/ch1/post2> <http://crosscloud/mblog/owner> <https://essam.ldm.io></https:>. <http://essam.ldm.io/storage/essamblog/ChannelList/ch1/post2> <http://purl.org/dc/terms/created> <2013-03-06T16:41:18+0300^^http://www.w3.org/2001/XMLSchema#dateTime>. <http://essam.ldm.io/storage/essamblog/ChannelList/ch1/post2> <http://rdfs.org/sioc/ns#content>.

很顯然,由於格式不同,我無法將它們發送爲@QueryParam@PathParam

這是irrilevant把jQuery代碼,因爲它deosnt與海報wotk沒有,但在這裏它是:

function doUpdate(path, rdf) 
     { 
      var obj1 = {"path": path, "rdf": rdf}; 
      var sUrl = "http://localhost:8080/browsing/services/RDF/update"; 
      $.ajax({ 
       type: "POST", 
       url: sUrl, 
       contentType: "application/json; charset=utf-8", 
       data: obj1, 
       //dataType: "json", 
       async: false, 
       success: function (resp, status, xhr) { 
        $("#message").html("STATUS: " + xhr.status + " " + xhr.statusText + "\n" + resp); 
        $("#message").hide(); 
        $("#login_message").html("<font color='green'><b>Record succesfully updated</b></font>d"); 
       }, 
       error: function(resp, status, xhr){ 
        $("#message").html("ERROR: " + resp.status + " " + resp.statusText + "\n" + xhr); 
        $("#message").show(); 
       } 
      }); 
     } 

什麼我做錯了嗎?

感謝,

回答

1

您可以對其進行編碼(即BASE64)發送之前,它在服務器上進行解碼,也可以使用JSON作爲請求PARAM。

使用JSON,例如;

@POST 
@Path("/update") 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
public void update(PathContext ctx) { 
    updateURI(ctx.getPath(),ctx.getContent()); 
} 

@XmlRootElement 
public class PathContext { 
    private String path; 
    private String content; 

    public String getPath() { 
     return path; 
    } 

    public void setPath(String path) { 
     this.path = path; 
    } 

    public String getContent() { 
     return content; 
    } 

    public void setContent(String content) { 
     this.content = content; 
    } 

} 

您的JSON看起來像;

{"path": somePath, "content": someContent} 

希望它有幫助。

+0

現在試着創建一個像你所建議的XmlRootEelement,但編碼我嘗試過「var sRdf = encodeURIComponent(rdf)」,但它改變了引號中的一些標籤..等等。 – mzereba 2014-09-28 13:21:37

+0

如果您想使用編碼,請使用base64。 http://scotch.io/quick-tips/js/how-to-encode-and-decode-strings-with-base64-in-javascript – 2014-09-28 13:31:21

+0

編碼工作,但我得到了400錯誤的請求,並告訴我「服務器拒絕此請求是因爲請求實體的格式不是所請求方法的請求資源支持的格式。「 – mzereba 2014-09-29 08:30:51