Christian
Christian

Reputation: 7429

Prevent a InternalServerErrror while using BeanValidation

I am using BeanValidation (with DropWizzard). Now if a form contains a field annotated with @NotEmpty, but is empty, I'll get an InternalServer ErrorException with Status Code 500. I'd like to log a RuntimeException for this and forward the user to an error page.

Is it possible to catch all ValidationException in one place, log them and do something like forwarding the user?

Upvotes: 0

Views: 43

Answers (1)

Alexander
Alexander

Reputation: 26

You can build your own exception mapper for the ValidationException. Jersey have its own ValidationExceptionMapper implementation that will return a bad request if the element is a parameter that is validated or a internal server error if the validation occur on a return value. Latest version of Dropwizard should configure these mappers by default.

To build your own exception mapper you should implement the interface javax.ws.rs.ext.ExceptionMapper and register it in the jersey context of Dropwizard ieenvironment.jersy().register(MyExceptionMapper.class)if you use Dropwizard 0.8 or later

Upvotes: 1

Related Questions