Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@RequestMapping get 406 Not Acceptable when I use MediaType.IMAGE_PNG_VALUE [SPR-17205] #21738

spring-projects-issues opened this issue Aug 22, 2018 · 3 comments
status: duplicate


Copy link

@spring-projects-issues spring-projects-issues commented Aug 22, 2018

Eduardo Cintra opened SPR-17205 and commented

I have a method in my controller that return a image, but exists some rules that can throws a exception, when this occurs I'm get a 406 error "406 Not Acceptable" instead of the Exception.

My method:

@RequestMapping(value = "/test", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE) @RequestMapping(value = "/test", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)

public @ResponseBody byte[] test() {

throw new RuntimeException()

//code that return byte[] image

Can anyone help me?


Issue Links:

  • #20865 @RequestMapping produces condition should not impact error responses ("duplicates")
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 23, 2018

Brian Clozel commented

Which Spring Framework version are you using?

Could you provide a small application reproducing the issue?

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 25, 2018

Eduardo Cintra commented

Brian Clozel

Sorry for the delay, I'm using Spring Boot version 2.0.4.RELEASE.

I created a project and put it in a github repository, it's a simple project just to reproduce my problem, when runs the project exists one route that you should send a fake token just to I can simulate a validate rule. 

A valid token is:


When you send a get to this URL, you get a Image as expected.

If you sends another token, example:


I expected an exception, but instead comes a "406 Not Acceptable".


Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 27, 2018

Brian Clozel commented

Hi Eduardo Cintra,

I understand the problem now, thanks to your repro project. The problem is that Spring MVC performs content negotiation while selecting the controller handler. If an exception happens during handling, exception handling kicks in but in several cases the content negotiation process does not start over. In this case, Spring MVC is trying to translate your @ResponseStatus exception into an PNG image HTTP response.

In the meantime, you can try and use a ResponseEntity return type on a @ExceptionHandler method.

This has been solved in #20865 - the fix is available as of Spring Framework 5.1 and has not been packported as it's a rather important behavior change and developers might rely on that, even if the fix is the proper behavior.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
status: duplicate
None yet

No branches or pull requests

2 participants