javajakarta-eecxfcxf-clientjava-ws

Bad header in CXF


I'm working with CXF WebClient, i tried to do a webclient service and make the call with it, i set JSON type in header, but i getting the wild card in the header

I did this for make the webClient

client = WebClient.create(endPoint,Collections.singletonList(new JacksonJsonProvider())).
            accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON);

    ClientConfiguration config = WebClient.getConfig(client);
    config.getInInterceptors().add(new LoggingInInterceptor());
    config.getOutInterceptors().add(new LoggingOutInterceptor());

And i have this to make the get call

Response reponse=clientThreadSafe().path("tokens/{id}",virtualToken.getId()).get();

return genericReponse(Token.class,Status.OK,reponse);

With clientThreadSafe

private WebClient clientThreadSafe() throws CertEuropeException{
    //thread safe, see http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-ThreadSafety
    return  WebClient.fromClient(client);
}

And genericReponse

private <T> T genericReponse(Class<T> classReponse, Status status, Response reponse ) throws Exception{

    if(reponse.getStatusInfo()!=status){
        throw new Exception("somthing bad here");
    }
    return  reponse.readEntity(classReponse);

}

But i getting the wildcard in the call

INFOS: Setting the server's publish address to be 
http://localhost:9090 mars 14, 2016 1:52:31 PM
org.apache.cxf.interceptor.LoggingOutInterceptor INFOS: Outbound
Message
--------------------------- ID: 1 Address: http://localhost:9090/api/v1/tokens/1 Http-Method: GET
Content-Type:  Headers: {Accept=[*/*]}

And i getting one exception

GRAVE: No message body reader has been found for class com.client.Token, ContentType: application/octet-stream
mars 14, 2016 1:52:31 PM
org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
AVERTISSEMENT: javax.ws.rs.WebApplicationException: HTTP 415 Unsupported Media Type

I don't know why the WebClient is not taking the MediaType.APPLICATION_JSON header, maybe i don't use the right function for set the headers.

If i try with other rest client, like post man, and i set the right header all seem work fine.


Solution

  • After a lots of test, i found that the "Fluent interface" is not really working like it should, it seem that the order is important, and if you set the accept and the type of the WebClient at the beginning, this can be reset.

    So for every call i have to made accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON) after the path method, like:

    path("tokens/{id}",token.getId())
    .accept(MediaType.APPLICATION_JSON)
    .type(MediaType.APPLICATION_JSON)
    .invoke("GET", "")
    

    If i change the order, the accept and type will not take in count