Spring Framework
  1. Spring Framework
  2. SPR-6008

@ResponseStatus on @ExceptionHandler method is ignored

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 3.0 M3
    • Fix Version/s: 3.0 RC1
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      @ResponseStatus annotations have no effect when used on @ExceptionHandler methods

      Controller:

      package com.test;
      
      import java.io.PrintWriter;
      
      import org.springframework.http.HttpStatus;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.ExceptionHandler;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.ResponseStatus;
      import org.springframework.web.servlet.ModelAndView;
      
      @Controller
      public class Test
      {
      	@ExceptionHandler
      	@ResponseStatus( value=HttpStatus.NOT_ACCEPTABLE )
      	public ModelAndView exception( Throwable t, PrintWriter out )
      	{
      		out.println( "Should be a 406" );
      		return new ModelAndView();
      	}
      	
      	@RequestMapping( "/busted" )
      	public void get( PrintWriter out )
      	{
      		throw new RuntimeException( "foo" );
      	}
      	
      	@RequestMapping( "/works" )
      	@ResponseStatus( value=HttpStatus.ACCEPTED )
      	public void works( PrintWriter out )
      	{
      		out.println( "Should be a 202" );
      	}
      }
      

      Note: I am returning an empty ModelAndView to work around another issue that I will open next.

      Everything is fine on the @RequestMapping

      $ curl -i "http://localhost/test/works"
      HTTP/1.1 202 Accepted
      Server: Apache-Coyote/1.1
      Content-Length: 17
      Date: Fri, 14 Aug 2009 19:27:11 GMT
      
      Should be a 202
      

      But not so on the @ExceptionHandler

      $ curl -i "http://localhost/test/busted"
      HTTP/1.1 200 OK
      Server: Apache-Coyote/1.1
      Content-Length: 17
      Date: Fri, 14 Aug 2009 19:27:16 GMT
      
      Should be a 406
      

        Issue Links

          Activity

          Hide
          William Ashley added a comment -

          Sorry, should have used the noformat macro

          package com.test;
          
          import java.io.PrintWriter;
          
          import org.springframework.http.HttpStatus;
          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.ExceptionHandler;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.ResponseStatus;
          import org.springframework.web.servlet.ModelAndView;
          
          @Controller
          public class Test
          {
          	@ExceptionHandler
          	@ResponseStatus( value=HttpStatus.NOT_ACCEPTABLE )
          	public ModelAndView exception( Throwable t, PrintWriter out )
          	{
          		out.println( "Should be a 406" );
          		return new ModelAndView();
          	}
          	
          	@RequestMapping( "/busted" )
          	public void get( PrintWriter out )
          	{
          		throw new RuntimeException( "foo" );
          	}
          	
          	@RequestMapping( "/works" )
          	@ResponseStatus( value=HttpStatus.ACCEPTED )
          	public void works( PrintWriter out )
          	{
          		out.println( "Should be a 202" );
          	}
          }
          
          Show
          William Ashley added a comment - Sorry, should have used the noformat macro package com.test; import java.io.PrintWriter; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.servlet.ModelAndView; @Controller public class Test { @ExceptionHandler @ResponseStatus( value=HttpStatus.NOT_ACCEPTABLE ) public ModelAndView exception( Throwable t, PrintWriter out ) { out.println( "Should be a 406" ); return new ModelAndView(); } @RequestMapping( "/busted" ) public void get( PrintWriter out ) { throw new RuntimeException( "foo" ); } @RequestMapping( "/works" ) @ResponseStatus( value=HttpStatus.ACCEPTED ) public void works( PrintWriter out ) { out.println( "Should be a 202" ); } }
          Hide
          Arjen Poutsma added a comment -

          Edited to add code tags.

          Show
          Arjen Poutsma added a comment - Edited to add code tags.

            People

            • Assignee:
              Arjen Poutsma
              Reporter:
              William Ashley
              Last updater:
              Trevor Marshall
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                4 years, 36 weeks, 2 days ago

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0d
                0d
                Logged:
                Time Spent - 13m
                13m