I got an error ...(no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) which indicates that I need a property-based creator. I have a few constructors with different parameters, but no default.
My solution was to add a default constructor SomeClass() {}. My question is: why does this happen? Also, what is a delegate/property-based constructor?
If there is no constructors that is annotated with #JsonCreator, by default, Jackson needs a default no-args constructor in order to parse the JSON into POJO or bean classes. That is why when you add a default constructor, it will work fine.
And if you don't actually need the usage of the default constructor, just add it for Jackson only, you can set it to private, protected or package-protected. Jackson is still able to fill all the fields via reflection.
Regarding the no delegate- or property-based Creator, they are constructors that is annotated with #JsonCreator. In Jackson, there are 2 types of Creator/JsonCreator which are delegate-based Creator and property-based Creator.
Delegate-based creators take just one argument, which is NOT annotated with #JsonProperty. Type of that property is used by Jackson to bind the whole JSON value (JSON Object, array or scalar value), to be passed as value of that one argument.
Property-based creators take one or more arguments; all of which MUST be annotated with #JsonProperty, to specify JSON name used for property. They can only be used to bind data from JSON Objects; and each parameter represents one property of the JSON Object; type of property being used for binding data to be passed as that parameter when calling creator.
You can read for more details about these 2 creators in the article below.
http://www.cowtowncoder.com/blog/archives/2011/07/entry_457.html
Related
I'm attempting to use Jackson annotations with Groovy / Grails / GORM.
Annotations applied to a Groovy property are applied to its field, not its getter.
GORM lazy loads relationship properties by default. Lazy properties have the appropriate default value (null, 0, false, etc.) until the object is loaded. Field access doesn't cause GORM to load the object; only getter access causes an unloaded object to be loaded.
Thus, for Jackson to support lazy loaded GORM object, Jackson annotations must be applied to property getters instead of fields.
I do not want to have to set lazy: false for all my relationships, nor do I want to have to write an explicit getter for each property so that I can annotate the getter instead of the property.
Is there:
a way in Groovy / Grails / GORM to associate a property annotation with its getter instead of its field
a way in Jackson to setup access through the corresponding getter and/or setter when only annotating the field
an annotation processor to move annotations from a field to its corresponding getter and/or setter (I started to write one, but noticed many potential problems with this that might take a while to solve, so I'd prefer some solution other than one I have to write myself)
some other solution
I'm working in windows phone 8 application with MVVM pattern. I need to preserve page state when my app going to suspend. So I'm using State dictionary to store my viewmodel its working fine, But when I try to store viewmodel with parameter wise constructor following error will be occurred.
Type 'ViewModel' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.
If you pass an object to the State dictionary it will be serialized with the [Xml]DataContractSerializer and that requires a public, parameterless constructor.
If you're not able, or unwilling, to add such a constructor then you'll need to handle the serialization and deserialization of the object yourself and add the serialized data (the string) to the dictionary instead.
Can anybody explain how the MappingMongoConverter (Spring's default implementation of the MongoConverter interface) works for the cases where the mapping between POJO and Document isn't so trivial? Example cases: a POJO has an additional field it can't find in the Document, the Document has a structure that doesn't fit to the POJO,...
The official Spring documentation seems to lack this information.
Example code:
while (cursor.hasNext()) {
DBObject obj = cursor.next();
Foo foo = mongoTemplate.getConverter().read(Foo.class, obj);
returnList.add(foo);
}
The documentation is lacking, so had to dive into the source. I'll share my work.The tricky part is the POJO to BSON conversion:
The first thing it does is look for a #PersistenceConstructor annotation on a constructor. If no preferred constructor is set, the no arg constructor is used. The no mapping of the no arg constructor is simple enough. For the mapping of the preferred constructor, all parameters have to be present in the BSON. If a parameter can not be found, a MappingException will be thrown. This means that the BSON file can contain extra fields that don't have to map to a constructor parameter. Those parameters will just be ignored.
Is it possible to give Spring MVC's form binding some kind of type hint to indicate what class to instantiate and populate with form data?
I've got some quite unusual requirements to try and build a dynamic form, that represents a collection of different objects. The user can add objects from a selection of types to this collection, and then set the properties for that object type using form elements. I can figure out how to create the form using jQuery, but I'm not sure how to get Spring to handle a load of POST data when it won't know what types to bind to in advance.
One way that I can think of doing this is to write your own custom HandlerMethodArgumentResolver , which is responsible for translating the request into the argument values of the controller methods. You should be able to create a custom annotation that will indicate to Spring MVC, that your custom handler method argument resolver will be resolving the specific annotated method arguments(say #CustomType Object argument).
Once the call comes into the handler resolver, you can probably determine the type that the json request should map to, and call the json mapper with the actual type.
You can register a custom argument resolver this way:
<annotation-driven>
<argument-resolvers>
<beans:bean class="..CustomArgumentResolver"/>
</argument-resolvers>
</annotation-driven>
I set a bean's property to a String object, then when I try to get the class name of the property ,below error is thrown out:
Expected hash. plist[0].javaType evaluated instead to freemarker.template.SimpleScalar on line 7, column 26 in ibatis/macro.ftl.
template code is as below:
<#assign clsName=plist[0].javaType.class.name>
When property javaType is set to a java bean, class name can be properly got. Why is it? I need the property could be given any type, java bean ,non java bean.
The root of the issue here is that FreeMarker doesn't work with Java values/objects directly. The template language has its own simple type-system, and stuff coming from outside is mapped to that through a technique called object-wrapping. (Values that doesn't come from outside doesn't even have a wrapped object inside.) That you was still able to get the class of some object is purely accidental... What happens is that the object-wrapping machinery decides that the object should be mapped to the "hash" FreeMarker type, and the hash items will correspond to the JavaBean properties of the objects. The object has a getClass() method, which is (mistakenly) seen as the getter of the "class" property.
So there's no universal way of getting the class... among others because sometimes there's no class to get. You could write a TemplateMethodModelEx that does a good enough effort to do so.