【问题标题】:Spring 3.2 Web MVC @ModelAttribute without labelSpring 3.2 Web MVC @ModelAttribute 没有标签
【发布时间】:2013-03-16 08:08:49
【问题描述】:

我正在尝试让 Spring 3.2 MVC 返回没有默认标签的 JSON 响应。

例如,

@Controller
@RequestMapping("/dt")
public class DTAgentsController {

@ModelAttribute
@RequestMapping(method = RequestMethod.GET, produces = "application/json;UTF-8")
    public DTResponse agents() {
        DTResponse resp = new DTResponse();
        resp.setsEcho(1);
        resp.setiTotalDisplayRecords(10);
        resp.setiTotalRecords(50);
        return resp;
    }
}

返回

{"DTResponse":{"sEcho":1,"iTotalRecords":50,"iTotalDisplayRecords":10}}

我只希望 JSON 输出是

{"sEcho":1,"iTotalRecords":50,"iTotalDisplayRecords":10}

谢谢。

【问题讨论】:

    标签: java json spring-mvc modelattribute


    【解决方案1】:

    问题不在于@ModelAttribute,它只是意味着您要存储或从模型中获取哪些数据。看来您使用的是 jQuery 数据表,所以您应该在方法 agents() 中添加 @ResponseBody

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
        public DTResponse agents() {
            DTResponse resp = new DTResponse();
            resp.setsEcho(1);
            resp.setiTotalDisplayRecords(10);
            resp.setiTotalRecords(50);
            return resp;
        }
    }
    

    【讨论】:

    • 谢谢:)。像魅力一样工作。
    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多