diff --git a/pom.xml b/pom.xml index 812708b4..c36b2312 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,6 @@ + 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 @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.0.RELEASE + 2.0.0.RELEASE @@ -22,6 +22,10 @@ UTF-8 UTF-8 1.8 + + + 3.0.8-SNAPSHOT + 3.0.8-SNAPSHOT @@ -29,10 +33,6 @@ org.springframework.boot spring-boot-starter-thymeleaf - - org.springframework.boot - spring-boot-starter-web - org.springframework.boot spring-boot-starter-webflux @@ -48,6 +48,11 @@ lombok true + + org.webjars + bootstrap + 3.3.7-1 + org.springframework.boot spring-boot-starter-test @@ -86,6 +91,14 @@ false + + sonatype-nexus-snapshots + Sonatype Nexus Snapshots + https://oss.sonatype.org/content/repositories/snapshots + + true + + 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 100644 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 100644 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 100644 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 100644 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 100644 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 100644 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 100644 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 100644 index 00000000..d02cb12f --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/User.java @@ -0,0 +1,112 @@ + +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); + } + +} 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..a1ff0f9f --- /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 ccabo 8/22/19 + */ +public class UserData { + + private 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..0e0b91f7 --- /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 ccabo 8/22/19 + */ +@Configuration + +public class RestTemplateConfig { + + @Bean + public RestTemplate restTemplate(RestTemplateBuilder builder) { + return builder.build(); + } +} 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..977e89fe --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java @@ -0,0 +1,28 @@ +package guru.springframework.springrestclientexamples.controllers; + +import guru.springframework.springrestclientexamples.services.ApiService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by ccabo 8/23/19 + */ +@Slf4j +@Controller +public class UserController { + public ApiService apiService; + + @Autowired + public UserController(ApiService apiService) { + this.apiService = apiService; + } + + @RequestMapping({"", "/", "/index"}) + public String index() { + return "index"; + } + + // TODO: 23/8/2019 Refactor comming up! +} 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..fbc760ab --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java @@ -0,0 +1,19 @@ +package guru.springframework.springrestclientexamples.services; + +import guru.springframework.api.domain.User; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.List; + +/** + * Created by ccabo 8/22/19 + */ +@Service +public interface ApiService { + + List getUsers(Integer limit); + + Flux getUsers(Mono 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..6e470037 --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java @@ -0,0 +1,55 @@ +package guru.springframework.springrestclientexamples.services; + +import guru.springframework.api.domain.User; +import guru.springframework.api.domain.UserData; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.UriComponentsBuilder; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.List; + +/** + * Created by ccabo 8/22/19 + */ +@Service +public class ApiServiceImpl implements ApiService { + + private RestTemplate restTemplate; + private final String api_url; + + @Autowired + public ApiServiceImpl(RestTemplate restTemplate, @Value("${api_url}") String api_url) { + this.restTemplate = restTemplate; + this.api_url = api_url; + } + + @Override + public List getUsers(Integer limit) { + + UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder + .fromUriString(api_url) + .queryParam("limit", limit); + + UserData userData = restTemplate + .getForObject(uriComponentsBuilder.toUriString(), UserData.class); + return userData.getData(); + } + + @Override + public Flux getUsers(Mono limit) { + + return WebClient.create(api_url) + .get() + .uri(uriBuilder -> uriBuilder.queryParam("limit", limit.block()).build()) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .flatMap(resp -> resp.bodyToMono(UserData.class)) + .flatMapIterable(UserData::getData); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29b..a1fa5d04 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 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/services/ApiServiceImplTest.java b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java new file mode 100644 index 00000000..03a0619a --- /dev/null +++ b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java @@ -0,0 +1,33 @@ +package guru.springframework.springrestclientexamples.services; + +import com.fasterxml.jackson.databind.JsonNode; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by ccabo 8/22/19 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApiServiceImplTest { + + public static final String API_ROOT = "https://virtserver.swaggerhub.com/CesarNicolasCabo/ApiTest/1.0.0"; + + @Test + public void getInventory() throws Exception{ + String apiUrl = API_ROOT + "/inventory/"; + + RestTemplate restTemplate = new RestTemplate(); + + JsonNode jsonNode = restTemplate.getForObject(apiUrl, JsonNode.class); + + System.out.println("Response: "); + System.out.println(jsonNode.toString()); + } +} \ No newline at end of file