title: Spring MVC + DataTables: bind array[] parameter
body: <h2>Short problem description</h2>
  
  <p>I can not convert url params like <code>...?name=John&amp;sports[]=Volleyball&amp;sports[]=Volleyball</code> to the following Java class in Spring MVC.</p>
  
  <pre><code>class PersonFilter {       
      String name;
      List&lt;String&gt; sports; 
  }
  </code></pre>
  
  <p>I need to use brackets in the sports[] parameter name.</p>
  
  <h2>Long problem description</h2>
  
  <p>I use DataTables framework with Spring MVC.
  I need to add some complex filter properties to datatables ajax request as follows:</p>
  
  <pre><code>$('#table').DataTable({
      serverSide: true,
      ajax: {
          url: "https://server/some/path",
          data: function (data) {
              data.name = "John",
              data.sports = ["Football", "Volleyball"]
          }
      }
  });
  </code></pre>
  
  <p>Created DataTables request with sorting (parameters <code>order</code> and <code>columns</code>) and custom filter (parameters <code>name</code> and <code>sports[]</code>) looks like:</p>
  
  <pre><code>https://server/some/path?name=John&amp;sports[]=Volleyball&amp;sports[]=Football&amp;columns[0][name]=name&amp;order[0][column]=0&amp;order[0][dir]=asc
  </code></pre>
  
  <p>I need to bind URL parameters to the PersonFilter class containing <code>java.util.List</code>.</p>
  
  <pre><code>class PersonFilter {       
      String name;
      List&lt;String&gt; sports; 
  }
  </code></pre>
  
  <p>Controller looks like:</p>
  
  <pre><code>@Controller
  class PersonController {
  
      @RequestMapping(path = "/search")
      public List&lt;Person&gt; search(PersonFilter personFilter) {
         ...
      }
  }
  </code></pre>
  
  <p>Spring allows following syntax for List binding (<a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html#beans-beans-conventions" rel="nofollow noreferrer">http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html#beans-beans-conventions</a>):</p>
  
  <pre><code>1) sports=Volleyball&amp;sports=Football -&gt; OK
  2) sports[0]=Volleyball&amp;sports[1]=Football -&gt; OK
  </code></pre>
  
  <p>But Spring can not convert property of name <code>sports[]</code> to List. </p>
  
  <pre><code>sports[]=Volleyball&amp;sports[]=Football -&gt; FAILs (expects index in brackets)
  
  java.lang.NumberFormatException: For input string: ""
      at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_121]
      at java.lang.Integer.parseInt(Integer.java:592) ~[na:1.8.0_121]
      at java.lang.Integer.parseInt(Integer.java:615) ~[na:1.8.0_121]
      at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:354) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:859) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.validation.DataBinder.doBind(DataBinder.java:755) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:192) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:106) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:150) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:114) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:160) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129) ~[spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.6.jar:8.5.6]
  </code></pre>
  
  <h2>What I've tried:</h2>
  
  <p>Use JQuery ajax setting <a href="http://api.jquery.com/jQuery.param/" rel="nofollow noreferrer"><code>jQuery.ajaxSettings.traditional = true</code></a>.
  But in this case DataTables parameters for sorting (order, columns) results in <code>[Object]</code>, so I am not able to parse sort parameters.</p>
  
  <pre><code>https://server/some/path?name=John&amp;sports=Volleyball&amp;sports=Football&amp;columns=[object Object]&amp;order=[object Object]
  </code></pre>
  
  <p>Thank you for any suggestion!</p>