java - 在 REST 资源中接收空值

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

我做错了什么吗?

谢谢

最佳答案

您可以在发送之前对其进行编码(即 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}

希望有帮助。

关于java - 在 REST 资源中接收空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26084866/

相关文章:

java - 在网站页面上实现 Android/iPhone 应用程序

java - 如何在Jfreechart饼图中使用Arraylist(for循环)?

java.io.FileNotFoundException :/home/ec2-user/src/main/resources/keystore. jks(没有这样的文件或目录)

http - Rest API - 好友集合创建

java - Jersey 2.27 无法处理 POST

java - Android - 无法使用 FileProvider 添加电子邮件附件

apache-flex - 如何在Flex中解码和转换JSON字符串?

Java 从 URL 读取 JSON

python - 如何在 python 中通过 jsonschema 过滤 json

node.js - REST API - 如何实现用户特定授权?