Announcement Announcement Module
No announcement yet.
Forcing 404 From a Controller Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Forcing 404 From a Controller

    Is it possible for a Spring Controller to force a 404 that uses the error page defined in web.xml?

    I've tried servletResponse.setStatus(404), but it only returns the server's default 404 error message.

  • #2
    I think if you do something in your controller that causes a 'FileNotFound' exception to be thrown, you will cause the container to throw a 404.


    • #3
      Thanks for the suggestion, but I'm afraid it didn't work. I tried inserting:

      throw new FileNotFoundException();
      but got a 500 error instead of a 404.

      Any other ideas?


      • #4
        What about ServletException?


        • #5
          Still no, sadly.

          I tried:
          throw new ServletException(new FileNotFoundException());
          and still just got a 500 error.


          • #6
            Hi mark_m,
            you can set servletResponse.setStatus(404) and in your web.xml you must define this:

            where error.htm is your error page handler.

            I hope this work for you.


            • #7
              Thanks m_auro1, but I've already tried that.

              For clarification, the error page specified in my web.xml works perfectly when a URL requests a page not defined in any Controller. However, I'm looking to explicitly invoke a 404 error within a Controller after a request has been accepted.

              Specifically, I have a situation like this:

              @RequestMapping(value = URI_GETIMAGE, method = RequestMethod.GET)
              public ModelAndView getImage(ModelMap model, @RequestParam(PARAM_ID) Long id, HttpServletResponse servletResponse) {
                 if( imageService.hasImage(id) )
                    // write the image to the response
                    // explicitly invoke a 404 Not Found error
              Essentially, I'm grabbing an image from a database BLOB field and delivering it by writing the BLOB's datastream to the servletResponse. However, if the id of that image is invalid, I would like to throw a 404 to indicate that the image does not exist. Like I mentioned before, I can set the servletResponse's status to 404, but then the error page defined in my web.xml is not used.

              Is there anything I'm missing?
              Last edited by mark_m; Feb 4th, 2009, 01:21 PM.


              • #8
                Anyone? I'm really completely stuck here.


                • #9
                  What about redirecting the client to a url which you know does not exist? That way the client will send an invalid request and you will see your 404...


                  • #10
                    Well, it's not particularly elegant, but it works!

                    Thanks, kabadisha.


                    • #11
                      Yeah, I agree it isn't very elegant, but at least you have a solution till you stumble upon something better :-)

                      Glad to help!