Spring MVC 4: “application/json” Content Type is not being set correctly

前端 未结 6 1674
旧巷少年郎
旧巷少年郎 2020-11-30 00:41

I have a controller mapped with the following annotation:

@RequestMapping(value = \"/json\", method = RequestMethod.GET, produces = \"application/json\")
@Re         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 01:14

    As other people have commented, because the return type of your method is String Spring won't feel need to do anything with the result.

    If you change your signature so that the return type is something that needs marshalling, that should help:

    @RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Map bar() {
        HashMap map = new HashMap();
        map.put("test", "jsonRestExample");
        return map;
    }
    

提交回复
热议问题