Working with Sessions

A Session object can be retrieved simply by declaring the Session in a controller method signature. For example consider the following controller:

  1. import io.micronaut.http.annotation.Controller;
  2. import io.micronaut.http.annotation.Get;
  3. import io.micronaut.http.annotation.Post;
  4. import io.micronaut.session.Session;
  5. import io.micronaut.session.annotation.SessionValue;
  6. import javax.annotation.Nullable;
  7. import javax.validation.constraints.NotBlank;
  8. @Controller("/shopping")
  9. public class ShoppingController {
  10. private static final String ATTR_CART = "cart"; (1)
  11. @Post("/cart/{name}")
  12. Cart addItem(Session session, @NotBlank String name) { (2)
  13. Cart cart = session.get(ATTR_CART, Cart.class).orElseGet(() -> { (3)
  14. Cart newCart = new Cart();
  15. session.put(ATTR_CART, newCart); (4)
  16. return newCart;
  17. });
  18. cart.getItems().add(name);
  19. return cart;
  20. }
  21. }
  1. import io.micronaut.http.annotation.Controller
  2. import io.micronaut.http.annotation.Get
  3. import io.micronaut.http.annotation.Post
  4. import io.micronaut.session.Session
  5. import io.micronaut.session.annotation.SessionValue
  6. import javax.annotation.Nullable
  7. import javax.validation.constraints.NotBlank
  8. @Controller("/shopping")
  9. class ShoppingController {
  10. static final String ATTR_CART = "cart" (1)
  11. @Post("/cart/{name}")
  12. Cart addItem(Session session, @NotBlank String name) { (2)
  13. Cart cart = session.get(ATTR_CART, Cart.class).orElseGet({ -> (3)
  14. Cart newCart = new Cart()
  15. session.put(ATTR_CART, newCart) (4)
  16. newCart
  17. })
  18. cart.getItems().add(name)
  19. cart
  20. }
  21. }
  1. import io.micronaut.http.annotation.Controller
  2. import io.micronaut.http.annotation.Get
  3. import io.micronaut.http.annotation.Post
  4. import io.micronaut.session.Session
  5. import io.micronaut.session.annotation.SessionValue
  6. @Controller("/shopping")
  7. class ShoppingController {
  8. @Post("/cart/{name}")
  9. internal fun addItem(session: Session, name: String): Cart { (2)
  10. require(name.isNotBlank()) { "Name cannot be blank" }
  11. val cart = session.get(ATTR_CART, Cart::class.java).orElseGet({
  12. (3)
  13. val newCart = Cart()
  14. session.put(ATTR_CART, newCart) (4)
  15. newCart
  16. })
  17. cart.items.add(name)
  18. return cart
  19. }
  20. }
1The ShoppingController declares a Session attribute called cart
2The Session is declared as a parameter to the method
3The cart attribute is retrieved
4Otherwise a new Cart instance is created and stored in the session

Note that because the Session is declared as a required parameter to the execute the controller action, the Session will be created and saved to the SessionStore.

If you don’t want to create unnecessary sessions then you can declare the Session as @Nullable in which case a session will not be created and saved unnecessarily. For example:

Using @Nullable with Sessions

  1. @Post("/cart/clear")
  2. void clearCart(@Nullable Session session) {
  3. if (session != null) {
  4. session.remove(ATTR_CART);
  5. }
  6. }

Using @Nullable with Sessions

  1. @Post("/cart/clear")
  2. void clearCart(@Nullable Session session) {
  3. if (session != null) {
  4. session.remove(ATTR_CART)
  5. }
  6. }

Using @Nullable with Sessions

  1. @Post("/cart/clear")
  2. internal fun clearCart(session: Session?) {
  3. session?.remove(ATTR_CART)
  4. }

The above method will only create and inject a new Session if one already exists.