Kasun Kariyawasam
Kasun Kariyawasam

Reputation: 2447

How do I send a object using JavaScript to Spring Controller using json

I am making simple Spring MVC web application. In my application I want to send a object from front end to Spring controller method. But when I do this I am getting js error

500 (Internal Server Error)

I tried to find a good answer in internet but still could not find a good one.

My controller method is

        @RequestMapping(value = "/add", method = RequestMethod.GET, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody String addInventory(@RequestBody InventoryAddParam inventoryAddParam) {

    log.info("addInventory");
    ServiceRequest<InventoryAddParam> serviceRequest =  convertAddParamtoServiceRequest(inventoryAddParam);
    validate(inventoryAddParam);
    inventoryService.addInventory(serviceRequest);
    return "Inventory Added Succesfully";
}                             

inventoryAddParam is a Serializable object that contain only String parameters.

My JavaScript function to send object is

function sendDataTest() {
$.ajax({
    url : "/GradleSpringMVC/inventory/add",
    type : 'GET',
    dataType : 'json',
    data : JSON.stringify(populateTestObject()),
    contentType : 'application/json',
    mimeType : 'application/json'
}).done(function(data) {
    // temGrid.addJSONData(data);
}).fail(function(error) {
    // parseToPageAlerts(error.responseText);
}).always(function() {
    // hideLoading()
});}

I am creating the addParam object to send as ajax call. It create in function

function populateTestObject(){
var addParam = new InventoryAddParam();
addParam.inventoryId = "INV001";
addParam.name = "ECG MACHINE";
addParam.price = "1000";
addParam.hospital = "Colombo";
addParam.userNote = "User Note";
return addParam;}      

Do I have done any wrong thing here ?

Other details about the error

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/GradleSpringMVC/inventory/add
Request Method:POST
Status Code:500 Internal Server Error

Header

Connection:close
Content-Language:en
Content-Length:4939
Content-Type:text/html;charset=utf-8
Date:Wed, 21 Jan 2015 03:55:19 GMT
Server:Apache-Coyote/1.1

Upvotes: 1

Views: 3407

Answers (2)

Kasun Kariyawasam
Kasun Kariyawasam

Reputation: 2447

Found the answer. Just changed the version of jackson to 1.9.12

'org.codehaus.jackson:jackson-mapper-asl:1.9.12'

Upvotes: 1

Master Slave
Master Slave

Reputation: 28569

You spring method consumes application/json and the error message says that what you're sending is not a valid JSON.

Following that line, I believe that your issue is a $.extend(addParam), when set with a single argument it will extend a jQuery namespace with the object, and won't return any JSON object to be stringified (and nothing being sent to the server). The possible solution is either removing the extend function, or adding another parameter in which case a valid JSON object will be returned, e.g.

function sendData() {
var addParam = createAddParam();
$.ajax({
    url : "/GradleSpringMVC/inventory/add",
    type : 'GET',
    dataType : 'json',
    data : JSON.stringify(addParam),
    contentType : 'application/json',
    mimeType : 'application/json'
}).done(function(data) {
    // temGrid.addJSONData(data);
}).fail(function(error) {
    // parseToPageAlerts(error.responseText);
}).always(function() {
    // hideLoading()
});} 

Upvotes: 2

Related Questions