0

我有以下服务器端 Java 代码,它非常适合我的 chrome-rest-client:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public long postVehicle(Vehicle vehicle) {
    vehicleDao.persist(vehicle);
    return vehicle.getId();
}

我可以通过我的 rest-client 发送一个 JSON 来测试这个接口,它得到了很好的处理。

但现在我想通过带有 jQ​​uery-ajax 请求的网页将以下 json 发送到我的服务器:

var data = {
  "color": "black",
  "emissionStandards": "EURO 5",
  "emptyWeight": "1500",
  "mileage": "60000",
  "topSpeed": "260"
}

var options = {
    url: "resources/vehicle",
    data: data,
    contentType: "application/json",
    dataType: "json",
    processData: false,
    type : "POST",
    success: callback,
    async: true
};
$.ajax(options);

我在调试器中看到的请求标头对我来说看起来不错。

但是我从服务器收到以下两个错误:

org.glassfish.jersey.server.ContainerException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of de.ibs.trail.entity.Vehicle out of START_ARRAY token
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of de.ibs.trail.entity.Vehicle out of START_ARRAY token

编辑 1::

我用 jQuery-Rest-Client 尝试过它也没有用

md.rest = new $.RestClient('/trail/resources/', {ajax: {processData: false, DataType: 'json', contentType: 'application/json'}});
md.rest.add("vehicle");

var data = {/* same data as above */}; 
md.rest.vehicle.create(data); //same data as my first try

相同的错误


编辑 2::

这是原始请求标头

POST /resources/vehicle/ HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 329
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Content-Type: application/json
DNT: 1
Referer: http://localhost:8080/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: JSESSIONID=f5a663a3743657defb5c13aef866; treeForm_tree-hi=treeForm:tree:applicationServer

和有效载荷:

'{"color": "black","emissionStandards": "EURO 5","emptyWeight": "1500","mileage": "60000","topSpeed": "260"}'
4

0 回答 0