[英]submitting json data through ajax call not working in spring controller
I have an angularjs page, from which I am trying to pass inputs as JSON object to spring controller through ajax call and trying to assign it to a user defined class to save it. 我有一个angularjs页面,我试图从该页面通过ajax调用将输入作为JSON对象传递给spring控制器,并尝试将其分配给用户定义的类以进行保存。 But all the values comes as
null
in the user object. 但是所有值在用户对象中都为
null
。 Below is my code. 下面是我的代码。
Controller method code(POST request): 控制器方法代码(POST请求):
@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());
}
Ajax call: Ajax呼叫:
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;
}
}
}
}
},
I have annotated all the fields of my class with @JsonProperty
. 我已经用
@JsonProperty
注释了我班上的所有领域。 I am not sure whether I am missing something here. 我不确定我是否在这里缺少任何东西。 Suggestions greatly appreciated.
建议非常感谢。
Spring servlet configuration: Spring servlet配置:
<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>
JSON data: JSON数据:
var EventItem = {
EventTypeId: $scope.eventTypeId,
StartDate: $scope.startDate,
EndDate: $scope.EndDate,
Description: $scope.EventName
};
Bean class: Bean类:
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;
private Long id;
private Long eventTypeId;
private Date startDate;
private Date endDate;
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;
}
@JsonProperty("ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonProperty("EventTypeId")
public Long getEventTypeId() {
return eventTypeId;
}
public void setEventTypeId(Long eventTypeId) {
this.eventTypeId = eventTypeId;
}
@JsonProperty("StartDate")
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
@JsonProperty("EndDate")
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
@JsonProperty("Description")
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
+ "]";
}
}
It was my carelessness which ate my two days. 正是我的粗心使我吃了两天。 What I did wrong is, instead of annotating the properties, I was annotating the getter methods.
我做错的是,我没有注释属性,而是注释了getter方法。 Below is the modified object class.
下面是修改后的对象类。
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
+ "]";
}
}
Also the request mapping should be like below: 请求映射也应如下所示:
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.