Saturday, 31 August 2013

Issue with @RequestBody annotation with Kendo UI Grid: Invalid Data Type

Issue with @RequestBody annotation with Kendo UI Grid: Invalid Data Type

I 've struggling with a little project in which I use kendo UI Grid ,
Spring 3.2 and jackson com.fasterxml.jackson.core version 2.2.2:(databind,
annotation and the core). and jquery 1.20.2 and spring-data-mongo. below
is a snippet of my mvc application context:
<bean id="messageConverters"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<!-- Message converters -->
<bean
class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean
class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter"
/>
<bean
class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean
class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
/>
</list>
</property>
</bean>
I have a PostDTO which represent a post sent throug a Restful API. and
Symptoms a pojo with @Document annotation which is supposed to be
managed(CRUD) by kendo Grid
public class PostDTO {
private String longitude;
private String latitude;
private String postType;
//,,,,
}
@Document
public class Symptom {
@Id
private String ID;
private String code;
private String name;
private String description;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date dateCreated;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date lastModified;
}
below are the Controllers actions to process each of them
@RequestMapping(value = "/api/post",method = RequestMethod.POST)
@ResponseBody
public ApiResponse receivePost(@RequestParam("username") String
username,@RequestParam("password") String password, @RequestBody PostDTO
postDTO , HttpServletRequest request, HttpServletResponse response){
ApiResponse apiResponse = new ApiResponse();
if(username ==null){
response.setStatus(500);
apiResponse.setStatus(500);
apiResponse.setMessage("empty username");
return apiResponse;
}
//other validations and some other long code
return apiResponse;
}
@RequestMapping(value = "/admin/do/symptoms/new",method =
RequestMethod.POST)
public @ResponseBody GridResponse createOne(@RequestBody Symptom symptom){
if(symptom !=null ){
//other processessing
}
//other codes
GridResponse gridResponse = new GridResponse();
return gridResponse;
}
a jquery ajax post to /api/post works well. google chrome extension
postman also works with raw json data in the request header. the PostDTO
object is created without any issue. But it doesn't seem to be the same
story with the Symptom object. I passed the raw Symptom json object using
the same postman with out success: below is what Kendo Ui Grid sends to
back end from what I see on Firebug:
type : application/x-www-form-urlencoded
ID
code something
dateCreated Sat Aug 31 2013 18:18:36 GMT+0000 (GMT)
description something
lastModified Sat Aug 31 2013 18:18:36 GMT+0000 (GMT)
name something
Source:
ID=&code=something&name=something&description=something&dateCreated=Sat+Aug+31+2013+18%3A18%3A36+GMT%2B0000+(GMT)&lastModified=Sat+Aug+31+2013+18%3A18%3A36+GMT%2B0000+(GMT)
Below is the actual kendo grid code :
<script type="text/javascript">
$(document).ready(function() {
var crudServiceBaseUrl = "<spring:url value="/admin/do"/>";
$("#grid").kendoGrid({
dataSource: {
type: "json",
serverPaging: true,
serverSorting: true,
pageSize: 20,
transport: {
read: crudServiceBaseUrl + "/symptoms/list",
create:{
dataType: "json",
url: crudServiceBaseUrl + "/symptoms/new",
type: "POST"
},
update: {
dataType: "json",
url: crudServiceBaseUrl + "/symptoms/update"
},
destroy: {
dataType: "json",
url: crudServiceBaseUrl + "/symptoms/delete"
}
},
schema:{
data: "rows",
total: "total",
model: {
id: "ID",
fields: {
ID: { editable: false, nullable: true },
code:{type: "string"},
name: { validation: { required: true} },
description: { type: "string" },
dateCreated: {type: "date"},
lastModified: {type: "date"}
}
}
}
},
height: 430,
sortable: true,
pageable: true,
columns: [
{field: 'code', title: "Code"},
{field: 'name',title:"Name"},
{field: 'description',title: "Description", width:150},
{field: 'dateCreated',title:"Date Created ",width: 50,
format: "{0:dd/MM/yyyy}"},
{field: 'lastModified',title:"Last Modified",format:
"{0:dd/MM/yyyy}"},
{ command: ["edit", "destroy"], title: "&nbsp;", width:
"160px" }
],
editable: "popup",
toolbar: ["create"]
});
});
</script>
Am a bit confused why it won't be so. The only difference I see here is
spring-data specific annotations. Is it possible that I left out an
important configuration or Something am doing wrong?

No comments:

Post a Comment