Angular Spring Post json with array

i try to send json object from angular frontend to spring mvc backend without success. This is my config and what i have tried :

config :

web.xml :

<servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/META-INF/jdu/contexts/rest-servlet.xml</param-value>
     </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

rest-servlet.xml :

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
        <!-- prevent JSON Hijacking -->
        <property name="prefixJson" value="true"/>
    </bean>
   </beans>

applicationContext:

<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only!  --> 
<mvc:annotation-driven>
    <mvc:message-converters>
    <!-- Use the HibernateAware mapper instead of the default -->
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="objectMapper">
            <bean class="com.mc.jdu.utils.HibernateAwareObjectMapper"/>
        </property>
    </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

pom.xml with jackson-version = 2.6.3:

        <!-- Data Mapper package is a high-performance data binding package built 
        on Jackson JSON processor -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-hibernate4</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency><!-- jackson -->
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

my controller :

@RequestMapping(value="/create", method=RequestMethod.POST)
@ResponseBody
public StatusResponse create(EventsDTO eventsDTO) throws TechnicalException {
    return service.create(eventsDTO);
}

EventDTO :

public class EventsDTO implements BeanInterface{


private String textColor;
private String color;
private List<CallendarDTO> events = new ArrayList<CallendarDTO>();


public EventsDTO(){

}
 // getters and setters

CallendarDTO :

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;

Angular :

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

And the data :

$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
    }
  ];

OK. if i try like this i get : "the server responded with a status of 400 (Bad Request)"

If the events list is empty, it works....but of course i need this list

So i red this post : Spring MVC : post request and json object with array : bad request

and i have tried to add @RequestBody on the controller like this :

@RequestMapping(value="/create", method=RequestMethod.POST)
@ResponseBody
public StatusResponse create(@RequestBody EventsDTO eventsDTO) throws TechnicalException {
    return service.create(eventsDTO);
}

but now i get : "the server responded with a status of 415 (Unsupported Media Type)"

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.

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 :

<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter" />
        </list>
    </property>
</bean> 

But i don't understand exactly how does it work..

How can i pass my EventsDTO ?

链接地址: http://www.djcxy.com/p/1340.html

上一篇: 从POST Curl插入数据到JAVA Spring

下一篇: 角度弹簧发布json与数组