0

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"]

2 Answers 2

1

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

   }
Sign up to request clarification or add additional context in comments.

4 Comments

I'm new on this. Can you explain when should I use other or another? I usually do like this in all my pages and it was working until now. How should I do it then?
I have like 50 ajax posts and I always did like this. I dont think that's the problem.
DVs aren't mine, but you haven't actually provided a solution here.
Sorry I was in the process of editing the answer. Updated the answer
0

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

Comments

Your Answer

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