Spring MVC returning JSONS and exception Handling

后端 未结 3 924
庸人自扰
庸人自扰 2021-02-06 07:33

I am using Spring MVC with Controllers, my question is how do I return a JSON response which is different from the @ResponseBody object which is returned and convereted to a JSO

3条回答
  •  情歌与酒
    2021-02-06 08:25

    Yes. Return a model and a view instead.

    public ModelMap getUserDetails() {
        UserDetails userDetails; // get this object from somewhere
        ModelMap map = new ModelMap()(;
        map.addAttribute("data", userDetails);
        map.addAttribute("success", true);
        return map;
    }
    

    To add the exception you'd do it the same way with a key and success = false.

提交回复
热议问题