Returning JSON with spring on 404 in XML free project

前端 未结 1 1149
北荒
北荒 2021-01-24 13:36

I am currently setting up a Spring MVC application (version 4.1.4.RELEASE) and I want the application to return a JSON string on a 404 error rather than the default html respons

1条回答
  •  时光说笑
    2021-01-24 14:15

    I don't think the return type you're trying to use is supported. Have you tried changing your return value to ResponseEntity or adding a @ResponseBody annotation? From the docs:

    • A ModelAndView object (Servlet MVC or Portlet MVC).
    • A Model object, with the view name implicitly determined through a RequestToViewNameTranslator.
    • A Map object for exposing a model, with the view name implicitly determined through a RequestToViewNameTranslator.
    • A View object.
    • A String value which is interpreted as view name.
    • @ResponseBody annotated methods (Servlet-only) to set the response content. The return value will be converted to the response stream using message converters.
    • An HttpEntity or ResponseEntity object (Servlet-only) to set response headers and content. The ResponseEntity body will be converted and written to the response stream using message converters.
    • void if the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse / HttpServletResponse / RenderResponse for that purpose) or if the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature; only applicable in a Servlet environment).

    0 讨论(0)
提交回复
热议问题