G. Demecki G. Demecki - 1 year ago 217
Java Question

How to override a built-in exception mapper in Jersey 2.23?

In one of my projects I've already upgraded Jersey from version

. But I'm struggling many hours with one problem. My project defines its own
for a
, but unfortunately Jersey already has a built-in exception mapper for this exception and I cannot override it.

I have registered correctly (I checked it) my own mapper which is presented below:

public class ValidationExceptionMapper implements
ExceptionMapper<ValidationException> {

public Response toResponse(ValidationException exception) {
return Response.status(Status.BAD_REQUEST).build();

but it is never being called. Jersey always pick up the
I've also tried to use
annotation for my custom mapper, but unfortunately Jersey doesn't take it into account.

So what is going on? It worked perfectly fine in the previous Jersey version, so it seems to be a regression bug.

I give up. Any clues?

Answer Source

It really turned out to be a regression bug in Jersey, introduced in January 2015.

Bug is related with two Jersey's extensions: for Weld and bean validation. Because without Weld container started, my custom ValidationExceptionMapper mapper takes precedence over the built-in one provided by the jersey-bean-validation module, so my goal is achieved.

I've filled a bug report under JERSEY-3153.

To be honest, I'm never ever going to use Weld + Jersey again... I'm so tired with this combination. Through the last two years I've encountered around 10 bugs already. I'm really tired.

Anyway, I hope it will help somebody.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download