├── .gitignore ├── src └── main │ ├── resources │ ├── application.properties │ └── templates │ │ └── index.html │ └── java │ └── example │ ├── Contributor.java │ ├── GitHubClient.java │ └── App.java ├── README.md └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | bin 4 | *.iml -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=10080 -------------------------------------------------------------------------------- /src/main/java/example/Contributor.java: -------------------------------------------------------------------------------- 1 | package example; 2 | 3 | public class Contributor { 4 | 5 | private String login; 6 | 7 | private int contributions; 8 | 9 | public String getLogin() { 10 | return login; 11 | } 12 | 13 | public void setLogin(String login) { 14 | this.login = login; 15 | } 16 | 17 | public int getContributions() { 18 | return contributions; 19 | } 20 | 21 | public void setContributions(int contributions) { 22 | this.contributions = contributions; 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /src/main/java/example/GitHubClient.java: -------------------------------------------------------------------------------- 1 | package example; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.cloud.netflix.feign.FeignClient; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | import org.springframework.web.bind.annotation.RequestParam; 9 | 10 | @FeignClient(url = "https://api.github.com") 11 | interface GitHubClient { 12 | 13 | //e.g. http://localhost:10080/andrefaria/spring-cloud-feign-example 14 | 15 | @RequestMapping(method = RequestMethod.GET, value = "/repos/{owner}/{repo}/contributors") 16 | List contributors(@RequestParam("owner") String owner, @RequestParam("repo") String repo); 17 | 18 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is an example of how to use AngularJS + Feign + Spring Cloud. 2 | 3 | Main Point: It has never been easier to do REST in Java. 4 | 5 | This application goes to https://api.github.com and lists contributors of a repository. 6 | 7 | Feign abstracts the Rest Client and Unmarshalling of JSON objects. 8 | 9 | You can run it like a spring boot app: 10 | mvn spring-boot:run 11 | 12 | You can see it working in your browser or curl: 13 | http://localhost:8080 14 | 15 | Ex: 16 | http://localhost:8080/netflix/hystrix - lists contributors of Hystrix. 17 | http://localhost:8080/jquery/jquery- lists contributors of jQuery. 18 | 19 | Every time you make a request the App goes to the REST github API and gets back with the information populated in a list of Pojos (Contributor.java). 20 | 21 | Isn't that really Awesome? Enjoy! 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/example/App.java: -------------------------------------------------------------------------------- 1 | package example; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.context.web.SpringBootServletInitializer; 7 | import org.springframework.cloud.netflix.feign.EnableFeignClients; 8 | import org.springframework.stereotype.Controller; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.ResponseBody; 12 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 13 | 14 | import java.util.List; 15 | 16 | @SpringBootApplication 17 | @EnableWebMvc 18 | @Controller 19 | @EnableFeignClients 20 | public class App extends SpringBootServletInitializer { 21 | 22 | 23 | @Autowired 24 | private GitHubClient gitHub; 25 | 26 | @RequestMapping("/") 27 | public String home() { 28 | return "index"; 29 | } 30 | 31 | @RequestMapping("/{owner}/{repo}") 32 | @ResponseBody 33 | public List contributors(@PathVariable String owner, @PathVariable String repo) { 34 | return gitHub.contributors(owner, repo); 35 | } 36 | 37 | public static void main(String[] args) { 38 | SpringApplication.run(App.class, args); 39 | } 40 | 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Cloud + Feign Example 5 | 6 | 21 | 22 | 23 | 24 | 25 | 26 |

List Git Hub Repository Contributors

27 | 28 |
29 | 30 | Owner: 31 | Repo: 32 |
33 |
34 | 35 | 36 |

Contributors of {{repo}}

37 |
38 | {{c.login}}: {{c.contributions}} contributions. 39 |
40 | 41 |
42 | Repository not found 43 |
44 | 45 |
46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.test 8 | spring-cloud-feign-example 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 1.2.0.RELEASE 16 | 17 | 18 | 19 | 20 | 21 | UTF-8 22 | demo.Application 23 | 1.8 24 | 1.0.2.RELEASE 25 | 1.2.3.RELEASE 26 | 6.1.3 27 | 0.6.5 28 | 18.0 29 | 30 | 31 | 32 | 33 | 34 | 35 | spring-snapshots 36 | Spring Snapshots 37 | http://repo.spring.io/libs-snapshot-local 38 | 39 | true 40 | 41 | 42 | 43 | 44 | spring-snapshot 45 | Spring Snapshots 46 | https://repo.spring.io/libs-snapshot 47 | 48 | true 49 | 50 | 51 | 52 | 53 | spring-milestones 54 | Spring Milestones 55 | http://repo.spring.io/libs-milestone-local 56 | 57 | false 58 | 59 | 60 | 61 | spring-releases 62 | Spring Releases 63 | http://repo.spring.io/libs-release-local 64 | 65 | false 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | org.springframework.cloud 74 | spring-cloud-netflix 75 | 1.0.2.RELEASE 76 | pom 77 | 78 | 79 | 80 | org.springframework.boot 81 | spring-boot-starter-web 82 | ${spring-boot.version} 83 | 84 | 85 | 86 | org.springframework.boot 87 | spring-boot-starter-thymeleaf 88 | 89 | 90 | 91 | org.springframework.boot 92 | spring-boot-starter-test 93 | ${spring-boot.version} 94 | test 95 | 96 | 97 | 98 | org.springframework.cloud 99 | spring-cloud-starter 100 | ${spring-cloud.version} 101 | 102 | 103 | 104 | org.springframework.cloud 105 | spring-cloud-netflix-core 106 | ${spring-cloud.version} 107 | 108 | 109 | 110 | org.springframework.cloud 111 | spring-cloud-starter-hystrix 112 | ${spring-cloud.version} 113 | 114 | 115 | 116 | org.springframework.cloud 117 | spring-cloud-starter-feign 118 | ${spring-cloud.version} 119 | 120 | 121 | 122 | org.springframework.cloud 123 | spring-cloud-starter-config 124 | 1.0.1.RELEASE 125 | 126 | 127 | 128 | org.springframework.boot 129 | spring-boot-starter-actuator 130 | 131 | 132 | 133 | com.netflix.archaius 134 | archaius-core 135 | ${archaius.version} 136 | 137 | 138 | 139 | com.google.guava 140 | guava 141 | ${guava.version} 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | org.springframework.boot 150 | spring-boot-maven-plugin 151 | 152 | 153 | pl.project13.maven 154 | git-commit-id-plugin 155 | 156 | 157 | maven-deploy-plugin 158 | 159 | true 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | spring-snapshots 168 | Spring Snapshots 169 | http://repo.spring.io/libs-snapshot-local 170 | 171 | true 172 | 173 | 174 | 175 | spring-milestones 176 | Spring Milestones 177 | http://repo.spring.io/libs-milestone-local 178 | 179 | false 180 | 181 | 182 | 183 | 184 | 185 | --------------------------------------------------------------------------------