0

i have the following server side Java Code which works perfectly for my chrome-rest-client:

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

i can send an JSON via my rest-client to test this interface and it gets processed very well.

But now i want to send the following json via an web-page with a jQuery-ajax request to my server:

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);

the request-headers which i see in my debugger looks good to me.

But i get the following two errors from the server:

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

EDIT 1::

I tried it with an jQuery-Rest-Client and it also didn't work

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

same errors


EDIT 2::

Here is the 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

And the payload:

'{"color": "black","emissionStandards": "EURO 5","emptyWeight": "1500","mileage": "60000","topSpeed": "260"}'
Laokoon
  • 1,241
  • 4
  • 24
  • 47
  • I am using RestEasy and this would work. When I has tried Jackson, there was a wrapper around my objects, like `{"Vehicle": {"color":"...", ...}}`. Could you make a REST method that returns a `Vehicle` and include the response in your question? With `$.get(...)` or `curl` from command line... – Nikos Paraskevopoulos Sep 12 '13 at 10:17
  • Well the result from the server after a GET is exactly the same json ;-) I don't want any wrapper object because that's not needed. It works with another rest-client. The only problem is the javascript part which may sends the json in the wrong format or something like this. – Laokoon Sep 12 '13 at 10:22
  • This exception seems to mean that Jackson encountered a JSON array start, when it was expecting a `Vehicle` object. Can you doulbe-check your actual posted data? Have you tried calling this endpoint from command line? (`curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"color": "black", ...}' http://yourserver/resources/vehicle`) – Nikos Paraskevopoulos Sep 12 '13 at 10:33
  • Yes i checked the raw-request. i will post it in my initials post. – Laokoon Sep 12 '13 at 12:03

0 Answers0