title: NotSerializableException in ViewScop Bean in JSF
body: <p>The code written below is to download the file from the database by using JSF2.0/Primefaces. The code below which is SessionScoped Bean runs perfectly fine and I am able to download the stored file</p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class FileProcessBB{
  
  private ScreenDisplayData screenDisplayData;
  
      public void selectAttachment() {
          AttachedFileTable attachedFileTable = new AttachedFileTableManager().getAttachedFileById(screenDisplayData.getSelectedAttachedFileBO().getAttachedFileTableId());
          InputStream inputStream = new ByteArrayInputStream(attachedFileTable.getAttachedFile());
          String contentType = attachedFileTable.getAttachedFileContentType();
          String fileName = attachedFileTable.getAttachedFileName();
          screenDisplayData.setAttachedFileStreamContent(new DefaultStreamedContent(inputStream, contentType, fileName));
      }
  }
  </code></pre>
  
  <p>But I want implement the same functionality into ViewScoped Bean. To achieved that I made necessary changes as below (implementing Serializable for ViewScoped Bean)</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class FileProcessBB implements Serializable{
  /**
   * 
   */
  private static final long serialVersionUID = 6137719800118206851L;
  
  private ScreenDisplayData screenDisplayData;
  
      public void selectAttachment() {
          AttachedFileTable attachedFileTable = new AttachedFileTableManager().getAttachedFileById(screenDisplayData.getSelectedAttachedFileBO().getAttachedFileTableId());
          InputStream inputStream = new ByteArrayInputStream(attachedFileTable.getAttachedFile());
          String contentType = attachedFileTable.getAttachedFileContentType();
          String fileName = attachedFileTable.getAttachedFileName();
          screenDisplayData.setAttachedFileStreamContent(new DefaultStreamedContent(inputStream, contentType, fileName));
      }
  }
  </code></pre>
  
  <p>ScreenDisplayData</p>
  
  <pre><code>public class ScreenDisplayData implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = -3339259618782904262L;
  
      private StreamedContent attachedFileStreamContent;
      private AttachedFileBO selectedAttachedFileBO;
  
  
      //getter setter
  
  }
  </code></pre>
  
  <p>I have implements Serializable in the AttachedFileBO class also. AttachedFileTable is an Entity class hence it already implements Serializable</p>
  
  <p>Where did I am missing Serializable?</p>
  
  <p>EXCEPTION I am getting is as below</p>
  
  <pre><code>[ERROR   ] SRVE0777E: Exception thrown by application class 'java.io.ObjectOutputStream.writeObject0():1200'
  java.io.NotSerializableException: java.io.ByteArrayInputStream
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1200)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1513)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1513)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1513)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:363)
    at java.util.HashMap.writeObject(HashMap.java:867)
    at sun.reflect.GeneratedMethodAccessor156.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:613)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1047)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1500)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1382)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1190)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:363)
    at java.util.HashMap.writeObject(HashMap.java:867)
    at sun.reflect.GeneratedMethodAccessor156.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:613)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1047)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1500)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1382)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1190)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:363)
    at org.apache.myfaces.shared_impl.util.StateUtils.getAsByteArray(StateUtils.java:273)
    at org.apache.myfaces.shared_impl.util.StateUtils.construct(StateUtils.java:235)
    at org.apache.myfaces.renderkit.html.HtmlResponseStateManager.getViewState(HtmlResponseStateManager.java:314)
    at org.apache.myfaces.application.jsp.JspStateManagerImpl.getViewState(JspStateManagerImpl.java:626)
    at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartialRendering(PartialViewContextImpl.java:443)
    at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:344)
    at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:88)
    at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:358)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:609)
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1159)
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:263)
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1221)
    at [internal classes]
  </code></pre>