有没有什么办法可以增加饼的方法@ResponseBody注释? [英] Is there any way to add cookie in method with @ResponseBody annotation?

查看:211
本文介绍了有没有什么办法可以增加饼的方法@ResponseBody注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立我的web应用程序基于Spring MVC和遇到一个问题,而试图添加一个cookie的方法处理Ajax请求。

I build my web application based on Spring MVC and come across a problem whilst trying to add a cookie in a method handling an ajax request.

我意识到,法@ResponseBody(在我的例子它返回一个字符串值)不创建一个真实的响应,并添加cookie丢失。

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

有没有什么办法来添加一个cookie在Spring MVC的?

Is there any way to add a cookie in a method called via ajax (and therefore annotated with @ResponseBody) in Spring MVC?

推荐答案

您可以使用下面的签名来做到这一点。

You can use the following signature to do this

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

    return "xxx";
}

这篇关于有没有什么办法可以增加饼的方法@ResponseBody注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆