我正在使用CXF WebClient,我尝试做一个webclient服务并使用它进行调用,我在标头中设置了JSON类型,但是在标头中获得了通配符
我这样做是为了使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());
而且我有这个可以拨打电话
Response reponse=clientThreadSafe().path("tokens/{id}",virtualToken.getId()).get();
return genericReponse(Token.class,Status.OK,reponse);
使用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);
}
和genericResponse
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);
}
但是我在通话中得到了通配符
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=[*/*]}
我得到一个例外
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
我不知道为什么WebClient不采用MediaType.APPLICATION_JSON标头,也许我没有使用正确的函数来设置标头。
如果我尝试使用其他休息客户端,例如邮递员,并且我设置了正确的标题,那么一切似乎都可以正常工作。
经过大量测试,我发现“ Fluent接口”实际上并没有按应有的方式工作,顺序似乎很重要,如果在开始时设置了WebClient的accept和类型,则可以将其重置。
因此,对于每次调用,我都必须accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
在path方法之后进行,例如:
path("tokens/{id}",token.getId())
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.invoke("GET", "")
如果我更改订单,则接受和类型不会计算在内
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句