There are several ways to achieve this. The easiest way is to use @JsonTypeInfo , as Harry said. All you have to do is annotate your abstract base class with @JsonTypeInfo(use = Id.CLASS, include = As.PROPERTY) and you should be good to go.
Another way is to make sure that all mappers on both the server side and the client side (probably NON_FINAL) default typing are turned on so the type information is (de) serialized on uninstalled classes. Both methods can be improved by providing your own TypeResolverBuilder if you are unsatisfied with the serialization capabilities. More detailed explanations of these approaches can be found in this article or on Working with wiki pages with a Jackson Polymorph manipulator .
Although the @JsonTypeInfo method @JsonTypeInfo simple, getting an actual CXF server / client can be a daunting task. So here is a complete example:
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; import com.fasterxml.jackson.jaxrs.xml.JacksonJaxbXMLProvider; import org.apache.cxf.endpoint.Server; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import java.util.Arrays; import java.util.Collections; import java.util.Random; import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static javax.ws.rs.core.MediaType.APPLICATION_XML; public class FullCxfJaxrsJacksonExample { public static void main(String[] args) { String serverAddress = "http://localhost:9000/"; Server server = null; try {
This example has been successfully tested using JDK 1.8.0_45 and the following dependencies:
<dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-xml-provider</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.1.1</version> </dependency>
source share