eclipse-ee4j / eclipse-ee4j/jersey
jersey-client 2.0: response.readEntity leads to java.lang.ClassCastException in case of mediaType = application/xml
- Dominant language
- Java
- Stars
- 730
- Forks
- 382
- PR merge metrics
- No merged PRs in 30d
Description
We are using the jersey-client 2.0:
....
MyExt2Response myExt2Response = response.readEntity(MyExt2Response.class);
...
We are using 3 jaxb classes, the first class is service specific, the second and third class are common part.
We always want to return a json or xml structure beginning with "response":
1) MyExt2Response
@XmlRootElement(name = "response")
public class MyExt2Response extends MyExt1Response
{ .... }
2) MyExt1Response
@XmlRootElement(name = "collectionReturn")
public class MyExt1Response extends MyResponse
{ ... }
3) MyResponse
@XmlRootElement(name = "response")
public class MyResponse
{ .. }
This runs fine in case of mediaType="application/json". We are using moxy as JsonProvider.
in case of mediaType = "application/xml" we are getting:
java.lang.ClassCastException: common.lib.MyResponse cannot be cast to task.lib.MyExt2Response
at test.Test.main(TaskTest.java:201)
no further stacktrace available
The error seems to occur in: org.glassfish.jersey.message.internal.MessageBodyFactory
or org.glassfish.jersey.message.internal.ReaderInterceptorExecutor:
the call: final Object instance = executor.proceed(); returns a MyResponse instance instead of returning MyExt2Response instance.
In case of mediaType="application/json" the correct jaxb class is returned: MyExt2Response
The readEntity call is also OK if we change the @XmlRootElement(name = "response") from MyResponse to a name different from "response".
But we can't change the existing interface and need a solution that works with same @XmlRootElement names.
#### Environment
jersey version: 2.22.2
java 7 oracle jdk1.7.0_76
java 8 oracle jdk1.8.0_92
#### Affected Versions
[2.22.2]
Contributor guide
Assessment
This issue has not been assessed yet.