1

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
1
  • Updated with changes and new exception. Commented Jan 20, 2015 at 11:54

2 Answers 2

2

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()
});} 
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.