Kevin Tan
Kevin Tan

Reputation: 1048

How do you set and get cookie values in Micronaut monolith?

The question is simple. How do you set and get cookie values in Micronaut monolith that uses Thymeleaf for server side rendering?

I already know how to get the cookie values based on the solution here

Bind the cookie value(s): https://docs.micronaut.io/latest/guide/index.html#binding And then pass them as the model to your thymeleaf view: https://micronaut-projects.github.io/micronaut-views/latest/guide/index.html

but what about setting a cookie value programmatically since I'm using a third party auth provider?

Upvotes: 0

Views: 1958

Answers (1)

Sascha Frinken
Sascha Frinken

Reputation: 3356

I don't think that thymeleaf makes any difference. But following Groovy sample shows how to receive and set cookies in a micronaut controller.

@Controller
class CookieController {

    @Get
    HttpResponse<ModelAndView> cookie(HttpRequest<?> request) {

        // receive cookie
        def myCookie = request.cookies.all.find { it.name == "my-cookie" }
        println myCookie?.value

        return HttpResponse.ok(new ModelAndView("view", [key: "value"]))
                // set cookie
                .cookie(new SimpleCookie("another-cookie", "value"))
    }
}

Upvotes: 1

Related Questions