Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Controller/Component Implements Serializable

In a recent Web based Java project, I have come across various places where I have seen a Spring based Controller/Component implementing Serializable.

I am curious, as to why a Controller/Component should have to implement Serializable?

I think it is wrong practice to do so and searching so far for any scenario to implement Serializable has not yielded anything. I think it violates MVC principle and is unconventional. You would want to keep Domain/Business Objects separate from Controllers/Components.

I would like to correct this bad practice in the code base but just wanted to ensure I have a reason to do so.

Any explanation/scenario illustrating this case will be appreciated.

like image 331
perpetual_learner Avatar asked Oct 21 '22 16:10

perpetual_learner


1 Answers

There's no need for controllers to be Serializable. They're stateless and just mediate between the model and the view. The same could be said for other stateless components. There's no benefit in making them Serializable.

Stateful model objects, however, are often Serializable because they're sometimes placed in HttpSession. App servers require objects in session to be Serializable for replication and persistence purposes.

like image 186
Will Keeling Avatar answered Oct 26 '22 23:10

Will Keeling