Is there a way to add a cookie to a method with @ResponseBody annotation?

I am building my web application based on Spring MVC and am facing a problem while trying to add a cookie to my ajax request handling method.

I realized that the method with @ResponseBody (in my example it returns a string value) does not create a "real" response and the added cookies are lost.

Is it possible to add a cookie to a method called via ajax (and hence annotated with @ResponseBody) in Spring MVC?

+3


source to share


1 answer


For this you can use the following signature



@ResponseBody
public String x((HttpServletRequest request, HttpServletResponse response){
    Cookie userCookie = new Cookie("<name>", "<valie>");
    //set other cookie properties
    response.addCookie(userCookie);

    return "xxx";
}

      

+4


source







All Articles