title: Persiting a state machine context in HttpSession java.io.NotSerializableException: org.springframework.statemachine.support.DefaultStateMachineContext
body: <p>I have created a <code>StateMachinePersist</code> implementation to persist the StateMachineContext y HttpSession:</p>
  
  <pre><code>@Suppress("UNCHECKED_CAST")
  class SessionStateMachinePersist(private val session: HttpSession) : StateMachinePersist&lt;States, String, String&gt; {
  
      private val logger = LoggerFactory.getLogger(javaClass)
  
      override fun read(contextObj: String): StateMachineContext&lt;States, String&gt;?  {
          val stateMachineContext = session.getAttribute("${session.id}-$contextObj") as StateMachineContext&lt;States, String&gt;?
          logger.debug("Session {} has state machine context {}?", session.id, contextObj, stateMachineContext != null)
          return stateMachineContext
      }
  
      override fun write(context: StateMachineContext&lt;States, String&gt;, contextObj: String) {
          session.setAttribute("${session.id}-$contextObj", context)
      }
  }
  </code></pre>
  
  <p>When it is going to persist the <code>DefaultStateMachineContext</code> it throws the following exception since the <code>DefaultStateMachineContext</code> isn't serializable: <code>Caused by: java.io.NotSerializableException: org.springframework.statemachine.support.DefaultStateMachineContext</code></p>
  
  <p>How can I use a StateMachineContext implementation that is serializable?</p>