Java - Generic Type as parameter of jax rs client's readEntity method

0

I want to abstract a post method from my code, using generic type to facilitate implementation. The code is a client of ws using jax-rs. Follow

public T2 post(T1 requisicao, String path){
    javax.ws.rs.client.Client client = ClientBuilder.newClient();
    WebTarget webTarget = client.target(Cliente.HOST).path(path);
    Invocation.Builder invocationBuilder = webTarget.request();
    Response response = invocationBuilder
            .header("chave",Cliente.CHAVE)
            .header("Content-type",Cliente.CONTENT_TYPE)
            .header("Accept",Cliente.ACCEPT)
            .post(Entity.json(requisicao));
    T2 resposta = null;
    resposta = response.readEntity((Class<T2>) resposta.getClass());
    return resposta;
}

And here's how to use the method:

public RespostaCadastroUsuarioSistema cadastraUsuarioSistema(RequisicaoCadastrarUsuarioSistema modeloRequisicao){
    Cliente<RequisicaoCadastrarUsuarioSistema,RespostaCadastroUsuarioSistema> cliente = new Cliente<>();
    return cliente.post(modeloRequisicao, "caminho/para/o/metodo");
}

For obvious reasons, the code above the NullPointException. The problem is that I could not think of a way to make this work, since I can not instantiate a class of type T2 because it is generic and the readEntity method of javax.ws.rs core. Response does not accept generic type as parameter.

Do you know if it's possible? Or am I going to have to work with interface and do cast method return?

    
asked by anonymous 25.04.2018 / 21:05

0 answers