Skip to content

Instantly share code, notes, and snippets.

@thomd
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomd/9441618 to your computer and use it in GitHub Desktop.
Save thomd/9441618 to your computer and use it in GitHub Desktop.
put model into play (1.x framework) session
package controllers;
import play.*;
import play.mvc.*;
import java.util.*;
import models.*;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.DeserializationConfig;
import java.io.IOException;
public class Application extends Controller {
// The Play! session is not living on the server side but on the client side.
// In fact, it is stored in a signed cookie. This session is therefore limited to 4kb.
//
// It is NOT recommended to store a model in a play session as it may contain a huge object graph
//
public static ObjectMapper mapper = new ObjectMapper();
public static void index() throws JsonGenerationException, JsonMappingException, IOException {
User user1 = new User("bob", 37);
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
session.put("user", mapper.writeValueAsString(foo1));
User user2 = mapper.readValue(session.get("user"), User.class);
Logger.info("name: %s, age: %s", user2.name, user2.age);
render();
}
}
require:
- play
- org.codehaus.jackson -> jackson-mapper-asl 1.9.13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment