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

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

Comments

@spring-projects-issues
Copy link
Collaborator

@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?


Attachments:

Issue Links:

  • #20865 @RequestMapping produces condition should not impact error responses ("duplicates")
@spring-projects-issues
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?

@spring-projects-issues
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.

https://github.com/eduardocintra/springboot-mediatype 

A valid token is:

http://localhost:8080/image/xOAyJtM6Bvqa0aXsAlmQChh4XpvpMzRH

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

If you sends another token, example:

http://localhost:8080/image/xvsjyYaniajYgauhanuUYGyaguah

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

!invalid.png!

@spring-projects-issues
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.

Thanks!

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

No branches or pull requests

2 participants