javax.ws.rs.client.AsyncInvoker.get()因通用回调实现而失败

时间:2018-09-10 13:53:27

标签: java resteasy

尝试使用.get()发出异步javax.ws.rs.client.Client请求,如下所示:

client.target("http://localhost:8080")
        .path("/sample")
        .request()
        .async()
        .get(new GenericCallback<String>());

GenericCallback如下

public final class GenericCallback<T> implements InvocationCallback<T>{
    @Override
    public void completed(T t) {
    }

    @Override
    public void failed(Throwable throwable) {
    }
}

并获得此异常

Exception in thread "main" java.lang.IllegalArgumentException: Type must not be null
    at javax.ws.rs.core.GenericType.<init>(GenericType.java:156)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.submit(ClientInvocation.java:530)
    at org.jboss.resteasy.client.jaxrs.internal.AsynchronousInvoke.get(AsynchronousInvoke.java:52)

类似org.jboss.resteasy.util.Types#getActualTypeArgumentsOfAnInterface的InvocationCallback无法获得正确的通用类型。

编写此示例进行检查

import org.jboss.resteasy.util.Types;

import javax.ws.rs.client.InvocationCallback;
import java.lang.reflect.Type;
import java.util.Arrays;

public class GetTypeMain {
    public static void main(String[] args) {
        printTypesForCallback(new InvocationCallback<String>() {
            @Override
            public void completed(String result) {
            }

            @Override
            public void failed(Throwable throwable) {
            }
        });

        printTypesForCallback(new TypedCallback());
        printTypesForCallback(new GenericCallback<String>());
    }

    private static void printTypesForCallback(InvocationCallback<?> callback) {
        final Type[] types = Types.getActualTypeArgumentsOfAnInterface(callback.getClass(), InvocationCallback.class);
        System.out.println(Arrays.toString(types));
    }

    private static final class TypedCallback implements InvocationCallback<String> {
        @Override
        public void completed(String result) {
        }

        @Override
        public void failed(Throwable throwable) {
        }
    }

    private static final class GenericCallback<T> implements InvocationCallback<T> {
        @Override
        public void completed(T result) {
        }

        @Override
        public void failed(Throwable throwable) {
        }
    }
}

输出:

[class java.lang.String]
[class java.lang.String]
[null]

符合预期。

是否有任何变通办法来使用实现InvocationCallback的通用类?

UPD 。我使用javax.ws.rs-apiresteasy-jaxrs

的最新版本
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>3.6.1.Final</version>
</dependency>

UPD 2 。将GenericCallback更新为界面

public interface GenericCallback<T> extends InvocationCallback<T> {
    @Override
    default void completed(T result) {
    }

    @Override
    default void failed(Throwable throwable) {
    }
}

这可行

printTypesForCallback(new GenericCallback<String>() {});

打印[class java.lang.String]

1 个答案:

答案 0 :(得分:0)

您是否尝试将通用回调定义为 date output price price2 0 20130101 39.5 25.0 23 1 20130101 39.5 25.0 56 2 20130105 45.0 23.5 45 3 20130105 67.0 27.0 67 4 20130101 33.0 40.0 33 5 20130108 2.0 8.0 2