eclipse-ee4j / jersey

Eclipse Jersey Project - Read our Wiki:
https://github.com/eclipse-ee4j/jersey/wiki
Other
690 stars 352 forks source link

Add option to prevent registration of JsonParseExceptionMapper and JsonMappingExceptionMapper in jersey-media-json-jackson #3560

Closed jerseyrobot closed 3 years ago

jerseyrobot commented 7 years ago

Please provide an option to prevent the two Jackson mappers from registering. See the problem description : https://stackoverflow.com/questions/5833147/jersey-jackson-exception-problem-with-exceptionmapper (top voted answer).

Mappers are registered in https://github.com/jersey/jersey/blob/master/media/json-jackson/src/main/java/org/glassfish/jersey/jackson/JacksonFeature.java line 87 and 88.

Perhaps through an init parameter in web.xml or similar way.

At the moment there is simply no way to create your own exception mapper and expect jersey-media to respect it. It will randomly use the jersey defined mapper or your custom mapper, it changes from application run. Annotating custom mapper with @Priority does nothing, probably because jersey-media mappers are registered explicitely.

There used to be an issue with jackson library defining their own mapper beans but that was fixed upstream, so right now the last culprit is jersey-media.

The workaround in SO thread with excluding jersey-media and registering Jackson provider manualy works but I'd prefer using jersey-media but without the two mappers registered.

jerseyrobot commented 6 years ago
jerseyrobot commented 6 years ago

@Treppenhouse Commented +1

jerseyrobot commented 6 years ago

@mazmrini Commented +1

jerseyrobot commented 6 years ago

@waleed-dawood Commented +1

jerseyrobot commented 6 years ago

@bojan89 Commented +1

jerseyrobot commented 6 years ago

@jansupol Commented We need to think about more general approach that would prevent registering default providers, not only the mappers in this case.

jerseyrobot commented 6 years ago

@joewolschon Commented +1

As-is the default behavior will leak internal classes.

In one concrete example that we've personally ran into, posting to a resource with an Integer-typed field and passing back a value that is out of range will produce the following response message:

[Cannot deserialize value of typejava.lang.Integerfrom String "2147483648": Overflow: numeric value (2147483648) out of range of Integer (-2147483648 - 2147483647) at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 201] (through reference chain: path.to.our.class["someField"])]

We can create our own json exception mapper handler, but, like the original poster explained, there is no way to get jersey to respect which one is used so the only viable alternative is to avoid JacksonFeature altogether (which is what the documentation suggests you use). We could also potentially modify our object mapper resolver but that feels at odds with the intent of exception mappers.

jerseyrobot commented 6 years ago

@gweinert Commented +1

nebirhos commented 6 years ago

+1

cen1 commented 3 years ago

After upgrading to Jersey to 2.32 the exception mapper now appears to function properly. @Priority chain seems to take effect so it is possible to force override with a custom mapper. I think it is due to the same fix as with ValidationExceptionMapper in https://github.com/eclipse-ee4j/jersey/issues/3425

I think the issue can be closed. I am the original reporter but since the issue was imported I can no longer close it myself. Summoning @jansupol

jansupol commented 3 years ago

Thank you @cen1. Closing.