Skip to content

Allow HTTP status exceptions to be easily thrown from Controllers [SPR-14895] #19461

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

Closed
spring-projects-issues opened this issue Nov 10, 2016 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Nov 10, 2016

Norris Shelton opened SPR-14895 and commented

There is no way to throw exceptions out of the box. Developers must create exceptions for each HTTP status they want to throw, create exception handlers, etc. It would be much better if basic functionality was included. I created a ResourceException class (probably could have a better name for Spring), a generic controller advice (maybe call it something specific to the exception class). If they are included in the SpringFramework, it would be backwards compatible and it would be very easy to throw an exception. An example usage would be:

throw new ResourceException(HttpStatus.BAD_REQUEST, "Missing the user first name");


Affects: 4.3.4

Reference URL: http://stackoverflow.com/questions/16232833/how-to-respond-with-http-400-error-in-a-spring-mvc-responsebody-method-returnin/40518283#40518283

Attachments:

Issue Links:

2 votes, 6 watchers

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

We have the ResponseStatusException in 5.0 for use on the Spring Web Reactive side. We can support that in Spring MVC as well probably as an enhancement of the ResponseStatusExceptionResolver. I see no reason why we couldn't backport that to 4.3.x as well.

@spring-projects-issues
Copy link
Collaborator Author

Eric Deandrea commented

This would be welcomed as I've basically had to implement my own version of this.

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

I'd love to see this back ported to 4.3. We have very similar scenarios in Spring Data REST, for which we could a couple of custom classes if that was available.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

The ResponseStatusException and sub-classes are new in Spring 5 and used on the reactive side. I suppose we could backport just the ResponseStatusException for use in applications. Juergen Hoeller?

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Since ResponseStatusException lives in the new web.server package and comes with several subclasses out of the box, I'd rather leave it 5.0 only.

An isolated backport to 4.3.x would have to happen in a different package and without the subclasses... or we'd have to generally move them even in 5.0... and finalize their location as of M4. All in all, since this can be achieved with a custom exception in 4.3.x already, I see no hurry here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants