Skip to content

@ResponseStatus on @ExceptionHandler method is ignored [SPR-6008] #10676

Closed

Description

@spring-projects-issues

William Ashley opened SPR-6008 and commented

@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

Affects: 3.0 M3

Issue Links:

Referenced from: commits 3b7691d

Activity

  1. spring-projects-issues commented on Aug 14, 2009

    @spring-projects-issues
    CollaboratorAuthor

    William Ashley commented

    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" );
    	}
    }
    
  2. spring-projects-issues commented on Aug 18, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Arjen Poutsma commented

    Edited to add code tags.

  3. added
    type: bug
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions