Serializing with Jackson (JSON) - getting "No serializer found"?
Solution 1:
As already described, the default configuration of an ObjectMapper
instance is to only access properties that are public fields or have public getters/setters. An alternative to changing the class definition to make a field public or to provide a public getter/setter is to specify (to the underlying VisibilityChecker
) a different property visibility rule. Jackson 1.9 provides the ObjectMapper.setVisibility()
convenience method for doing so. For the example in the original question, I'd likely configure this as
myObjectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
For Jackson >2.0:
myObjectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
For more information and details on related configuration options, I recommend reviewing the JavaDocs on ObjectMapper.setVisibility()
.
Solution 2:
Add a
getter
and a
setter
and the problem is solved.
Solution 3:
For Jackson to serialize that class, the SomeString
field needs to either be public
(right now it's package level isolation) or you need to define getter and setter methods for it.