-1

I have a simple jsf 2.1 that used to work fine on Java EE 6 using primefaces 3.4.

When I migrated to glassfish 4.0 and primefaces 5.1 I've got the following exceptions each time I redeploy the project on Netbeans:

java.io.NotSerializableException: org.primefaces.model.DefaultStreamedContent

java.io.NotSerializableException: org.primefaces.component.datatable.DataTable

Even if this exception is thrown, the project is deployed and run correctly!

What could be wrong?

Community
  • 1
  • 1
Hicham
  • 100
  • 2
  • 14
  • possible dublicate http://stackoverflow.com/questions/26320170/java-io-notserializableexception-org-primefaces-component-datatable-datatable – user3424032 Oct 12 '14 at 15:22
  • The other one is similar but related to another component and its exception is : java.io.NotSerializableException: org.primefaces.component.datatable.DataTable – Hicham Oct 15 '14 at 16:43

2 Answers2

5

You've declared those types as a property of a view or session scoped managed bean. You should absolutely not do that. You should declare them as a property of a request scoped bean.

View and session scoped beans must be Serializable because view scoped beans are reused/shared across multiple requests on the same view in the same session, and session scoped beans are reused/shared across multiple requests in the same session. Anything tied to a specific HTTP session must be Serializable, because it enables the server to store sessions on disk, so it could be shared among other servers in a cluster, or survive server restarts.

The DefaultStreamedContent (and the InputStream it is wrapping, if any) may absolutely not be created and assigned as a view/session scoped bean property, not only because it's not serializable, but also because it can be read only once. You need to create this in the getter method only. This is indeed a rather special case which is fleshed out further in this answer: Display dynamic image from database with p:graphicImage and StreamedContent

The DataTable is a JSF component which you most likely referenced via binding attribute. It may absolutely not be assigned as a view/session scoped bean property, because UI components are inherently request scoped. Reusing the same UI component instance across multiple restored views in the same session may cause its state being shared across multiple requests (NOT threadsafe thus!) and/or potential "Duplicate Component ID" errors. See also a.o. How does the 'binding' attribute work in JSF? When and how should it be used?

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
0

NotSerializableException is thrown when an instance of a class must implement the Serializable interface.

If the class that throws the exception does not belong to a third-party library, find the class and make it implement the serializable interface.

If you do not want to serialize the objects in the class, you can mark the objects as transient, to make the serializable runtime ignore the objects.

You can read about it here

Community
  • 1
  • 1
lee
  • 26
  • 2