Parsing json into java objects with other object attribue in spring-mvc
I have an object of type element and this has an attribute of type theme. when I create a new element is represented by a theme select in the view, and the primary key is the value of the items in the select, when I send with json, spring try to create an object element and the next error开发者_开发问答 is show
/element: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.example.Theme, problem: no suitable creator method found at [Source: org.mortbay.jetty.HttpParser$Input@dd0099; line: 1, column: 31]
this is my code in spring MVC
public class Element {
private String name;
private String type;
private Theme theme;
private String description;
// Get - Set
}
Theme Class
public class Theme {
private String name;
private String description;
// Get - Set
}
Method in the controller
@RequestMapping(method=RequestMethod.POST)
public @ResponseBody String create(@RequestBody Element element){
elementManager.saveElement(element);
return "exito";
}
and the javascript is this
$("#element").submit(function() {
var element = $(this).serializeObject();
$.postJSON("element", element, function(data) {
});
return false;
});
i hope someone can help me.
Based on your comment containing the JSON you send the server, I would say the issue is the JSON itself.
Your server is expecting a value along the lines of this:
{"name":"rooms","type":"Doc","theme":{"name":"themeName", "description":"themeDescription"},"description":"They are realy big"}
You should fetch the object theme from where it is stored, and create the proper JSON. If the Theme
is looked up at the client, you will need to change the Element to have a String
theme attribute, and do the lookup on the client.
精彩评论