Skip to content

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

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions