From 6a82644153faa8f275b2e21d488402a1f07f9cd0 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Mon, 5 Dec 2016 12:48:01 -0500 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20add=20URL=20protocol=20when=20u?= =?UTF-8?q?rl=20is=20set=20using=20EL.=20=20Fixes=20#1522.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../netflix/feign/FeignClientsRegistrar.java | 2 +- .../feign/FeignHttpClientUrlTests.java | 46 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java index 53c34ef5..0adb2789 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java @@ -243,7 +243,7 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, private String getUrl(Map attributes) { String url = resolve((String) attributes.get("url")); - if (StringUtils.hasText(url)) { + if (StringUtils.hasText(url) && !(url.startsWith("#{") && url.endsWith("}"))) { if (!url.contains("://")) { url = "http://" + url; } diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignHttpClientUrlTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignHttpClientUrlTests.java index 081b7e16..9fd5e0ce 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignHttpClientUrlTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignHttpClientUrlTests.java @@ -30,6 +30,7 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -75,6 +76,11 @@ public class FeignHttpClientUrlTests { @Autowired private UrlClient urlClient; + @Autowired + private BeanUrlClient beanClient; + + @Autowired BeanUrlClientNoProtocol beanClientNoProtocol; + // this tests that @FeignClient(name = "localappurl", url = "http://localhost:${server.port}/") protected interface UrlClient { @@ -82,17 +88,41 @@ public class FeignHttpClientUrlTests { Hello getHello(); } + @FeignClient(name = "beanappurl", url = "#{SERVER_URL}") + protected interface BeanUrlClient { + @RequestMapping(method = RequestMethod.GET, value = "/hello") + Hello getHello(); + } + + @FeignClient(name = "beanappurlnoprotocol", url = "#{SERVER_URL_NO_PROTOCOL}") + protected interface BeanUrlClientNoProtocol { + @RequestMapping(method = RequestMethod.GET, value = "/hello") + Hello getHello(); + } + @Configuration @EnableAutoConfiguration @RestController - @EnableFeignClients(clients = { UrlClient.class }) + @EnableFeignClients(clients = { UrlClient.class, BeanUrlClient.class, BeanUrlClientNoProtocol.class }) protected static class TestConfig { + @Value("${server.port}") + private int port; @RequestMapping(method = RequestMethod.GET, value = "/hello") public Hello getHello() { return new Hello("hello world 1"); } + @Bean(name="SERVER_URL") + public String serverUrl() { + return "http://localhost:" + port + "/"; + } + + @Bean(name="SERVER_URL_NO_PROTOCOL") + public String serverUrlNoProtocol() { + return "localhost:" + port + "/"; + } + @Bean public Targeter feignTargeter() { return new Targeter() { @@ -122,6 +152,20 @@ public class FeignHttpClientUrlTests { assertEquals("first hello didn't match", new Hello("hello world 1"), hello); } + @Test + public void testBeanUrl() { + Hello hello = this.beanClient.getHello(); + assertNotNull("hello was null", hello); + assertEquals("first hello didn't match", new Hello("hello world 1"), hello); + } + + @Test + public void testBeanUrlNoProtocol() { + Hello hello = this.beanClientNoProtocol.getHello(); + assertNotNull("hello was null", hello); + assertEquals("first hello didn't match", new Hello("hello world 1"), hello); + } + @Data @AllArgsConstructor @NoArgsConstructor