【发布时间】:2015-10-17 13:21:59
【问题描述】:
我有一个 angularjs 页面,我试图通过 ajax 调用将输入作为 JSON 对象传递给 spring 控制器,并尝试将其分配给用户定义的类以保存它。但是所有的值都在用户对象中以null 的形式出现。下面是我的代码。
控制器方法代码(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 调用:
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;
}
}
}
}
},
我已经用@JsonProperty 注释了我班级的所有字段。我不确定我是否在这里遗漏了什么。非常感谢您的建议。
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 数据:
var EventItem = {
EventTypeId: $scope.eventTypeId,
StartDate: $scope.startDate,
EndDate: $scope.EndDate,
Description: $scope.EventName
};
豆类:
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
+ "]";
}
}
【问题讨论】:
-
请发布您的 bean 类代码
-
确保you're not using an old version ObjectMapper。请发布您的
pom.xml -
为什么将标头设置为“application/x-www-form-urlencoded”?应该是 application/json 吗?
-
@Anand 以前我使用
@JsonIgnoreProperties和@JsonProperty从org.codehaus.jackson.mapper.1.4.2,现在我将其更改为jackson-annotations-2.2.1。但我现在得到 MappingException 。如您所见,我已正确映射所有属性。以下是例外:org.codehaus.jackson.map.JsonMappingException: Unrecognized field "EventTypeId" (Class com.ems.business.model.EventsMstr), not marked as ignorable at [Source: org.apache.catalina.connector.CoyoteInputStream@1a33ad7; line: 1, column: 2] -
@anand 我没有使用 Maven
标签: ajax json angularjs spring model-view-controller