-
Notifications
You must be signed in to change notification settings - Fork 80
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
Remove @Provider
annotation from JsonParseExceptionMapper
and JsonMappingExceptionMapper
#22
Comments
Hmmh. I don't know what could be done here actually: these are NOT registered anywhere, just offered for convenience. For any usage would have to be either explicit (intentional), or use scanning from classpath. Be that as it may, I am open to suggestions for improvement. But don't quite see an obvious solution here. |
I solved this "problem" by using javax.ws.rs.Application to programmatically, and explicitly register my classes (in that way no automatic classpath scan is done), because if I used only web.xml somewhow jackson included classes would be automatically scanned and registered by glassfish (btw is there any way to disable automatic classpath scanning when using web.xml?) But I guess you're right, if this is due to the automatic scan then there's nothing jackson could do (except document this possible behaviour and possible ways to avoid it) |
Ok. I hope we'll figure something out -- I agree that this is problematic, so your problem report is valid. One possibility would be to just remove |
That would certainly be an option. With minimal effor the user could "activate" that provider, and if he wanted he could override the methods to have it's own implementation. Users that don't know about that class and create their own won't have any "unexpected" mappings which is good imo. |
I agree with just removing |
I know this is an old post, but since it still remains open I would like to add some more commentary in hopes of removing the @Provider annotation from JsonMappingExceptionMapper and JsonParseExceptionMapper As described above, due to explicitly marking JsonMappingExceptionMapper or JsonParseExceptionMapper with @Provider it will get auto-registerd with JAX-RS implementations and therefore make it more difficult to override this exception mapper with something more customized. And since these were offered for convenience it seems to defeat the intended purpose. With regard to the comment "...removing @Provider, unfortunately it would break existing implementations" this is not entirely true. The net effect would be the same, a JsonMappingException or JsonParseException would be raised and sent to the client including the same 400 BAD REQUEST. The only thing that would be different would be the text returned to client. |
There is any update on this issue? Any workaround to override the exceptions? |
No progress that I know of; I don't think anyone is working on it. |
I found a workaround, when JacksonJaxbJsonProvider is registered than the JsonMappingExceptionMapper and JsonParseExceptionMapper will not be registered by JacksonFeature (At least in the version we use). Simple add this to your JerseyConfigurer implementation configure method: |
Thank you h-yaron, your workaround worked for me, despite the fact that I was not explicitly registering JacksonJaxbProvider. |
Please remove all the Exception Mappers and let the users have their own. Really bad design, isn't it? Tons of developers' hours have been wasted. The price would be incompatibility with previous versions, but it is worth it. |
Once again do note that the implementation is merely included in jar, and NOT explicitly registered ANYWHERE. If you choose to include it or decide to use classpath auto-discovery, it may be found. Either way you are including it and can as well exclude it. Especially if you are using classpath-discovery you are sort of getting whatever there might be, and should really consider how fragile it is to use mechanism that relies on something as arbitrary as what might be found from fully expanded classpath, including all dependencies of your web app and everything it depends indirectly. |
@mpellegrini Do you have a link to JAX-RS specification (or implementation specific documents) that detail how auto-discovery for If it is the case that major frameworks (or, worse, JAX-RS 2.x) specify default to use classpath auto-discovery I will consider removal of |
@cowtowncoder I am currently working in and constrained to using JavaEE 6 level specs. Therefore the reference to JAX-RS is to the 1.1 Spec (https://jsr311.java.net/nonav/releases/1.1/spec/spec.html). Not sure if JAX-RS 2.0 changes this behavior or not. In Chapter 2 Applications, section 2.3 Publication it states the following (see bold highlights) :
|
@mpellegrini Thank you for including relevant section(s). One follow-up question: what does 'root' in "all root resource classes and providers packaged in the web application" mean? Also: assuming that |
@Provider
annotation from JsonParseExceptionMapper
and JsonMappingExceptionMapper
Multiple people have confirmed that |
@cowtowncoder, seems like you just changed the comment in JsonParseExceptionMapper but forgot to actually remove the @Provider annotation. |
@v-mihaylov Thank you! Yes, looks like I did, will fix. |
I am struggling with same issue even after updating to jackson-jaxrs-base-2.8.1. My ExceptioMapper is just never invoked.
|
@ghulamemustafa Make sure that version of actual provider (like Other than that perhaps this is then an issue with container not finding the exception mapper: Jackson JAX-RS module can not register your exception mappers so you need to make that happen. |
@cowtowncoder (All jars were already updated to latest releases) I got my issue solved in following way. here is the code.
|
@ghulamemustafa Thank you for sharing your solution! Not sure why default priority would be set so high that custom ones would not be used, but it does sound like this is what is happening. |
@cowtowncoder Your solution works. I think its a hack but I can live with it for now :) Thanks for sharing it with community. |
não deixava fazer EsxceptionMapper para as Exceptions JsonParser e JsonMapper - FasterXML/jackson-jaxrs-providers#22
With @priority(4000) also it is not working. sometimes it is picking one over the other. |
Weirdly enough, even though this was supposedly fixed in 2.8 we are on 2.8.5 and there is still some stray mapper being defined somewhere and gets picked up by Jersey before our own mapper. @priority fix seems to help but I am not yet sure it's a full fix. I'll try to find where the offending mapper comes from. |
As mentioned in this Stackoverflow thread the JackonFeature within jersey-media-json-jackson artifact registeres exception mappers for JsonParseException and JsonMappingException. Since the JacksonFeature cannot be changed, as long as you need to use this feature and the feature registers those exception mappers directly within its code, the only workaround is to register own exception mappers for both exceptions using a lower priority. @Priority(1) // override Jackson JsonMappingExceptionMapper
public class JsonMappingExceptionMapper extends AbstractExceptionMapper<JsonMappingException> {
//...
} |
I was also using Jersey 2.26 and jersey-media-json-jackson as Jersey's user guide recommends (https://jersey.github.io/documentation/latest/media.html#json.jackson). I was encountering this bug and no previously mentioned workarounds worked for me. I ended up switching jersey-media-json-jackson for Jackson's own jackson-jaxrs-json-provider. This way no default ExceptionMappers are included and the problem is solved. Keep in mind that Jackson's own JSON provider must be registered manually (it won't be discovered by Jersey's metainf services lookup unlike Jersey's JacksonJaxbJsonProvider): |
Have the same issue, but would like to leave @Provider as I need Applications auto scan ( do not want to register manually all the classes). And Priority does not work at all. |
Currently jackson's json provider package (com.fasterxml.jackson.jaxrs.json) includes ExceptionMappers for JsonMappingException and JsonParseException.
This may cause unpredictable behaviour if the application also has an ExceptionMapper for either JsonMappingException or JsonParseException and includes in the registration the com.fasterxml.jackson.jaxrs.json package (where the Json and Jaxb providers are located). The result is that depending on which class gets registered first it will be the one used by jersey. From what I can gather the order by which they get picked up is random, sometimes the app's mappers get used, other times it's the jackson ones.
I understand the reasoning behind the inclusion of these mappers, but I think their registration as provider should at least be configurable (maybe as a feature?). It could also be a good idea to put them in another package to avoid people registering the com.fasterxml.jackson.jaxrs.json package and "accidentally" also registering the mappers.
The text was updated successfully, but these errors were encountered: