Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to serialize objects for using @RequestBody @ResponseBody annotations

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.

like image 231
bmlynarczyk Avatar asked Nov 15 '14 16:11

bmlynarczyk


1 Answers

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

  • RequestResponseBodyMethodProcessor JavaDoc
  • HttpMessageConverter JavaDoc
like image 132
Bohuslav Burghardt Avatar answered Oct 16 '22 08:10

Bohuslav Burghardt