I want to get the header value from the reply of http call, store it in cache and use this value in next requests by passing it to header of the http requests in order to achieve sticky session. My project is in Java 8.
I have feign http client builder like this which decode using JacksonDecoder.
public <T> T buildClient(Class<T> type, String url) {
return Feign.builder()
.contract(new JAXRSContract())
.encoder(new JacksonEncoder(mapper))
.decoder(new CustomDecoder(mapper))
.logger(new Slf4jLogger(Constants.APIC_LOGGER_NAME))
.logLevel(Level.FULL)
.options(new Options(connectTimeout, readTimeout))
.retryer(Retryer.NEVER_RETRY)
.requestInterceptor(auth2FeignRequestInterceptor)
.requestInterceptor(requestInterceptor())
.invocationHandlerFactory(factory)
.target(type, url);
}
The default decoder from jackson decode only body not header so I am implementing my own CustomDecoder.
What I want to achieve is get the value of the resonse.headers and map it to body
public final class CustomDecoder extends JacksonDecoder implements Decoder {
@Override
public Object decode(Response response, Type type) throws IOException {
//Here the default decoder only decoding body.
Reader reader = response.body().asReader();
Object responseBodyObject = mapper.readValue(reader, mapper.constructType(type));
Now I want to do the dynamic casting to the Object above so that after I get my class in runtime I am able to assign the header value inside the body through Java Reflection. but Dynamic casting is not working as below.
How to make the below casting work in runtime so that I assign the value to the object using reflection?
Class<?> myclass= Class.forName(type.getTypeName());
myclass.cast(responseBodyObject);
Aucun commentaire:
Enregistrer un commentaire