Describe the bug
I'm trying to use JSON-B 2 (from Jakarta EE 9) and Yasson 2 in an application that will be deployed on IBM WebSphere Application Server 9, which is a Java EE 7 environment. I know this is unusual, but it mostly works except for the below issue.
Yasson tries to use JNDI to lookup java:comp/BeanManager
and use it if it exists. The first problem is that jakarta.enterprise.inject.spi.BeanManager
is not in the classpath, so we get a NoClassDefFoundError:
Caused by: java.lang.NoClassDefFoundError: jakarta/enterprise/inject/spi/BeanManager
at org.eclipse.yasson.internal.components.BeanManagerInstanceCreator.<init>(BeanManagerInstanceCreator.java:49)
at org.eclipse.yasson.internal.components.JsonbComponentInstanceCreatorFactory.getComponentInstanceCreator(JsonbComponentInstanceCreatorFactory.java:69)
at org.eclipse.yasson.internal.JsonbContext.initComponentInstanceCreator(JsonbContext.java:148)
at org.eclipse.yasson.internal.JsonbContext.<init>(JsonbContext.java:64)
at org.eclipse.yasson.internal.JsonBinding.<init>(JsonBinding.java:49)
at org.eclipse.yasson.internal.JsonBindingBuilder.build(JsonBindingBuilder.java:61)
at jakarta.json.bind.JsonbBuilder.create(JsonbBuilder.java:86)
at com.example.TestBean.<init>(TestBean.java:11)
After adding jakarta.enterprise:jakarta.enterprise.cdi-api
jars, we get a more understandable error:
Caused by: jakarta.json.bind.JsonbException: Internal error: beanManager instance should be of type 'interface jakarta.enterprise.inject.spi.BeanManager'
at org.eclipse.yasson.internal.components.BeanManagerInstanceCreator.<init>(BeanManagerInstanceCreator.java:50)
at org.eclipse.yasson.internal.components.JsonbComponentInstanceCreatorFactory.getComponentInstanceCreator(JsonbComponentInstanceCreatorFactory.java:69)
at org.eclipse.yasson.internal.JsonbContext.initComponentInstanceCreator(JsonbContext.java:148)
at org.eclipse.yasson.internal.JsonbContext.<init>(JsonbContext.java:64)
at org.eclipse.yasson.internal.JsonBinding.<init>(JsonBinding.java:49)
at org.eclipse.yasson.internal.JsonBindingBuilder.build(JsonBindingBuilder.java:61)
at jakarta.json.bind.JsonbBuilder.create(JsonbBuilder.java:86)
at com.example.TestBean.<init>(TestBean.java:11)
To Reproduce
Here's a simple maven project that reproduces the issue: test-servlet.zip
Expected behavior
To fix this, I suggest changing JsonbComponentInstanceCreatorFactory
as follows:
- If
jakarta.enterprise.inject.spi.BeanManager
is not in the classpath, then it should skip trying to get a BeanManager from CDI or JNDI and jump directly to using DefaultConstructorCreator
.
- If
jakarta.enterprise.inject.spi.BeanManager
is in the classpath but getJndiBeanManager
somehow returned a javax.enterprise.inject.spi.BeanManager
instead (note the javax), then just ignore it and use DefaultConstructorCreator
.
System information:
- OS: Linux
- Java Version: 8
- Yasson Version: 2.0.4
Additional context
I was also able to reproduce this error using Open Liberty server with the javaee-7.0
feature enabled.
bug