user9176898
user9176898

Reputation:

Pass simple array to Java Spring

I'm trying to post a value and a string[] to Spring MVC. I either get:

HTTP Status 400 - Required String[] parameter 'testCaseNames' is not present

If I turn it to a list:

HTTP Status 400 - Required List parameter 'testCaseNames' is not present

What type should I put then??

var flowName = $('#flowName').val();
var testCaseNames = [];
$('.icons-right .action-icon:first-of-type').each(function() { 
  testCaseNames.push($(this).attr('name')) 
});
console.log(testCaseNames);

$.ajax({
  type: 'post',
  url: '/create-flow/save',
  data: {
    flowName: flowName,
    testCaseNames: testCaseNames
  },
  success: (function (result) {
  })
});
@RequestMapping(value = "/create-flow/save" , method = RequestMethod.POST)
public @ResponseBody String saveFlow(HttpSession session, @RequestParam("flowName") String flowName, @RequestParam("testCaseNames") String[] testCaseNames) 
{
  String user = session.getAttribute("loggedUser").toString();
  return TestFlow.addFlow(flowName,testCaseNames,user);
}

Output in console:

["sdad", "xzxc"]

Upvotes: 0

Views: 342

Answers (2)

Nguyen Hung
Nguyen Hung

Reputation: 1

You can try as follow

@RequestMapping(value = "/create-flow/save" , method = RequestMethod.POST)
public @ResponseBody String saveFlow(HttpSession session, @RequestParam("flowName") String flowName, @RequestParam("testCaseNames[]") String[] testCaseNameValues) 
{
  String user = session.getAttribute("loggedUser").toString();
  return TestFlow.addFlow(flowName, testCaseNameValues,user);
}

And also add "[]" to Javascript field name

$.ajax({
      type: 'post',
      url: '/create-flow/save',
      data: {
        'flowName': flowName,
        'testCaseNames[]': testCaseNames
      },
      success: (function (result) {
      })
});

Upvotes: 0

pvpkiran
pvpkiran

Reputation: 27018

You are posting the data as body of your REST request.

$.ajax({
  type: 'post',
  url: '/create-flow/save',
  data: {
    flowName: flowName,
    testCaseNames: testCaseNames
  },
  success: (function (result) {
  })
});

But in your controller you are recieveing as RequestParams.

@RequestParam("testCaseNames") String[] testCaseNames

That is the problem.

RequestParams are part of your url. For example if your url is like

http://blahbla:1234?abcd=1234&defg=5677   

then abcd and defg are request parameters.(This is one way to send data)
Another way is to set the data in the Message Body which you don't see in the url, like how you are doing now.

You have two options to fix this
1. Change your ajax request to remove the data part and include testCaseNames and flowName in url like this

$.ajax({
  type: 'post',
  url: '/create-flow/save?flowName='+flowName+'&testCaseNames='+testCaseNames,
  success: (function (result) {
  })
});

2. Change your controller by removing the requestParams and creating a class with testCaseNames and flowName as fields and accept that as argument.

@RequestMapping(value = "/create-flow/save" , method = RequestMethod.POST)
public @ResponseBody String saveFlow(HttpSession session, @RequestBody SomeClass someclass) 
{
  .....
}



class SomeClass {
    String flowName;
    String testCaseNames; // Field Names should match exactly with what you send from frontend
    // Gettter & Setters

   }

Upvotes: 1

Related Questions