我有以下服务器端Java代码,它完全适用于我的chrome-rest-client:
@POST
@Consumes(MediaType.APPLICATION_JSON)
public long postVehicle(Vehicle vehicle) {
vehicleDao.persist(vehicle);
return vehicle.getId();
}
我可以通过我的rest-client发送一个JSON来测试这个接口,它得到了很好的处理。
但是现在我想通过带有jQuery-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 ::
这是raw-request-header
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"}'