Is a class passed as parameter in controller method and marked by @RequestBody
annotation required to implement the Serializable
interface?
I have same question for return value class marked by @ResponseBody
annotation.
I also will be very grateful for explanation why the Serializable
interface is or isn't required.
It does not. When you return an instance from controller method annotated with @ResponseBody
you might say that it is serialized to JSON for instance. But this kind of serialization is not the Java serialization which involves Serializable
interface.
@RequestBody
and @ResponseBody
annotations are handled by RequestResponseBodyMethodProcessor
which uses HttpMessageConverter
implementations to perform the conversion for example from object to JSON or from JSON to object.
When you look at the HttpMessageConverter
interface there is a canRead
method which has the following signature: boolean canRead(Class<?> clazz, MediaType mediaType);
and as you can see it is not bound to only serializable classes using generics.
Sources and more information
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