Websphere Liberty JAXRS - ошибка (?) С абстрактными супертипами, REST и JSON - PullRequest
0 голосов
/ 20 сентября 2019

Я нахожусь в процессе миграции большого приложения в WebSphere Liberty и обнаружил проблему с - вероятно, -JAXRS:

У меня есть следующий минимальный нерабочий пример:

Мой тестовый класспопытка сохранить Кошку, опубликованную как JSON:

public class MyTest extends TestBase {
//TestBase is responsible for generating the .war

    @Test
    public void test() throws Exception {
        expect().
                statusCode(204).request().body(
                "{" +
                        "\"name\": \"Furry\"," +
                        "\"lives\": \"5\"" +
                        "}"
        ).
                given().contentType(FallApplication.JSON_MEDIATYPE).
                when().post("/test/mypath/send");
    }
}

Вот мои два DTO:

public abstract class AnimalDTO {

    private String name;

    //getter and setter
}

public class CatDTO extends AnimalDTO {
    private int lives;

    //getter and setter
}

Мой Диспетчер:

@Path("/mypath")
public class TestDispatcher extends AbstractTestDispatcher<CatDTO> {

    @Override
    @Path("/send")
    @Consumes("application/json;charset=utf-8")
    @POST
    public void save(CatDTO catDTO) {
        System.out.println(catDTO.toString());
    }
}

public abstract class AbstractTestDispatcher<W extends AnimalDTO> {

    public abstract void save(W wDTO);
}

Появляется «ошибка»в моем провайдере:

@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MyProvider implements MessageBodyReader<Object>, MessageBodyWriter<Object> {

    @Override
    public Object readFrom(Class<Object> type, Type genericType,
                           Annotation[] annotations, MediaType mediaType,
                           MultivaluedMap<String, String> multivaluedMap, InputStream entityStream) throws IOException {
        //**here genericType is AnimalDTO instead of CatDTO**
    }

//other irrelevant methods

}

Я могу сделать MyTest правильным, если TestDispatcher не расширяет AbstractTestDispatcher.Это ошибка в Websphere Liberty или я что-то не так настраиваю?

Stacktrace:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `*.AnimalDTO` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (org.apache.cxf.transport.http.AbstractHTTPDestination$1); line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) ~[jackson-databind-2.10.0.pr2.jar:2.10.0.pr2]
    at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1473) ~[jackson-databind-2.10.0.pr2.jar:2.10.0.pr2]
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1029) ~[jackson-databind-2.10.0.pr2.jar:2.10.0.pr2]
    at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:265) ~[jackson-databind-2.10.0.pr2.jar:2.10.0.pr2]
    at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:4167) ~[jackson-databind-2.10.0.pr2.jar:2.10.0.pr2]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2530) ~[jackson-databind-2.10.0.pr2.jar:2.10.0.pr2]
    at *.MyProvider.JsonResultatSteuerung.readFrom(JsonResultatSteuerung.java:64) ~[classes/:?]
    at org.apache.cxf.jaxrs.utils.JAXRSUtils$2.run(JAXRSUtils.java:1430) ~[com.ibm.ws.org.apache.cxf.cxf.rt.frontend.jaxrs.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_66]
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBodyReader(JAXRSUtils.java:1427) ~[com.ibm.ws.org.apache.cxf.cxf.rt.frontend.jaxrs.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1382) ~[com.ibm.ws.org.apache.cxf.cxf.rt.frontend.jaxrs.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processRequestBodyParameter(JAXRSUtils.java:897) ~[com.ibm.ws.org.apache.cxf.cxf.rt.frontend.jaxrs.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:838) [com.ibm.ws.org.apache.cxf.cxf.rt.frontend.jaxrs.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:278) [com.ibm.ws.org.apache.cxf.cxf.rt.frontend.jaxrs.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:89) [com.ibm.ws.org.apache.cxf.cxf.rt.frontend.jaxrs.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) [com.ibm.ws.org.apache.cxf.cxf.core.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:124) [com.ibm.ws.org.apache.cxf.cxf.core.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:275) [com.ibm.ws.org.apache.cxf.cxf.rt.transports.http.3.2_1.0.30.jar:3.3.0]
    at com.ibm.ws.jaxrs20.endpoint.AbstractJaxRsWebEndpoint.invoke(AbstractJaxRsWebEndpoint.java:134) [com.ibm.ws.org.apache.cxf.cxf.rt.frontend.jaxrs.3.2_1.0.30.jar:3.3.3.SNAPSHOT]
    at com.ibm.websphere.jaxrs.server.IBMRestServlet.handleRequest(IBMRestServlet.java:146) [com.ibm.ws.jaxrs.2.0.server_1.0.30.jar:?]
    at com.ibm.websphere.jaxrs.server.IBMRestServlet.doPost(IBMRestServlet.java:104) [com.ibm.ws.jaxrs.2.0.server_1.0.30.jar:?]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:706) [com.ibm.websphere.javaee.servlet.4.0_1.0.30.jar:?]
    at com.ibm.websphere.jaxrs.server.IBMRestServlet.service(IBMRestServlet.java:96) [com.ibm.ws.jaxrs.2.0.server_1.0.30.jar:?]
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1230) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:729) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:426) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:182) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:93) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71) [log4j-web-2.11.0.jar:2.11.0]
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:201) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:90) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.security.jaspi.JaspiServletFilter.doFilter(JaspiServletFilter.java:56) [com.ibm.ws.security.jaspic.1.1_1.0.30.jar:?]
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:201) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:90) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:993) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1131) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:4993) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2.handleRequest(DynamicVirtualHost.java:314) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1007) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2.run(DynamicVirtualHost.java:279) [com.ibm.ws.webcontainer_1.1.30.jar:?]
    at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink$TaskWrapper.run(HttpDispatcherLink.java:1136) [com.ibm.ws.transport.http_1.0.30.jar:?]
    at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.wrapHandlerAndExecute(HttpDispatcherLink.java:417) [com.ibm.ws.transport.http_1.0.30.jar:?]
    at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink.ready(HttpDispatcherLink.java:376) [com.ibm.ws.transport.http_1.0.30.jar:?]
    at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:532) [com.ibm.ws.transport.http_1.0.30.jar:?]
    at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.handleNewRequest(HttpInboundLink.java:466) [com.ibm.ws.transport.http_1.0.30.jar:?]
    at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.processRequest(HttpInboundLink.java:331) [com.ibm.ws.transport.http_1.0.30.jar:?]
    at com.ibm.ws.http.channel.internal.inbound.HttpInboundLink.ready(HttpInboundLink.java:302) [com.ibm.ws.transport.http_1.0.30.jar:?]
    at com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:165) [com.ibm.ws.channelfw_1.0.30.jar:?]
    at com.ibm.ws.tcpchannel.internal.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:74) [com.ibm.ws.channelfw_1.0.30.jar:?]
    at com.ibm.ws.tcpchannel.internal.WorkQueueManager.requestComplete(WorkQueueManager.java:503) [com.ibm.ws.channelfw_1.0.30.jar:?]
    at com.ibm.ws.tcpchannel.internal.WorkQueueManager.attemptIO(WorkQueueManager.java:573) [com.ibm.ws.channelfw_1.0.30.jar:?]
    at com.ibm.ws.tcpchannel.internal.WorkQueueManager.workerRun(WorkQueueManager.java:954) [com.ibm.ws.channelfw_1.0.30.jar:?]
    at com.ibm.ws.tcpchannel.internal.WorkQueueManager$Worker.run(WorkQueueManager.java:1043) [com.ibm.ws.channelfw_1.0.30.jar:?]
    at com.ibm.ws.threading.internal.ExecutorServiceImpl$RunnableWrapp
er.run(ExecutorServiceImpl.java:239) [com.ibm.ws.threading_1.1.30.jar:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_66]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_66]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
...