在REST资源中接收空值

时间:2014-09-28 12:31:41

标签: java json rest post jersey

我通过带有ajax的POST发送这两个参数时继续接收空值(也尝试使用Poster):

@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发送。

使用jQuery代码是不相关的,因为它既没有使用Poster也没有使用,但是这里是:

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 个答案:

答案 0 :(得分:1)

您可以在发送之前对其进行编码(即base64)并在服务器上对其进行解码,或者您可以将JSON用作请求参数。

例如,使用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}

希望它有所帮助。