Skip to content

Commit 34253f3

Browse files
author
Adrian Cole
committed
Makes examples standalone and built from standard Gradle or Maven
1 parent 265d6b4 commit 34253f3

File tree

10 files changed

+226
-89
lines changed

10 files changed

+226
-89
lines changed

example-github/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
GitHub Example
2+
===================
3+
4+
This is an example of a simple json client.
5+
6+
=== Building example with Gradle
7+
Install and run `gradle` to produce `build/wikipedia`
8+
9+
=== Building example with Maven
10+
Install and run `mvn` to produce `target/wikipedia`

example-github/build.gradle

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
plugins {
2-
id 'nebula.provided-base' version '2.0.1'
3-
}
1+
// NOTE: This module is intended to be a stand-alone example which does depend on nebula.
2+
defaultTasks 'clean', 'fatJar'
43

54
apply plugin: 'java'
65

7-
sourceCompatibility = 1.6
6+
repositories {
7+
mavenCentral()
8+
}
9+
10+
configurations {
11+
compile
12+
}
813

914
dependencies {
10-
compile 'com.netflix.feign:feign-core:5.3.0'
11-
compile 'com.netflix.feign:feign-gson:5.3.0'
12-
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
15+
compile 'com.netflix.feign:feign-core:7.1.0'
16+
compile 'com.netflix.feign:feign-gson:7.1.0'
1317
}
1418

1519
// create a self-contained jar that is executable
@@ -49,7 +53,3 @@ task fatJar(dependsOn: classes, type: Jar) {
4953
srcFile.setExecutable(true, true)
5054
}
5155
}
52-
53-
artifacts {
54-
archives fatJar
55-
}

example-github/pom.xml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.sonatype.oss</groupId>
8+
<artifactId>oss-parent</artifactId>
9+
<version>7</version>
10+
</parent>
11+
12+
<groupId>com.netflix.feign</groupId>
13+
<artifactId>feign-example-github</artifactId>
14+
<packaging>jar</packaging>
15+
<version>7.1.0</version>
16+
<name>GitHub Example</name>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.netflix.feign</groupId>
21+
<artifactId>feign-core</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.netflix.feign</groupId>
26+
<artifactId>feign-gson</artifactId>
27+
<version>${project.version}</version>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<defaultGoal>package</defaultGoal>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-shade-plugin</artifactId>
37+
<version>2.3</version>
38+
<executions>
39+
<execution>
40+
<phase>package</phase>
41+
<goals>
42+
<goal>shade</goal>
43+
</goals>
44+
<configuration>
45+
<transformers>
46+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
47+
<mainClass>feign.example.github.GitHubExample</mainClass>
48+
</transformer>
49+
</transformers>
50+
<createDependencyReducedPom>false</createDependencyReducedPom>
51+
</configuration>
52+
</execution>
53+
</executions>
54+
</plugin>
55+
<plugin>
56+
<groupId>org.skife.maven</groupId>
57+
<artifactId>really-executable-jar-maven-plugin</artifactId>
58+
<version>1.3.0</version>
59+
<configuration>
60+
<programFile>github</programFile>
61+
</configuration>
62+
<executions>
63+
<execution>
64+
<phase>package</phase>
65+
<goals>
66+
<goal>really-executable-jar</goal>
67+
</goals>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>

example-github/src/main/java/feign/example/github/GitHubExample.java

+8-20
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@
1515
*/
1616
package feign.example.github;
1717

18-
import dagger.Module;
19-
import dagger.Provides;
2018
import feign.Feign;
2119
import feign.Logger;
20+
import feign.Param;
2221
import feign.RequestLine;
23-
import feign.gson.GsonModule;
22+
import feign.gson.GsonDecoder;
2423
import java.util.List;
25-
import javax.inject.Named;
2624

2725
/** adapted from {@code com.example.retrofit.GitHubClient} */
2826
public class GitHubExample {
2927

3028
interface GitHub {
3129
@RequestLine("GET /repos/{owner}/{repo}/contributors")
32-
List<Contributor> contributors(@Named("owner") String owner, @Named("repo") String repo);
30+
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
3331
}
3432

3533
static class Contributor {
@@ -39,26 +37,16 @@ static class Contributor {
3937

4038
public static void main(String... args) throws InterruptedException {
4139
GitHub github =
42-
Feign.create(GitHub.class, "https://api.github.com", new GsonModule(), new LogToStderr());
40+
Feign.builder()
41+
.decoder(new GsonDecoder())
42+
.logger(new Logger.ErrorLogger())
43+
.logLevel(Logger.Level.BASIC)
44+
.target(GitHub.class, "https://api.github.com");
4345

4446
System.out.println("Let's fetch and print a list of the contributors to this library.");
4547
List<Contributor> contributors = github.contributors("netflix", "feign");
4648
for (Contributor contributor : contributors) {
4749
System.out.println(contributor.login + " (" + contributor.contributions + ")");
4850
}
4951
}
50-
51-
@Module(overrides = true, library = true, includes = GsonModule.class)
52-
static class LogToStderr {
53-
54-
@Provides
55-
Logger.Level loggingLevel() {
56-
return Logger.Level.BASIC;
57-
}
58-
59-
@Provides
60-
Logger logger() {
61-
return new Logger.ErrorLogger();
62-
}
63-
}
6452
}

example-wikipedia/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Wikipedia Example
2+
===================
3+
4+
This is an example of advanced json response parsing, including pagination.
5+
6+
=== Building example with Gradle
7+
Install and run `gradle` to produce `build/wikipedia`
8+
9+
=== Building example with Maven
10+
Install and run `mvn` to produce `target/wikipedia`

example-wikipedia/build.gradle

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
plugins {
2-
id 'nebula.provided-base' version '2.0.1'
3-
}
1+
// NOTE: This module is intended to be a stand-alone example which does depend on nebula.
2+
defaultTasks 'clean', 'fatJar'
43

54
apply plugin: 'java'
65

7-
sourceCompatibility = 1.6
6+
repositories {
7+
mavenCentral()
8+
}
9+
10+
configurations {
11+
compile
12+
}
813

914
dependencies {
10-
compile 'com.netflix.feign:feign-core:5.3.0'
11-
compile 'com.netflix.feign:feign-gson:5.3.0'
12-
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
15+
compile 'com.netflix.feign:feign-core:7.1.0'
16+
compile 'com.netflix.feign:feign-gson:7.1.0'
1317
}
1418

1519
// create a self-contained jar that is executable
@@ -49,7 +53,3 @@ task fatJar(dependsOn: classes, type: Jar) {
4953
srcFile.setExecutable(true, true)
5054
}
5155
}
52-
53-
artifacts {
54-
archives fatJar
55-
}

example-wikipedia/pom.xml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.sonatype.oss</groupId>
8+
<artifactId>oss-parent</artifactId>
9+
<version>7</version>
10+
</parent>
11+
12+
<groupId>com.netflix.feign</groupId>
13+
<artifactId>feign-example-wikipedia</artifactId>
14+
<packaging>jar</packaging>
15+
<version>7.1.0</version>
16+
<name>Wikipedia Example</name>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.netflix.feign</groupId>
21+
<artifactId>feign-core</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.netflix.feign</groupId>
26+
<artifactId>feign-gson</artifactId>
27+
<version>${project.version}</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.google.code.gson</groupId>
31+
<artifactId>gson</artifactId>
32+
<version>2.2.4</version>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<defaultGoal>package</defaultGoal>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-shade-plugin</artifactId>
42+
<version>2.3</version>
43+
<executions>
44+
<execution>
45+
<phase>package</phase>
46+
<goals>
47+
<goal>shade</goal>
48+
</goals>
49+
<configuration>
50+
<transformers>
51+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
52+
<mainClass>feign.example.wikipedia.WikipediaExample</mainClass>
53+
</transformer>
54+
</transformers>
55+
<createDependencyReducedPom>false</createDependencyReducedPom>
56+
</configuration>
57+
</execution>
58+
</executions>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.skife.maven</groupId>
62+
<artifactId>really-executable-jar-maven-plugin</artifactId>
63+
<version>1.3.0</version>
64+
<configuration>
65+
<programFile>wikipedia</programFile>
66+
</configuration>
67+
<executions>
68+
<execution>
69+
<phase>package</phase>
70+
<goals>
71+
<goal>really-executable-jar</goal>
72+
</goals>
73+
</execution>
74+
</executions>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
</project>

example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,11 @@ public WikipediaExample.Response<X> read(JsonReader reader) throws IOException {
5858
}
5959
}
6060
reader.endObject();
61-
} else if ("query-continue".equals(nextName)) {
61+
} else if ("continue".equals(nextName)) {
6262
reader.beginObject();
6363
while (reader.hasNext()) {
64-
if ("search".equals(reader.nextName())) {
65-
reader.beginObject();
66-
while (reader.hasNext()) {
67-
if ("gsroffset".equals(reader.nextName())) {
68-
pages.nextOffset = reader.nextLong();
69-
}
70-
}
71-
reader.endObject();
64+
if ("gsroffset".equals(reader.nextName())) {
65+
pages.nextOffset = reader.nextLong();
7266
} else {
7367
reader.skipValue();
7468
}
@@ -79,7 +73,6 @@ public WikipediaExample.Response<X> read(JsonReader reader) throws IOException {
7973
}
8074
}
8175
reader.endObject();
82-
reader.close();
8376
return pages;
8477
}
8578

0 commit comments

Comments
 (0)