角度弹簧发布json与数组

我尝试从角度前端发送json对象到spring mvc后端,但没有成功。 这是我的配置,我曾尝试过:

配置:

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>

使用jackson-version = 2.6.3的pom.xml:

        <!-- 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>

我的控制器:

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

数据:

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

好。 如果我尝试这样做,我会得到:“服务器响应状态为400(错误请求)”

如果事件列表是空的,它的作品....但我当然需要这个列表

所以我红色这个帖子:春天MVC:发布请求和json对象与数组:请求不好

我试图在控制器上添加@RequestBody像这样:

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

但是现在我得到了:“服务器的响应状态为415(不支持的介质类型)”

事实上,应用程序工作时,我传递没有列表内的对象。 但是如果有一个列表(如带有CallendarDTO列表的EventsDTO),它将不起作用。

通过添加@RequestBody错误415使我认为我的杰克逊配置是错误的......但是在哪里? 我试图改变apllicationContext.xml配置与我发现在网上,例如,通过添加:

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

但我不明白它是如何工作的。

我怎样才能通过我的EventsDTO?

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

上一篇: Angular Spring Post json with array

下一篇: How to post variables with curl in JSON