Skip to content

Instantly share code, notes, and snippets.

@asarkar
Last active September 23, 2016 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asarkar/c3ab6269ef92c8f0323ca2a1bf8b8bda to your computer and use it in GitHub Desktop.
Save asarkar/c3ab6269ef92c8f0323ca2a1bf8b8bda to your computer and use it in GitHub Desktop.
Feign client
@SpringCloudApplication
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@FeignClient(name = "httpbin", url = "httpbin.org")
public interface HttpBinClient {
@RequestMapping(path = "/post", method = POST, produces = APPLICATION_JSON_VALUE)
public String echo(@HeaderMap MultiValueMap<String, String> headers);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTest {
@Autowired
private DemoApplication.HttpBinClient client;
private final ObjectReader objectReader = new ObjectMapper()
.enable(ACCEPT_SINGLE_VALUE_AS_ARRAY)
.readerFor(Request.class);
@Test
public void testEcho() throws IOException {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.put(ACCEPT, singletonList(APPLICATION_JSON_VALUE));
String lang = ENGLISH.toLanguageTag();
headers.put(ACCEPT_LANGUAGE, singletonList(lang));
String response = client.echo(headers);
System.out.println(response);
Request request = objectReader.readValue(response);
assertThat(request.getUrl(), is("http://httpbin.org/post"));
MultiValueMap<String, String> requestHeaders = request.getHeaders();
assertThat(requestHeaders.keySet(), hasSize(greaterThanOrEqualTo(2)));
assertThat(requestHeaders, hasEntry(is(ACCEPT), containsInAnyOrder(APPLICATION_JSON_VALUE)));
assertThat(requestHeaders, hasEntry(is(ACCEPT_LANGUAGE), containsInAnyOrder(lang)));
}
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Request {
Map<String, String> args;
@JsonDeserialize(as = LinkedMultiValueMap.class)
MultiValueMap<String, String> headers;
String url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment