通过ajax调用提交json数据不在spring控制器中工作

时间:2015-07-27 11:18:52

标签: ajax json angularjs spring model-view-controller

我有一个angularjs页面,我试图通过ajax调用将输入作为JSON对象传递给spring控制器并尝试将其分配给用户定义的类来保存它。但是所有值都以lobj.Add(new SelectListItem { Value = dr["city"].ToString(), Text = dr["city"].ToString() }); 的形式出现在用户对象中。以下是我的代码。

控制器方法代码(POST请求):

~/Library/Developer/Xcode/DerivedData

Ajax电话:

null

我已使用@RequestMapping(method = RequestMethod.POST, value ={"/addEvent"}) @ResponseBody public void addEvent(@RequestBody final EventsMstr eventsMstr) { System.out.println("@@@@@@@@@@@@@@@@@@@@@ addEvent controller started."); System.out.println("eventsMstr = " + eventsMstr);//Prints null for all the fields this.eventsMstrService.addEvent(new EventsMstr()); } 注释了班级的所有字段。我不确定我是否遗漏了一些东西。建议非常感谢。

Spring servlet配置:

SaveEvent: function (param) {
                 var successCallback = null;
                 var errorCallback = null;
                 alert("Param "+param.EventTypeId + param.StartDate+param.EndDate+param.Description);//values getting printed
                 $http({
                     url: config.InsertEvent,
                     type: "POST",
                     data: JSON.stringify(param),
                     headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }
                 })
                .success(function (data, httpStatus, headers) {
                    successCallback(data, httpStatus, headers);
                })
                .error(function (httpStatus, headers) {
                    successCallback(data, httpStatus, headers);
                });
                 return {
                     success: function (callback) {
                         successCallback = callback;
                         return {
                             error: function (callback) {
                                 errorCallback = callback;
                             }
                         }
                     }
                 }
             },

JSON数据:

@JsonProperty

Bean类:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonConverter" />
            </list>
        </property>
    </bean>
<bean id="jsonConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
        <list>
         <value>application/json</value>
         <value>application/x-www-form-urlencoded; charset=UTF-8</value>
        </list>
        </property>
    </bean>

1 个答案:

答案 0 :(得分:0)

这是我粗心大意吃了两天。我做错了是,我没有注释属性,而是注释了getter方法。下面是修改后的对象类。

package com.ems.business.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

//import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;


@XmlRootElement(name = "EventsMaster")
//@JsonIgnoreProperties(ignoreUnknown = true)
public class EventsMstr implements java.io.Serializable  {

    private static final long serialVersionUID = 1L;

    @JsonProperty("ID")
    private Long id;
    @JsonProperty("EventTypeId")
    private Long eventTypeId;
    @JsonProperty("StartDate")
    private Date startDate;
    @JsonProperty("EndDate")
    private Date endDate;
    @JsonProperty("Description")
    private String description;

    public EventsMstr() {
    }
        public EventsMstr(Long id, Long eventTypeId, Date startDate, Date endDate,
            String description) {

        this.id = id;
        this.eventTypeId = eventTypeId;
        this.startDate = startDate;
        this.endDate = endDate;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getEventTypeId() {
        return eventTypeId;
    }

    public void setEventTypeId(Long eventTypeId) {
        this.eventTypeId = eventTypeId;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "EventsMstr [id=" + id + ", startDate=" + startDate
                + ", endDate=" + endDate + ", description=" + description
                + "]";
    }
}

请求映射也应如下所示:

@RequestMapping(method = RequestMethod.POST, value ={"/addEvent"}, 
            headers = {"content-type=application/x-www-form-urlencoded; charset=UTF-8"})
    public @ResponseBody EventsMstr addEvent(@RequestBody EventsMstr eventsMstr) {
        this.eventsMstrService.addEvent(eventsMstr);
        return eventsMstr;
    }