Last active
September 23, 2016 18:31
-
-
Save asarkar/c3ab6269ef92c8f0323ca2a1bf8b8bda to your computer and use it in GitHub Desktop.
Feign client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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