Skip to content

Commit 1876a85

Browse files
committed
Netflix Feign with Spring Cloud Example
1 parent 77cb45c commit 1876a85

29 files changed

Lines changed: 1189 additions & 0 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Netflix Feign with Spring Cloud Example
2+
3+
4+
## 1. Import source code into Eclipse
5+
6+
Menu **File –> Import –> Maven –> Existing Maven Projects**
7+
8+
Browse to your source code location
9+
10+
Click **Finish** button to finish the importing
11+
12+
## 2. Run Book RESTful Web Services
13+
14+
Open the **Application.java**
15+
16+
**Right click -> Run As -> Java Application** or use the shortcut: **Alt+Shift+x, j** to start the main method
17+
18+
## 3. Run the Netflix Feign with Spring Cloud Example
19+
Open spring-boot-netflix-feign-example\src\test\java\com\howtoprogram\ApplicationTests.java
20+
Right Click --> Run As --> JUnit test
21+
22+
This RESTful ws provides API for all below sub projects:
23+
###[Simple Java REST Client Using java.net.URL package]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="output" path="target/classes"/>
26+
</classpath>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>book-restful-service</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding/<project>=UTF-8
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.howtoprogram</groupId>
7+
<artifactId>book-restful-service</artifactId>
8+
<version>0.1.0</version>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.3.5.RELEASE</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-test</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.jayway.jsonpath</groupId>
28+
<artifactId>json-path</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
</dependencies>
33+
34+
<properties>
35+
<java.version>1.8</java.version>
36+
</properties>
37+
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
<repositories>
49+
<repository>
50+
<id>spring-releases</id>
51+
<url>https://repo.spring.io/libs-release</url>
52+
</repository>
53+
</repositories>
54+
<pluginRepositories>
55+
<pluginRepository>
56+
<id>spring-releases</id>
57+
<url>https://repo.spring.io/libs-release</url>
58+
</pluginRepository>
59+
</pluginRepositories>
60+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
import org.springframework.boot.SpringApplication;
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.context.annotation.ComponentScan;
5+
6+
@SpringBootApplication
7+
@ComponentScan({"com.howtoprogram.book", "com.howtoprogram.service"})
8+
public class Application {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class, args);
12+
}
13+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.howtoprogram.book;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
@XmlRootElement(name = "book")
6+
public class Book {
7+
8+
private Long id;
9+
private String name;
10+
private String author;
11+
12+
public Book() {
13+
14+
}
15+
// getter
16+
17+
public Book(Long id, String name, String author) {
18+
super();
19+
this.id = id;
20+
this.name = name;
21+
this.author = author;
22+
}
23+
24+
/**
25+
* @return the id
26+
*/
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
/**
32+
* @param id the id to set
33+
*/
34+
public void setId(Long id) {
35+
this.id = id;
36+
}
37+
38+
/**
39+
* @return the name
40+
*/
41+
public String getName() {
42+
return name;
43+
}
44+
45+
/**
46+
* @param name the name to set
47+
*/
48+
public void setName(String name) {
49+
this.name = name;
50+
}
51+
52+
/**
53+
* @return the author
54+
*/
55+
public String getAuthor() {
56+
return author;
57+
}
58+
59+
/**
60+
* @param author the author to set
61+
*/
62+
public void setAuthor(String author) {
63+
this.author = author;
64+
}
65+
66+
/*
67+
* (non-Javadoc)
68+
*
69+
* @see java.lang.Object#toString()
70+
*/
71+
@Override
72+
public String toString() {
73+
return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
74+
}
75+
76+
77+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.howtoprogram.book;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
@RestController
17+
@RequestMapping(value = "/v1")
18+
public class BookController {
19+
private static List<Book> books = new ArrayList<>();
20+
21+
public BookController() {
22+
books.add(new Book(1l, "Java How To Program", "Paul Deitel"));
23+
books.add(new Book(2l, "Thinking in Java", "Bruce Eckel"));
24+
}
25+
26+
@RequestMapping(value = "/books", method = RequestMethod.GET)
27+
public ResponseEntity<Iterable<Book>> getAllBooks() {
28+
return new ResponseEntity<>(books, HttpStatus.OK);
29+
}
30+
31+
@RequestMapping(value = "/books", method = RequestMethod.POST)
32+
public ResponseEntity<Book> createBook(@RequestBody Book book) {
33+
books.add(book);
34+
book.setId(Long.valueOf(books.size() + 1));
35+
System.out.println(book);
36+
return new ResponseEntity<>(book, HttpStatus.CREATED);
37+
}
38+
39+
@RequestMapping(value = "/books/{id}")
40+
public ResponseEntity<Book> readBook(@PathVariable("id") Long id) {
41+
Optional<Book> fBook = books.stream().filter(b -> b.getId().equals(id))
42+
.distinct()
43+
.findFirst();
44+
if (fBook.isPresent()) {
45+
return new ResponseEntity<>(fBook.get(), HttpStatus.OK);
46+
} else {
47+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
48+
}
49+
}
50+
51+
@RequestMapping(value = "/books/{id}", method = RequestMethod.DELETE)
52+
public ResponseEntity<Book> deleteBook(@PathVariable("id") Long id) {
53+
boolean found = false;
54+
for (Iterator<Book> i = books.iterator(); i.hasNext();) {
55+
Book item = i.next();
56+
if (item.getId().equals(id)) {
57+
i.remove();
58+
found = true;
59+
break;
60+
}
61+
}
62+
if (!found) {
63+
return new ResponseEntity<Book>(HttpStatus.NOT_FOUND);
64+
}
65+
66+
return new ResponseEntity<Book>(HttpStatus.NO_CONTENT);
67+
}
68+
69+
@RequestMapping(value = "/books/{id}", method = RequestMethod.PUT)
70+
public ResponseEntity<Book> updateBook(@PathVariable("id") Long id,
71+
@RequestBody Book Book) {
72+
Book currentBook = null;
73+
for (Book book : books) {
74+
if (book.getId().equals(id)) {
75+
currentBook = book;
76+
break;
77+
}
78+
}
79+
if (currentBook == null) {
80+
return new ResponseEntity<Book>(HttpStatus.NOT_FOUND);
81+
}
82+
83+
currentBook.setName(Book.getName());
84+
currentBook.setAuthor(Book.getAuthor());
85+
return new ResponseEntity<Book>(currentBook, HttpStatus.OK);
86+
}
87+
}

0 commit comments

Comments
 (0)