Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javajax-rs客户端响应实体泛型_Java_Generics_Jax Rs - Fatal编程技术网

javajax-rs客户端响应实体泛型

javajax-rs客户端响应实体泛型,java,generics,jax-rs,Java,Generics,Jax Rs,我对泛型函数有问题。我想使用一个我分配了特定类/类型的函数,首先从rest响应生成相应的结果,然后返回它 public class TimerService { [...] public <T extends myObjInterface> RestMessageContainer<T> send(String endpointPath, Map<String, String> parameters, Class<T> claz

我对泛型函数有问题。我想使用一个我分配了特定类/类型的函数,首先从rest响应生成相应的结果,然后返回它

public class TimerService {

    [...]

    public <T extends myObjInterface> RestMessageContainer<T> send(String endpointPath, Map<String, String> parameters, Class<T> clazz) {
        [...]
        Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get();
        RestMessageContainer<T> container = response.readEntity(new GenericType<RestMessageContainer<T>>() {});
        return container;
    }
}

public class RestMessageContainer<T extends myObjInterface> {

    [...]

    @XmlAttribute(name = "data")
    private List<T> data;

    [...]
}
错误是针对行
RestMessageContainer=response.readEntity(新的GenericType(){})输出的


我的方法正确吗?或者我应该如何解决我的问题?

您不能创建抽象类的实例。但是,您可以通过对抽象类进行简单的注释–
@JsonDeserialize
来解决此问题:

@JsonDeserialize(as=Cat.class)
抽象类动物{…}
在您的例子中,抽象类将是
myObjInterface

注意:如果你有一个以上的抽象类的子类型,那么你应该考虑包括子类型信息,如此所示。

< P>谢谢你的建议, 我有几个子类。JSON字符串中没有关于类型的信息。该类型由请求地址生成。我无法配置Jackson以识别子类型。JSON字符串中没有可以用作类型的唯一字段。 我无法更改传递JSON字符串的web服务


[更新]

我找到了解决办法。我不再让JAX-RS客户机转换JSON字符串。我将JSON字符串作为字符串返回给我,并使用Jackson独立转换它

    public <T extends myObjInterface> RestMessageContainer<T> send(String endpointPath, Map<String, String> parameters, Class<T> clazz) {
        [...]
        Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get();

        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        RestMessageContainer<T> container = mapper.readValue(response.readEntity(String.class), mapper.getTypeFactory().constructParametricType(RestMessageContainer.class, clazz));

        return container;
    }
public RestMessageContainer发送(字符串端点路径、映射参数、类clazz){
[...]
Response Response=webTarget.request(MediaType.APPLICATION_JSON_TYPE).get();
ObjectMapper mapper=新的ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD,Visibility.ANY);
禁用(反序列化功能。在未知属性上失败);
RestMessageContainer=mapper.readValue(response.readEntity(String.class),mapper.getTypeFactory().ConstructParameterType(RestMessageContainer.class,clazz));
返回容器;
}

这是否回答了您的问题?
    public <T extends myObjInterface> RestMessageContainer<T> send(String endpointPath, Map<String, String> parameters, Class<T> clazz) {
        [...]
        Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get();

        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        RestMessageContainer<T> container = mapper.readValue(response.readEntity(String.class), mapper.getTypeFactory().constructParametricType(RestMessageContainer.class, clazz));

        return container;
    }