繁体   English   中英

400错误的请求将JSON POST发送到REST

[英]400 Bad Request sending JSON POST to REST

我正在使用一些数据从JQuery发送POST,但是通过我的Javascript我得到了:

400错误的请求错误,REST无法触发

从海报尝试一下,我得到以下信息:

HTTP状态415-不支持的媒体类型。服务器拒绝了此请求,因为请求实体的格式不受请求的资源所支持。

这是我的JQuery:

function doUpdate(path, rdf)
        {
            var encodedRdf = base64_encode(rdf);
            var data = {"path": path, "rdf": encodedRdf};
            var sUrl = "http://localhost:8080/browsing/services/RDF/update";
            $.ajax({
                type: "POST",
                url: sUrl,
                contentType: "application/json",
                data: data,
                dataType: "json",
                async: false,
                success: function parse(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();
                }
            });
        }

这是我的REST资源:

@POST
@XmlElement(name = "data")
@Path("/update")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public void update(Data data) {
    ... 
}

这是我的数据对象:

@XmlRootElement 
public class Data implements Serializable {

     private String path;
     private String rdf;

      /**
     * @return the path
     */
    public String getPath() {
        return path;
    }
    /**
     * @param path the path to set
     */
    public void setPath(String path) {
        this.path = path;
    }
    /**
     * @return the rdf
     */
    public String getRdf() {
        return rdf;
    }
    /**
     * @param rdf the rdf to set
     */
    public void setRdf(String rdf) {
        this.rdf = rdf;
    }

有人可以告诉我问题出在哪里吗?

谢谢,

web.xml中是否有以下init参数?

 <init-param>
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
 </init-param>

如下组成JSON并将其字符串化解决了!

var data = {"path": path, "rdf": encodedRdf};
...
data: JSON.stringify(data),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM