Last active
August 29, 2015 13:57
-
-
Save thomd/9441618 to your computer and use it in GitHub Desktop.
put model into play (1.x framework) session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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