Spring MVC REST Handing Bad Url (404) by returning JSON

前端 未结 4 822
醉梦人生
醉梦人生 2021-01-30 23:56

I am developing a REST service using SpringMVC, where I have @RequestMapping at class and method level.

This application is currently configured to return error-page js

4条回答
  •  故里飘歌
    2021-01-31 00:51

    you can return json in the location below,that /handle/404.

    
        404
        /handle/404
    
    

    after you config this in web.xml,a 404 error will redirect to /handle/404,and you can create a controller with this mapping and return a json result. for example.

    @RestController
    @RequestMapping(value = "handle")
    public class HttpErrorController {
       @RequestMapping(value = "404")
       public String handle404() {
          return "404 error";
       }
    }
    

提交回复
热议问题