From 90316d2b36f7e080ef0e4dae609d1e4dc39cb02c Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 21 Sep 2017 18:15:16 -0400 Subject: [PATCH 1/7] adding data model --- .../springframework/api/domain/Billing.java | 31 +++++ .../guru/springframework/api/domain/Card.java | 67 +++++++++++ .../api/domain/ExpirationDate.java | 49 ++++++++ .../guru/springframework/api/domain/Job.java | 40 +++++++ .../springframework/api/domain/Location.java | 58 +++++++++ .../springframework/api/domain/Login.java | 67 +++++++++++ .../guru/springframework/api/domain/Name.java | 49 ++++++++ .../guru/springframework/api/domain/User.java | 113 ++++++++++++++++++ 8 files changed, 474 insertions(+) create mode 100755 src/main/java/guru/springframework/api/domain/Billing.java create mode 100755 src/main/java/guru/springframework/api/domain/Card.java create mode 100755 src/main/java/guru/springframework/api/domain/ExpirationDate.java create mode 100755 src/main/java/guru/springframework/api/domain/Job.java create mode 100755 src/main/java/guru/springframework/api/domain/Location.java create mode 100755 src/main/java/guru/springframework/api/domain/Login.java create mode 100755 src/main/java/guru/springframework/api/domain/Name.java create mode 100755 src/main/java/guru/springframework/api/domain/User.java diff --git a/src/main/java/guru/springframework/api/domain/Billing.java b/src/main/java/guru/springframework/api/domain/Billing.java new file mode 100755 index 00000000..e90c9a5b --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Billing.java @@ -0,0 +1,31 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Billing implements Serializable +{ + + private Card card; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 6577338081290507077L; + + public Card getCard() { + return card; + } + + public void setCard(Card card) { + this.card = card; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Card.java b/src/main/java/guru/springframework/api/domain/Card.java new file mode 100755 index 00000000..f4f49ddc --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Card.java @@ -0,0 +1,67 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Card implements Serializable +{ + + private String type; + private String number; + private ExpirationDate expirationDate; + private String iban; + private String swift; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 6203456183354582742L; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public ExpirationDate getExpirationDate() { + return expirationDate; + } + + public void setExpirationDate(ExpirationDate expirationDate) { + this.expirationDate = expirationDate; + } + + public String getIban() { + return iban; + } + + public void setIban(String iban) { + this.iban = iban; + } + + public String getSwift() { + return swift; + } + + public void setSwift(String swift) { + this.swift = swift; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/ExpirationDate.java b/src/main/java/guru/springframework/api/domain/ExpirationDate.java new file mode 100755 index 00000000..e3109274 --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/ExpirationDate.java @@ -0,0 +1,49 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class ExpirationDate implements Serializable +{ + + private String date; + private Integer timezoneType; + private String timezone; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 4661228813349752965L; + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public Integer getTimezoneType() { + return timezoneType; + } + + public void setTimezoneType(Integer timezoneType) { + this.timezoneType = timezoneType; + } + + public String getTimezone() { + return timezone; + } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Job.java b/src/main/java/guru/springframework/api/domain/Job.java new file mode 100755 index 00000000..5ce2654b --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Job.java @@ -0,0 +1,40 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Job implements Serializable +{ + + private String title; + private String company; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = -4985150429002262656L; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getCompany() { + return company; + } + + public void setCompany(String company) { + this.company = company; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Location.java b/src/main/java/guru/springframework/api/domain/Location.java new file mode 100755 index 00000000..569ae65e --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Location.java @@ -0,0 +1,58 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Location implements Serializable +{ + + private String street; + private String city; + private String state; + private String postcode; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = -3532048267747973846L; + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getPostcode() { + return postcode; + } + + public void setPostcode(String postcode) { + this.postcode = postcode; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Login.java b/src/main/java/guru/springframework/api/domain/Login.java new file mode 100755 index 00000000..2dcc8082 --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Login.java @@ -0,0 +1,67 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Login implements Serializable +{ + + private String username; + private String password; + private String md5; + private String sha1; + private String sha256; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 1041720428871730372L; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMd5() { + return md5; + } + + public void setMd5(String md5) { + this.md5 = md5; + } + + public String getSha1() { + return sha1; + } + + public void setSha1(String sha1) { + this.sha1 = sha1; + } + + public String getSha256() { + return sha256; + } + + public void setSha256(String sha256) { + this.sha256 = sha256; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Name.java b/src/main/java/guru/springframework/api/domain/Name.java new file mode 100755 index 00000000..8a2028ed --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Name.java @@ -0,0 +1,49 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Name implements Serializable +{ + + private String title; + private String first; + private String last; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 420620315591775395L; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getFirst() { + return first; + } + + public void setFirst(String first) { + this.first = first; + } + + public String getLast() { + return last; + } + + public void setLast(String last) { + this.last = last; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/User.java b/src/main/java/guru/springframework/api/domain/User.java new file mode 100755 index 00000000..ab7e1882 --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/User.java @@ -0,0 +1,113 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class User implements Serializable +{ + + private String gender; + private Name name; + private Location location; + private String email; + private Login login; + private String phone; + private Job job; + private Billing billing; + private String language; + private String currency; + private Map additionalProperties = new HashMap(); + + private final static long serialVersionUID = 270727596527329664L; + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public Name getName() { + return name; + } + + public void setName(Name name) { + this.name = name; + } + + public Location getLocation() { + return location; + } + + public void setLocation(Location location) { + this.location = location; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Login getLogin() { + return login; + } + + public void setLogin(Login login) { + this.login = login; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public Job getJob() { + return job; + } + + public void setJob(Job job) { + this.job = job; + } + + public Billing getBilling() { + return billing; + } + + public void setBilling(Billing billing) { + this.billing = billing; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} From 41bd7e2f21979623fe803da8f7b0a12150302661 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 21 Sep 2017 18:56:41 -0400 Subject: [PATCH 2/7] adding Rest API Service --- .../springframework/api/domain/UserData.java | 19 +++++++++++ .../config/RestTemplateConfig.java | 19 +++++++++++ .../services/ApiService.java | 13 +++++++ .../services/ApiServiceImpl.java | 28 +++++++++++++++ .../services/ApiServiceImplTest.java | 34 +++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 src/main/java/guru/springframework/api/domain/UserData.java create mode 100644 src/main/java/guru/springframework/springrestclientexamples/config/RestTemplateConfig.java create mode 100644 src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java create mode 100644 src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java create mode 100644 src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java diff --git a/src/main/java/guru/springframework/api/domain/UserData.java b/src/main/java/guru/springframework/api/domain/UserData.java new file mode 100644 index 00000000..26fc28dc --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/UserData.java @@ -0,0 +1,19 @@ +package guru.springframework.api.domain; + +import java.util.List; + +/** + * Created by jt on 9/21/17. + */ +public class UserData { + + List data; + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } +} diff --git a/src/main/java/guru/springframework/springrestclientexamples/config/RestTemplateConfig.java b/src/main/java/guru/springframework/springrestclientexamples/config/RestTemplateConfig.java new file mode 100644 index 00000000..5656c26e --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/config/RestTemplateConfig.java @@ -0,0 +1,19 @@ +package guru.springframework.springrestclientexamples.config; + +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * Created by jt on 9/21/17. + */ +@Configuration +public class RestTemplateConfig { + + @Bean + public RestTemplate restTemplate(RestTemplateBuilder builder){ + + return builder.build(); + } +} diff --git a/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java b/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java new file mode 100644 index 00000000..6c39858d --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java @@ -0,0 +1,13 @@ +package guru.springframework.springrestclientexamples.services; + +import guru.springframework.api.domain.User; + +import java.util.List; + +/** + * Created by jt on 9/21/17. + */ +public interface ApiService { + + List getUsers(Integer limit); +} diff --git a/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java new file mode 100644 index 00000000..d11ad5ab --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java @@ -0,0 +1,28 @@ +package guru.springframework.springrestclientexamples.services; + +import guru.springframework.api.domain.User; +import guru.springframework.api.domain.UserData; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.List; + +/** + * Created by jt on 9/21/17. + */ +@Service +public class ApiServiceImpl implements ApiService { + + private RestTemplate restTemplate; + + public ApiServiceImpl(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + @Override + public List getUsers(Integer limit) { + + UserData userData = restTemplate.getForObject("http://apifaketory.com/api/user?limit=" + limit, UserData.class); + return userData.getData(); + } +} diff --git a/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java new file mode 100644 index 00000000..1853e950 --- /dev/null +++ b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java @@ -0,0 +1,34 @@ +package guru.springframework.springrestclientexamples.services; + +import guru.springframework.api.domain.User; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApiServiceImplTest { + + @Autowired + ApiService apiService; + + @Before + public void setUp() throws Exception { + } + + + @Test + public void testGetUsers() throws Exception { + + List users = apiService.getUsers(3); + + assertEquals(4, users.size()); + } +} \ No newline at end of file From c88eff964a3ad32f81a3b71ddb9a897247890423 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Fri, 22 Sep 2017 14:06:52 -0400 Subject: [PATCH 3/7] adding web form and view --- pom.xml | 207 ++++++++++-------- .../controllers/UserController.java | 48 ++++ src/main/resources/templates/index.html | 42 ++++ src/main/resources/templates/userlist.html | 76 +++++++ .../controllers/UserControllerTest.java | 52 +++++ 5 files changed, 328 insertions(+), 97 deletions(-) create mode 100644 src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java create mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/userlist.html create mode 100644 src/test/java/guru/springframework/springrestclientexamples/controllers/UserControllerTest.java diff --git a/pom.xml b/pom.xml index 0726c87e..fc9c2f08 100644 --- a/pom.xml +++ b/pom.xml @@ -1,111 +1,124 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - guru.springframework - spring-rest-client-examples - 0.0.1-SNAPSHOT - jar + guru.springframework + spring-rest-client-examples + 0.0.1-SNAPSHOT + jar - spring-rest-client-examples - Demo project for Spring Boot + spring-rest-client-examples + Demo project for Spring Boot - - org.springframework.boot - spring-boot-starter-parent - 2.0.0.M4 - - + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.M4 + + - - UTF-8 - UTF-8 - 1.8 - + + UTF-8 + UTF-8 + 1.8 + + + 3.0.8-SNAPSHOT + 3.0.8-SNAPSHOT + - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-webflux - + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-webflux + - - org.springframework.boot - spring-boot-devtools - runtime - - - org.projectlombok - lombok - true - - - org.springframework.boot - spring-boot-starter-test - test - - - io.projectreactor - reactor-test - test - - + + org.springframework.boot + spring-boot-devtools + runtime + + + org.projectlombok + lombok + true + + + org.webjars + bootstrap + 3.3.7-1 + + + org.springframework.boot + spring-boot-starter-test + test + + + io.projectreactor + reactor-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + sonatype-nexus-snapshots + Sonatype Nexus Snapshots + https://oss.sonatype.org/content/repositories/snapshots + + true + + + - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + diff --git a/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java b/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java new file mode 100644 index 00000000..f481be74 --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java @@ -0,0 +1,48 @@ +package guru.springframework.springrestclientexamples.controllers; + +import guru.springframework.springrestclientexamples.services.ApiService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.server.ServerWebExchange; + +/** + * Created by jt on 9/22/17. + */ +@Slf4j +@Controller +public class UserController { + + private ApiService apiService; + + public UserController(ApiService apiService) { + this.apiService = apiService; + } + + @GetMapping({"", "/", "/index"}) + public String index(){ + return "index"; + } + + @PostMapping("/users") + public String formPost(Model model, ServerWebExchange serverWebExchange){ + + MultiValueMap map = serverWebExchange.getFormData().block(); + + Integer limit = new Integer(map.get("limit").get(0)); + + log.debug("Received Limit value: " + limit); + //default if null or zero + if(limit == null || limit == 0){ + log.debug("Setting limit to default of 10"); + limit = 10; + } + + model.addAttribute("users", apiService.getUsers(limit)); + + return "userlist"; + } +} diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 00000000..e507ecdc --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,42 @@ + + + + + Get Users + + + + + + + + + +
+
+
+
+
+
+
+ + + +
+
+
+
+ +
+
+
+
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/userlist.html b/src/main/resources/templates/userlist.html new file mode 100644 index 00000000..1ae05ea5 --- /dev/null +++ b/src/main/resources/templates/userlist.html @@ -0,0 +1,76 @@ + + + + + Users + + + + + + + + + + + + +
+
+
+
+ +
+

Users from API

+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
First NameLast NameGenderEmailLanguagePhone
JoeBuckMalefoo@example.comEnglish132-123-1234
JoeBuckMalefoo@example.comEnglish132-123-1234
JoeBuckMalefoo@example.comEnglish132-123-1234
+
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/src/test/java/guru/springframework/springrestclientexamples/controllers/UserControllerTest.java b/src/test/java/guru/springframework/springrestclientexamples/controllers/UserControllerTest.java new file mode 100644 index 00000000..75d7284f --- /dev/null +++ b/src/test/java/guru/springframework/springrestclientexamples/controllers/UserControllerTest.java @@ -0,0 +1,52 @@ +package guru.springframework.springrestclientexamples.controllers; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.reactive.function.BodyInserters; + + +@RunWith(SpringRunner.class) +@SpringBootTest +public class UserControllerTest { + + @Autowired + ApplicationContext applicationContext; + + WebTestClient webTestClient; + + @Before + public void setUp() throws Exception { + webTestClient = WebTestClient.bindToApplicationContext(applicationContext).build(); + } + + @Test + public void index() throws Exception { + + webTestClient.get().uri("/") + .exchange() + .expectStatus().isOk(); + } + + @Test + public void formPost() throws Exception { + + MultiValueMap formData = new LinkedMultiValueMap<>(); + formData.add("limit", "3"); + + webTestClient.post().uri("users") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .body(BodyInserters.fromFormData(formData)) + .exchange() + .expectStatus().isOk(); + } + +} \ No newline at end of file From add5cfa91b62d22482d94e70551231d2829ff5cc Mon Sep 17 00:00:00 2001 From: John Thompson Date: Fri, 22 Sep 2017 14:23:48 -0400 Subject: [PATCH 4/7] Adding URI Component Builder --- .../services/ApiServiceImpl.java | 13 +++++++++++-- src/main/resources/application.properties | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java index d11ad5ab..ca7c6299 100644 --- a/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java @@ -2,8 +2,10 @@ import guru.springframework.api.domain.User; import guru.springframework.api.domain.UserData; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; import java.util.List; @@ -15,14 +17,21 @@ public class ApiServiceImpl implements ApiService { private RestTemplate restTemplate; - public ApiServiceImpl(RestTemplate restTemplate) { + private final String api_url; + + public ApiServiceImpl(RestTemplate restTemplate, @Value("${api.url}") String api_url) { this.restTemplate = restTemplate; + this.api_url = api_url; } @Override public List getUsers(Integer limit) { - UserData userData = restTemplate.getForObject("http://apifaketory.com/api/user?limit=" + limit, UserData.class); + UriComponentsBuilder uriBuilder = UriComponentsBuilder + .fromUriString(api_url) + .queryParam("limit", limit); + + UserData userData = restTemplate.getForObject(uriBuilder.toUriString(), UserData.class); return userData.getData(); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29b..1ae5c07f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +api.url=http://apifaketory.com/api/user \ No newline at end of file From 4bb6856c293e52b9b9471a766eabf9e9e7efd310 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Sat, 23 Sep 2017 11:27:01 -0400 Subject: [PATCH 5/7] adding resttemplate examples --- pom.xml | 6 + .../RestTemplateExamples.java | 216 ++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 src/test/java/guru/springframework/springrestclientexamples/RestTemplateExamples.java diff --git a/pom.xml b/pom.xml index fc9c2f08..77c3b8f3 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,12 @@ bootstrap 3.3.7-1 + + org.apache.httpcomponents + httpclient + 4.4.1 + + org.springframework.boot spring-boot-starter-test diff --git a/src/test/java/guru/springframework/springrestclientexamples/RestTemplateExamples.java b/src/test/java/guru/springframework/springrestclientexamples/RestTemplateExamples.java new file mode 100644 index 00000000..a6101763 --- /dev/null +++ b/src/test/java/guru/springframework/springrestclientexamples/RestTemplateExamples.java @@ -0,0 +1,216 @@ +package guru.springframework.springrestclientexamples; + +import com.fasterxml.jackson.databind.JsonNode; +import org.junit.Test; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestTemplate; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by jt on 9/22/17. + */ +public class RestTemplateExamples { + + public static final String API_ROOT = "https://api.predic8.de:443/shop"; + + @Test + public void getCategories() throws Exception { + String apiUrl = API_ROOT + "/categories/"; + + RestTemplate restTemplate = new RestTemplate(); + + JsonNode jsonNode = restTemplate.getForObject(apiUrl, JsonNode.class); + + System.out.println("Response"); + System.out.println(jsonNode.toString()); + } + + @Test + public void getCustomers() throws Exception { + String apiUrl = API_ROOT + "/customers/"; + + RestTemplate restTemplate = new RestTemplate(); + + JsonNode jsonNode = restTemplate.getForObject(apiUrl, JsonNode.class); + + System.out.println("Response"); + System.out.println(jsonNode.toString()); + } + + @Test + public void createCustomer() throws Exception { + String apiUrl = API_ROOT + "/customers/"; + + RestTemplate restTemplate = new RestTemplate(); + + //Java object to parse to JSON + Map postMap = new HashMap<>(); + postMap.put("firstname", "Joe"); + postMap.put("lastname", "Buck"); + + JsonNode jsonNode = restTemplate.postForObject(apiUrl, postMap, JsonNode.class); + + System.out.println("Response"); + System.out.println(jsonNode.toString()); + } + + @Test + public void updateCustomer() throws Exception { + + //create customer to update + String apiUrl = API_ROOT + "/customers/"; + + RestTemplate restTemplate = new RestTemplate(); + + //Java object to parse to JSON + Map postMap = new HashMap<>(); + postMap.put("firstname", "Micheal"); + postMap.put("lastname", "Weston"); + + JsonNode jsonNode = restTemplate.postForObject(apiUrl, postMap, JsonNode.class); + + System.out.println("Response"); + System.out.println(jsonNode.toString()); + + String customerUrl = jsonNode.get("customer_url").textValue(); + + String id = customerUrl.split("/")[3]; + + System.out.println("Created customer id: " + id); + + postMap.put("firstname", "Micheal 2"); + postMap.put("lastname", "Weston 2"); + + restTemplate.put(apiUrl + id, postMap); + + JsonNode updatedNode = restTemplate.getForObject(apiUrl + id, JsonNode.class); + + System.out.println(updatedNode.toString()); + + } + + @Test(expected = ResourceAccessException.class) + public void updateCustomerUsingPatchSunHttp() throws Exception { + + //create customer to update + String apiUrl = API_ROOT + "/customers/"; + + RestTemplate restTemplate = new RestTemplate(); + + //Java object to parse to JSON + Map postMap = new HashMap<>(); + postMap.put("firstname", "Sam"); + postMap.put("lastname", "Axe"); + + JsonNode jsonNode = restTemplate.postForObject(apiUrl, postMap, JsonNode.class); + + System.out.println("Response"); + System.out.println(jsonNode.toString()); + + String customerUrl = jsonNode.get("customer_url").textValue(); + + String id = customerUrl.split("/")[3]; + + System.out.println("Created customer id: " + id); + + postMap.put("firstname", "Sam 2"); + postMap.put("lastname", "Axe 2"); + + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + headers.setContentType(MediaType.APPLICATION_JSON); + + HttpEntity> entity = new HttpEntity<>(postMap, headers); + + //fails due to sun.net.www.protocol.http.HttpURLConnection not supporting patch + JsonNode updatedNode = restTemplate.patchForObject(apiUrl + id, entity, JsonNode.class); + + System.out.println(updatedNode.toString()); + + } + + @Test + public void updateCustomerUsingPatch() throws Exception { + + //create customer to update + String apiUrl = API_ROOT + "/customers/"; + + // Use Apache HTTP client factory + //see: https://github.com/spring-cloud/spring-cloud-netflix/issues/1777 + HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); + RestTemplate restTemplate = new RestTemplate(requestFactory); + + //Java object to parse to JSON + Map postMap = new HashMap<>(); + postMap.put("firstname", "Sam"); + postMap.put("lastname", "Axe"); + + JsonNode jsonNode = restTemplate.postForObject(apiUrl, postMap, JsonNode.class); + + System.out.println("Response"); + System.out.println(jsonNode.toString()); + + String customerUrl = jsonNode.get("customer_url").textValue(); + + String id = customerUrl.split("/")[3]; + + System.out.println("Created customer id: " + id); + + postMap.put("firstname", "Sam 2"); + postMap.put("lastname", "Axe 2"); + + //example of setting headers + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + headers.setContentType(MediaType.APPLICATION_JSON); + + HttpEntity> entity = new HttpEntity<>(postMap, headers); + + JsonNode updatedNode = restTemplate.patchForObject(apiUrl + id, entity, JsonNode.class); + + System.out.println(updatedNode.toString()); + } + + @Test(expected = HttpClientErrorException.class) + public void deleteCustomer() throws Exception { + + //create customer to update + String apiUrl = API_ROOT + "/customers/"; + + RestTemplate restTemplate = new RestTemplate(); + + //Java object to parse to JSON + Map postMap = new HashMap<>(); + postMap.put("firstname", "Les"); + postMap.put("lastname", "Claypool"); + + JsonNode jsonNode = restTemplate.postForObject(apiUrl, postMap, JsonNode.class); + + System.out.println("Response"); + System.out.println(jsonNode.toString()); + + String customerUrl = jsonNode.get("customer_url").textValue(); + + String id = customerUrl.split("/")[3]; + + System.out.println("Created customer id: " + id); + + restTemplate.delete(apiUrl + id); //expects 200 status + + System.out.println("Customer deleted"); + + //should go boom on 404 + restTemplate.getForObject(apiUrl + id, JsonNode.class); + + } + + +} From 99fe820a6e1803230ac20f85a57e39b1fb4f35a4 Mon Sep 17 00:00:00 2001 From: springframeworkguru Date: Tue, 12 Dec 2017 09:54:14 +0530 Subject: [PATCH 6/7] Updated to Spring Boot 2.0.0.M7 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 77c3b8f3..9ed278a3 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M4 + 2.0.0.M7 From 1c26cae8884528bad455858adc3e4ae6f7d68cb5 Mon Sep 17 00:00:00 2001 From: springframeworkguru Date: Sat, 24 Mar 2018 20:04:55 +0530 Subject: [PATCH 7/7] Updated to Spring Boot 2.0.0.RELEASE --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 9ed278a3..41e93d99 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M7 + 2.0.0.RELEASE