fix(OSGI) Rest plugin fails when redeployed from OSGI Refs: #31348 #32274
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixed IllegalStateException: object is not an instance of declaring class that occurs when OSGi bundles are unloaded and reloaded in Jersey REST endpoints.
Root Cause: Jersey's default ResourceMethodInvocationHandlerFactory uses a static DEFAULT_HANDLER that captures method references from the initial ClassLoader. When OSGi bundles are reloaded:
Method objects retain references to the original ClassLoader (A)
Target instances are created with the new ClassLoader (B)
method.invoke(target, args) fails because target is not an instance of the method's declaring class from different ClassLoader
Solution
Implemented a proactive ClassLoader resolution strategy by replacing Jersey's default InvocationHandler:
Custom ResourceMethodInvocationHandlerProvider (DotResourceMethodInvocationHandlerProvider)
Registered with @priority(1) to override Jersey's default handler
Implements proactive ClassLoader conflict detection and resolution
Proactive ClassLoader Resolution
BEFORE method invocation, checks if target.getClass().getClassLoader() differs from method.getDeclaringClass().getClassLoader()
If different, resolves the correct method from target's ClassLoader using target.getClass().getMethod()
GUARANTEES method and target share the same ClassLoader before invocation
Enhanced Error Handling
Downgrades original Jersey exception logging from SEVERE to WARN level
Removes verbose stack traces to reduce log noise
Provides clear diagnostic messages for ClassLoader conflicts
Testing
Verified fix resolves the original IllegalStateException during OSGi bundle reload cycles without impacting normal REST endpoint functionality.
This PR fixes: #31348