I'm trying to understand when the fail on empty bean happens , from doc :
public static final SerializationFeature FAIL_ON_EMPTY_BEANS Feature that determines what happens when no accessors are found for a type (and there are no annotations to indicate it is meant to be serialized). If enabled (default), an exception is thrown to indicate these as non-serializable types; if disabled, they are serialized as empty Objects, i.e. without any properties. Note that empty types that this feature has only effect on those "empty" beans that do not have any recognized annotations (like @JsonSerialize): ones that do have annotations do not result in an exception being thrown.
Feature is enabled by default.
Now I have tried to create an empty POJO (no getters ):
@Component
public class Wrapper2 {
private String name;
}
The wrapper class which I want to serialize :
@Component
public class Wrapper {
@Autowired
private Wrapper2 wrapper2;
}
The controller:
@RestController
public class TestController {
@Autowired
private Wrapper wrapper;
@GetMapping("/test")
public Wrapper test() {
return wrapper;
}
}
however I always get the wrapper serialized even with adding/removing getter and setter for wrapper2. can anyone explain when the error can happen ?
You need your bean to be empty and without recognized annotations
Note that empty types that this feature has only effect on those "empty" beans that do not have any recognized annotations
So if you remove the annotations it'll failed
public class Wrapper {
}
Error will be as
Unrecognized field "wrapper" (class com.Wrapper ), not marked as ignorable (0 known properties: ])
Jackson relevant code applied when mark it as UnknownSerializer
:
if (isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)) { if (ser instanceof UnknownSerializer) { return true;
Similar exception with nested empty class:
public class Wrapper {
private Wrapper2 wrapper2;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With