title: Angular Spring Post json with array
body: <p>i try to send json object from angular frontend to spring mvc backend without success. This is my config and what i have tried :</p>
  
  <p>config :</p>
  
  <p>web.xml :</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;rest&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:/META-INF/jdu/contexts/rest-servlet.xml&lt;/param-value&gt;
       &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;rest&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/api/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>rest-servlet.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans           
                         http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"&gt;
          &lt;!-- prevent JSON Hijacking --&gt;
          &lt;property name="prefixJson" value="true"/&gt;
      &lt;/bean&gt;
     &lt;/beans&gt;
  </code></pre>
  
  <p>applicationContext:</p>
  
  <pre><code>&lt;!-- Configures the annotation-driven Spring MVC Controller programming model.
  Note that, with Spring 3.0, this tag works in Servlet MVC only!  --&gt; 
  &lt;mvc:annotation-driven&gt;
      &lt;mvc:message-converters&gt;
      &lt;!-- Use the HibernateAware mapper instead of the default --&gt;
      &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
          &lt;property name="objectMapper"&gt;
              &lt;bean class="com.mc.jdu.utils.HibernateAwareObjectMapper"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;/mvc:message-converters&gt;
  &lt;/mvc:annotation-driven&gt;
  </code></pre>
  
  <p>pom.xml with jackson-version = 2.6.3:</p>
  
  <pre><code>        &lt;!-- Data Mapper package is a high-performance data binding package built 
          on Jackson JSON processor --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
          &lt;version&gt;${jackson.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-datatype-hibernate4&lt;/artifactId&gt;
          &lt;version&gt;${jackson.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
          &lt;version&gt;${jackson.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;&lt;!-- jackson --&gt;
          &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
          &lt;version&gt;1.9.13&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>my controller :</p>
  
  <pre><code>@RequestMapping(value="/create", method=RequestMethod.POST)
  @ResponseBody
  public StatusResponse create(EventsDTO eventsDTO) throws TechnicalException {
      return service.create(eventsDTO);
  }
  </code></pre>
  
  <p>EventDTO :</p>
  
  <pre><code>public class EventsDTO implements BeanInterface{
  
  
  private String textColor;
  private String color;
  private List&lt;CallendarDTO&gt; events = new ArrayList&lt;CallendarDTO&gt;();
  
  
  public EventsDTO(){
  
  }
   // getters and setters
  </code></pre>
  
  <p>CallendarDTO :</p>
  
  <pre><code>public class CallendarDTO {
  
  
  private String title;
  private String libelle;
  private String date;
  private String start;
  private String end;
  private String duree;
  private boolean allDay;
  private boolean stick;
  </code></pre>
  
  <p>Angular :</p>
  
  <pre><code> var _queryPost = function(url, data, defData) {
  
      $http({
        headers:{'Content-Type':'application/json'},
        method: 'POST',
        url: url,
        params: data
      })
        .success( function(data, status, headers, config) {
          defData.$resolve(data);
          $log.info(data);
        })
        . error(function(data, status, headers, config) {
          $log.warn('*** DataProvider - query - error - status:' + status);
          401===status?$location.path('/signIn'):$location.path('/error' + status);
          defData.$reject(data);
        });
  
      return defData;
    };
  </code></pre>
  
  <p>And the data :</p>
  
  <pre><code>$scope.eventsDTO = [];
    $scope.eventsDTO.color = "green ";
    $scope.eventsDTO.texColor="";
    //$scope.eventsDTO.events=[];
    $scope.eventsDTO.events= [
      {
        "title":"1",
        "date":"2013-10-04",
        "libelle":"lib 1",
        "start":"08:30",
        "end":"10:30",
        "duree":"02:00",
        "allDay":false,
        "stick":true
      },
      {
        "title":"2",
        "date":"2013-10-04",
        "libelle":"lib 2",
        "start":"08:30",
        "end":"10:30",
        "duree":"02:00",
        "allDay":false,
        "stick":true
      }
    ];
  </code></pre>
  
  <p>OK. if i try like this i get : "the server responded with a status of 400 (Bad Request)"</p>
  
  <p>If the events list is empty, it works....but of course i need this list</p>
  
  <p>So i red this post : <a href="https://stackoverflow.com/questions/28838261/spring-mvc-post-request-and-json-object-with-array-bad-request">Spring MVC : post request and json object with array : bad request</a></p>
  
  <p>and i have tried to add @RequestBody on the controller like this :</p>
  
  <pre><code>@RequestMapping(value="/create", method=RequestMethod.POST)
  @ResponseBody
  public StatusResponse create(@RequestBody EventsDTO eventsDTO) throws TechnicalException {
      return service.create(eventsDTO);
  }
  </code></pre>
  
  <p>but now i get : "the server responded with a status of 415 (Unsupported Media Type)"</p>
  
  <p>In fact, the app works when i pass object without List inside. But if there is a List (like EventsDTO with its CallendarDTO List), it doesn't work.</p>
  
  <p>The error 415 by adding @RequestBody make me think that my Jackson config is wrong...but where ?
  I have tried to change apllicationContext.xml configuration with what i have found ont the web, for exemple, by adding : </p>
  
  <pre><code>&lt;bean id="jacksonMessageConverter"
  class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt;&lt;/bean&gt;
  &lt;bean
      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
      &lt;property name="messageConverters"&gt;
          &lt;list&gt;
              &lt;ref bean="jacksonMessageConverter" /&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt; 
  </code></pre>
  
  <p>But i don't understand exactly how does it work..</p>
  
  <p>How can i pass my EventsDTO ?</p>