Angular Spring Post json with array

时间:2015-11-18 16:50:47

标签: json angularjs spring spring-mvc

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

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>

带有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(错误请求)”

如果事件列表为空,则有效....但我当然需要此列表

所以我发布这篇文章:Spring MVC : post request and json object with array : bad request

我尝试在控制器上添加@RequestBody,如下所示:

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

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

事实上,当我在没有List的情况下传递对象时,app会起作用。但是如果有一个List(就像EventsDTO及其CallendarDTO List那样),它就不起作用了。

添加@RequestBody的错误415让我觉得我的Jackson配置错了......但是在哪里? 我试图改变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?

0 个答案:

没有答案