0

Heyy, I want to take a list of data in my request param,here "personIdCollection" is a set of list but when i am hitting through postman i am getting a bad request. Here is my code.

controller

@PostMapping("/face-tag-data")
public String getFaceTaggedData(@RequestParam String projectId,@RequestParam List<String> personIdCollection) {
    return null;
}

and here is my ajax

var data = {};
            data.personIdCollection = personIdCollection;
            data.projectId = $("#projectId").val();
            $.ajax({
                type:'POST',
                url:contextPath+'/face-tag-data',
                data:data,
                success:function(resp){
                    console.log(resp);
                },
                failure:function(resp){
                    console.log(resp);
                }
            });
Mr Freak
  • 216
  • 3
  • 20
  • This may help you : https://stackoverflow.com/questions/4596351/binding-a-list-in-requestparam – user Oct 22 '19 at 11:37
  • already tested but not working – Mr Freak Oct 22 '19 at 11:40
  • 2
    It would be better to use the `@RequestBody` to pass large collection data, there is limitation on max url length. – Eugen Oct 22 '19 at 11:40
  • " Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986" getting this message – Mr Freak Oct 22 '19 at 11:42
  • If you are using tomcat, then an upper version of Tomcat 8.5 throws this exception if the URL path contains '[' and ']'. Please check if your data has brackets in it. – user Oct 22 '19 at 11:46

1 Answers1

0

This is working for me. I do not use an ajax-request but instead submit my form directly but it should work either way.

My controller looks like:

@RequestMapping(value="addSingleArticles", method=RequestMethod.POST)
public @ResponseBody String addSingleArticles(
        ArticleSubmitData pupilPackageData,
        HttpServletRequest request) {

    ... // do something with the data

}

As you can see I have defined my own composite type which consists of three lists. So you obviously can use it with only one list directly.

public class ArticleSubmitData {

  private List<Article> singleArticles;
  private List<Article> packageArticle;
  private List<Article> popupArticles;

  ... // getter & setter, inner classes etc.

}

In my server page oder faclet I use the following html-code to make this work

...
    <input id="" class="" type="text" name="singleArticles[${line.index}].engraving" />
...

So the trick is to define the variables on your webpage as an array and use this in your controller as a list. As you can see in my example I also use an inner class in my composite class which has extra attributes.

M46
  • 923
  • 9
  • 20