Karaf 上的应用程序 REST 客户端

Application REST Client on Karaf

我正在写一个简单的 .在 Karaf 4.1.0 上部署的应用程序。它的作用是向 REST API 发送休息请求。当我启动我的包时出现错误:

javax.ws.rs.ProcessingException: org.apache.cxf.interceptor.Fault: No message body writer has been found for class package.QueueSharedDTO, ContentType: application/json
    at org.apache.cxf.jaxrs.client.WebClient.doResponse(WebClient.java:1149)
    at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1094)
    at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:894)
    at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:865)
    at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:428)
    at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.method(WebClient.java:1631)
    at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.method(WebClient.java:1626)
    at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.post(WebClient.java:1566)
    at org.apache.cxf.jaxrs.client.spec.InvocationBuilderImpl.post(InvocationBuilderImpl.java:145)
    at package.worker.service.implementation.ConnectionServiceImpl.postCheckRequest(ConnectionServiceImpl.java:114)
    at package.worker.service.implementation.ConnectionServiceImpl.sendCheck(ConnectionServiceImpl.java:103)
    at package.worker.module.QueueSharedListener.run(QueueSharedListener.java:37)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.cxf.interceptor.Fault: No message body writer has been found for class package.QueueSharedDTO, ContentType: application/json
    at org.apache.cxf.jaxrs.client.WebClient$BodyWriter.doWriteBody(WebClient.java:1222)
    at org.apache.cxf.jaxrs.client.AbstractClient$AbstractBodyWriter.handleMessage(AbstractClient.java:1091)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    at org.apache.cxf.jaxrs.client.AbstractClient.doRunInterceptorChain(AbstractClient.java:649)
    at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1093)
    ... 11 more
Caused by: javax.ws.rs.ProcessingException: No message body writer has been found for class com.emot.dto.QueueSharedDTO, ContentType: application/json
    at org.apache.cxf.jaxrs.client.AbstractClient.reportMessageHandlerProblem(AbstractClient.java:780)
    at org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:494)
    at org.apache.cxf.jaxrs.client.WebClient$BodyWriter.doWriteBody(WebClient.java:1217)
    ... 15 more

初始化 WebTarget:

private ConnectionServiceImpl() {
    client = ClientBuilder.newClient();
    client.property(
            ClientProperties.CONNECT_TIMEOUT,
            snifferProperties.getProperty(SnifferProperties.PARAM_REST_API_CONNECTION_TIMEOUT));
    client.property(
            ClientProperties.READ_TIMEOUT,
            snifferProperties.getProperty(SnifferProperties.PARAM_REST_API_READ_TIMEOUT));
    System.out.println(2);
    webTarget = client.target(buildUrl());
}

发送请求:

private synchronized boolean postCheckRequest(String path, Object content) {
    boolean result = true;
    try {
        Response response = webTarget
                .path("check")
                .path("add/one")
                .request(MediaType.APPLICATION_JSON)
                .post(Entity.json(content));
        result = (response.getStatus() == 200);
    } catch (Exception e) {
        System.out.println("Error but working");
        e.printStackTrace();
        result = false;
    }
    return result;
}

我对 Karaf 总是有问题...我不明白为什么。无法正常工作...

您面临的问题主要不是 Karaf 问题,而是您在非 JavaEE 环境中使用某些 JAX-RS 实现时可能遇到的典型问题。

Exception 的字面意思是您的实现缺少消息正文编写器。消息正文编写器是实现 class javax.ws.rs.ext.MessageBodyWriter 的 class,负责将数据对象序列化为某种格式(如 JSON)。还有另一个名为 javax.ws.rs.ext.MessageBodyReader 的 class,它做相反的事情。所有这些 classes 都作为提供者注册到 JAX-RS 框架,扩展了它的功能。详情在这里:https://jersey.java.net/documentation/latest/message-body-workers.html

因此,通常您必须在数据对象和 HTTP MediaType 之间决定 serializing/deserializing 使用什么,并注册适当的 JAX-RS 提供程序。

例如,对于 Jackson,您的问题可以通过使用其标准实现之一轻松解决:com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider,如果您使用 JAXB 注释,或者 com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider,如果您更喜欢 Jackson 注释.在您的蓝图描述符的提供者部分添加此 class:

<jaxrs:server id="restServer" address="/rest">
    <jaxrs:serviceBeans>
            ....
    </jaxrs:serviceBeans>
    <jaxrs:providers>
            ....
        <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"/>
            ....
    </jaxrs:providers>
</jaxrs:server>