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

ability to set response status on ModelAndView [SPR-13560] #18136

Closed
spring-projects-issues opened this issue Oct 9, 2015 · 4 comments
Closed

ability to set response status on ModelAndView [SPR-13560] #18136

spring-projects-issues opened this issue Oct 9, 2015 · 4 comments

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 9, 2015

Christopher Smith opened SPR-13560 and commented

I am supporting resource deletion under certain circumstances, but semantic preconditions (referential integrity) must be enforced, and if they aren't, I want to return a 422. This is fairly simple in a REST controller, but in an HTML controller, I want to send the user an HTML page describing the condition, along with the 422 status code.

I can programmatically set the status code for a REST response with a ResponseEntity, and I can statically set the status code for an HTML response with @ResponseStatus, but there's no mechanism for programmatically setting the status code when using a ModelAndView. I would like a status field on ModelAndView that I can set based on controller logic.


Affects: 4.2.1

Issue Links:

  • #19362 ModelAndView's setStatus does not work for @ExceptionHandler methods

1 votes, 4 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 15, 2016

Rossen Stoyanchev commented

Giving this some thought, it sounds like it could be modeled as an error. Raise an error that indicates a referential integrity issue. The SimpleMappingExceptionResolver supports rendering with a view plus associating the view with a status code. Seems like a good fit to me. Note that the same can also be done with an @ExceptionHandler method that returns a ResponseEntity on the @RestController side.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 15, 2016

Christopher Smith commented

I believe there are additional use cases for wanting to dynamically determine the status code for an HTML return; for example, some sort of request might be processable immediately (200 or 201) or might require scheduling (202). There currently is no easy way for an HTML controller to vary the status programmatically.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 20, 2016

Rossen Stoyanchev commented

See commit a5f4aa.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 21, 2016

Christopher Smith commented

Exactly what I was hoping to have. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants