简体   繁体   中英

Force feign to use a specific content-type

I'm using Feign to do REST calls on a remote Java interface. Interface is defined as follow:

@Produces({"text/xml", "application/json"})

Since JAXRSContract simple sends data.template().header("Accept", serverProduces); server choose to answer with xml payload.

Is there any way I can force Feign to ask for `JSON payload in this situation?

You can override headers using interceptors:

Feign.builder().setInterceptor(requestTemplate -> {
        Map<String, Collection<String>> map = new HashMap<>(requestTemplate.headers());
        map.put("Accept", Collections.singleton("application/json"));
        requestTemplate.headers(null);
        requestTemplate.headers(map);
});

The method requestTemplate.headers(map) looks like poorly designed, and if you pass a valid map, its values are added to the inner header map; if you pass null instead the inner header map is reset. That's why you need to call it twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM