diff --git a/.gitignore b/.gitignore index a4ea2ed..5eac309 100644 --- a/.gitignore +++ b/.gitignore @@ -1,29 +1,33 @@ -# Compiled class file -*.class +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ -# IntelliJ IDEA +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### .idea *.iws *.iml *.ipr -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* +### VS Code ### +.vscode/ \ No newline at end of file diff --git a/eureka/discovery-server/pom.xml b/eureka/discovery-server/pom.xml index fd67994..f0aba1b 100644 --- a/eureka/discovery-server/pom.xml +++ b/eureka/discovery-server/pom.xml @@ -50,4 +50,4 @@ - + \ No newline at end of file diff --git a/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java index 15d783c..460d747 100644 --- a/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java +++ b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java @@ -4,9 +4,11 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import java.io.IOException; +@EnableEurekaServer @SpringBootApplication public class DiscoveryServer { @@ -22,4 +24,4 @@ public static void main(String... args) throws IOException { System.in.read(); ctx.close(); } -} +} \ No newline at end of file diff --git a/eureka/discovery-server/src/main/resources/discovery-server.yml b/eureka/discovery-server/src/main/resources/discovery-server.yml index b55b43b..df053f2 100644 --- a/eureka/discovery-server/src/main/resources/discovery-server.yml +++ b/eureka/discovery-server/src/main/resources/discovery-server.yml @@ -1,8 +1,14 @@ spring: application: name: discovery-service -# Configure this Discovery Server -#TODO here you add configurations for server + +eureka: + client: + register-with-eureka: false + fetch-registry: false + +server: + port: 3000 logging: pattern: @@ -10,4 +16,4 @@ logging: level: root: INFO org.springframework: DEBUG - com.apress.cems: DEBUG + com.apress.cems: DEBUG \ No newline at end of file diff --git a/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java b/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java index f102053..358ae22 100644 --- a/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java +++ b/eureka/discovery-server/src/test/java/com/example/discoveryserver/DiscoveryServerApplicationTests.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; + @SpringBootTest class DiscoveryServerApplicationTests { @@ -11,3 +12,5 @@ void contextLoads() { } } + + diff --git a/eureka/persons-server/pom.xml b/eureka/persons-server/pom.xml index b342d8e..21d9f55 100644 --- a/eureka/persons-server/pom.xml +++ b/eureka/persons-server/pom.xml @@ -93,4 +93,4 @@ - + \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java index 5e7b169..ee5b588 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonRepo.java @@ -33,4 +33,4 @@ public interface PersonRepo extends JpaRepository { @Query("select p from Person p where p.hiringDate=:hd") List findByHiringDate(@Param("hd") LocalDateTime date); -} +} \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java index ff4acb5..639c2d8 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java @@ -1,14 +1,22 @@ package com.eureka.persons; -import com.eureka.persons.person.Person; -import com.eureka.persons.services.PersonService; +import java.util.Comparator; +import java.util.List; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.*; - -import java.util.ArrayList; -import java.util.List; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; +import com.eureka.persons.ex.NotFoundException; +import com.eureka.persons.person.Person; +import com.eureka.persons.services.PersonService; @RestController @RequestMapping("/persons") @@ -26,7 +34,16 @@ public PersonsController(PersonService personService) { @ResponseStatus(HttpStatus.OK) @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) public List list() { - return new ArrayList<>(); + List people = personService.findAll(); + + people.sort(new Comparator() { + @Override + public int compare(Person p1, Person p2) { + return p1.getId().compareTo(p2.getId()); + } + }); + + return people; } /** @@ -36,6 +53,11 @@ public List list() { @ResponseStatus(HttpStatus.CREATED) @PostMapping public void create(@RequestBody Person person, BindingResult result) { + if(result.hasErrors()){ + throw new PersonsException(HttpStatus.BAD_REQUEST, "There was an error."); + } else { + personService.save(person); + } } /** @@ -48,7 +70,7 @@ public void create(@RequestBody Person person, BindingResult result) { @ResponseStatus(HttpStatus.OK) @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public Person show(@PathVariable Long id) { - return new Person(); + return personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id)); } /** @@ -62,6 +84,16 @@ public Person show(@PathVariable Long id) { @ResponseStatus(HttpStatus.NO_CONTENT) @PutMapping("/{id}") public void update(@RequestBody Person updatedPerson, @PathVariable Long id) { + Person person = personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id)); + + person.setUsername(updatedPerson.getUsername()); + person.setFirstName(updatedPerson.getFirstName()); + person.setLastName(updatedPerson.getLastName()); + person.setPassword(updatedPerson.getPassword()); + person.setHiringDate(updatedPerson.getHiringDate()); + person.setNewPassword(updatedPerson.getNewPassword()); + + personService.save(person); } /** @@ -73,5 +105,6 @@ public void update(@RequestBody Person updatedPerson, @PathVariable Long id) { @ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { + personService.delete(personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id))); } } \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java index f2098ae..b764325 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java @@ -5,9 +5,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import java.io.IOException; +@EnableEurekaClient @EntityScan(basePackages = "com.eureka.persons") @SpringBootApplication public class PersonsServer { diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java b/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java index 199103b..8a7f91e 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/base/AbstractEntity.java @@ -1,16 +1,20 @@ package com.eureka.persons.base; -import com.eureka.persons.util.DateProcessor; -import com.fasterxml.jackson.annotation.JsonFormat; -import lombok.Getter; -import lombok.Setter; -import org.springframework.format.annotation.DateTimeFormat; - -import javax.persistence.*; import java.io.Serializable; import java.time.LocalDateTime; import java.util.Comparator; import java.util.Objects; +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; +import javax.persistence.Version; +import org.springframework.format.annotation.DateTimeFormat; +import com.eureka.persons.util.DateProcessor; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Getter; +import lombok.Setter; @MappedSuperclass @Getter @@ -24,6 +28,14 @@ public abstract class AbstractEntity implements Serializable { @Column(updatable = false) protected Long id; + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + @Version protected int version; @@ -51,7 +63,6 @@ protected AbstractEntity() { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - var that = (AbstractEntity) o; if (!Objects.equals(id, that.id)) return false; return true; @@ -67,4 +78,4 @@ public String toString() { return String.format("AbstractEntity[id='%d%n', createdAt='%s', modifiedAt='%s', version='%d%n']", id, DateProcessor.toString(createdAt), DateProcessor.toString(modifiedAt), version); } -} +} \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java b/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java index 9064df8..5851633 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/person/Person.java @@ -9,7 +9,6 @@ import lombok.NoArgsConstructor; import lombok.Setter; import org.springframework.format.annotation.DateTimeFormat; - import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Transient; @@ -76,7 +75,53 @@ public int hashCode() { public String toString() { return String.format("Person[username='%s', firstName='%s', lastName='%s', hiringDate='%s']\n", username, firstName, lastName, hiringDate.toString()); + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; } -} + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public LocalDateTime getHiringDate() { + return hiringDate; + } + + public void setHiringDate(LocalDateTime hiringDate) { + this.hiringDate = hiringDate; + } + + public String getNewPassword() { + return newPassword; + } + + public void setNewPassword(String newPassword) { + this.newPassword = newPassword; + } +} \ No newline at end of file diff --git a/eureka/persons-server/src/main/resources/persons-server.yml b/eureka/persons-server/src/main/resources/persons-server.yml index b2b466d..21392bc 100644 --- a/eureka/persons-server/src/main/resources/persons-server.yml +++ b/eureka/persons-server/src/main/resources/persons-server.yml @@ -26,6 +26,12 @@ server: # Discovery Server Access #TODO here you add configurations for eureka client +eureka: + client: + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + fetchRegistry: true + info: app: name: persons-server @@ -36,6 +42,6 @@ logging: pattern: console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" level: - root: INFO + root: DEBUG org.springframework: DEBUG - com.apress.cems: DEBUG + com.apress.cems: DEBUG \ No newline at end of file diff --git a/java8/src/test/java/com/unitbv/Java8Tests.java b/java8/src/test/java/com/unitbv/Java8Tests.java index 849a3fe..841cdeb 100644 --- a/java8/src/test/java/com/unitbv/Java8Tests.java +++ b/java8/src/test/java/com/unitbv/Java8Tests.java @@ -214,8 +214,10 @@ public void getGetMapOfUsers(){ public void testGetPredicateForFilteringName(){ String name = "John"; Predicate predicate = dataSource.getPredicateForFilteringByName(name); - List expected = Stream.of(new User(1, "John", "Wick", 35, "actor")) - .collect(Collectors.toList()); + List expected = Stream.of( + new User(1, "John", "Wick", 35, "actor"), + new User(7, "Mark", "John", 17, "student") + ).collect(Collectors.toList()); List actual = dataSource.filterUsers(predicate); Assertions.assertEquals(expected, actual); } diff --git a/lab-6-api-gateway/api-gateway-project/pom.xml b/lab-6-api-gateway/api-gateway-project/pom.xml index 871ce8d..fe4ab8d 100644 --- a/lab-6-api-gateway/api-gateway-project/pom.xml +++ b/lab-6-api-gateway/api-gateway-project/pom.xml @@ -9,28 +9,23 @@ com.example - api-gateway-project + lab-6-api-gateway 0.0.1-SNAPSHOT - api-gateway-project - api-gateway-project + lab-6-api-gateway + lab-6-api-gateway 11 - 2021.0.1 - - + pom + + api-gateway-project + service1 + service2 + org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-webflux - - - org.springframework.cloud - spring-cloud-starter-gateway + spring-boot-starter @@ -38,23 +33,7 @@ spring-boot-starter-test test - - io.projectreactor - reactor-test - test - - - - - org.springframework.cloud - spring-cloud-dependencies - ${spring-cloud.version} - pom - import - - - @@ -65,4 +44,4 @@ - + \ No newline at end of file diff --git a/lab-6-api-gateway/service1/pom.xml b/lab-6-api-gateway/service1/pom.xml index 478fe3f..5f2f8d5 100644 --- a/lab-6-api-gateway/service1/pom.xml +++ b/lab-6-api-gateway/service1/pom.xml @@ -18,10 +18,10 @@ 2021.0.1 - + org.springframework.boot spring-boot-starter-actuator @@ -29,8 +29,12 @@ org.springframework.boot spring-boot-starter-web - + + + org.springframework.cloud + spring-cloud-netflix-eureka-client + org.springframework.boot spring-boot-starter-test diff --git a/lab-6-api-gateway/service1/src/main/resources/application.yml b/lab-6-api-gateway/service1/src/main/resources/application.yml index 54b155f..61712e3 100644 --- a/lab-6-api-gateway/service1/src/main/resources/application.yml +++ b/lab-6-api-gateway/service1/src/main/resources/application.yml @@ -1,2 +1,14 @@ server: - port: 8081 \ No newline at end of file + port: 8081 + address: 0.0.0.0 + +spring: + application: + name: service1 + +eureka: + client: + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + register-with-eureka: true + fetch-registry: true \ No newline at end of file diff --git a/lab-6-api-gateway/service2/pom.xml b/lab-6-api-gateway/service2/pom.xml index be06918..a3e7960 100644 --- a/lab-6-api-gateway/service2/pom.xml +++ b/lab-6-api-gateway/service2/pom.xml @@ -26,16 +26,32 @@ org.springframework.boot spring-boot-starter-web - + org.springframework.boot spring-boot-starter-test test + + org.springframework.cloud + spring-cloud-netflix-eureka-client + + + org.hibernate + hibernate-core + + + org.projectlombok + lombok + + + org.projectlombok + lombok + diff --git a/lab-6-api-gateway/service2/src/main/java/com/example/service2/Product.java b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Product.java new file mode 100644 index 0000000..a7436a4 --- /dev/null +++ b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Product.java @@ -0,0 +1,24 @@ +package com.example.service2; + +import com.sun.istack.NotNull; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import javax.persistence.Column; +import javax.persistence.Entity; + +@Entity +@Getter +@Setter +@NoArgsConstructor +public class Product { + + @NotNull + @Column(nullable = false, unique = true) + private String name; + + @NotNull + @Column(nullable = false, unique = true) + private int quantity; +} diff --git a/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Controller.java b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Controller.java new file mode 100644 index 0000000..05c01b2 --- /dev/null +++ b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Controller.java @@ -0,0 +1,34 @@ +package com.example.service2; + +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/product") +public class Service2Controller { + + private List products = new ArrayList(); + + @ResponseStatus(HttpStatus.OK) + @PostMapping() + public void addProduct(@RequestBody Product product, @RequestHeader Map headers) { + for(Map.Entry header : headers.entrySet()) { + System.out.println(header); + } + products.add(product); + } + + @ResponseStatus(HttpStatus.OK) + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) + public List getProducts(@RequestHeader Map headers) { + for(Map.Entry header : headers.entrySet()) { + System.out.println(header); + } + return products; + } +} \ No newline at end of file diff --git a/lab-6-api-gateway/service2/src/main/resources/application.yml b/lab-6-api-gateway/service2/src/main/resources/application.yml index 4772153..20b9976 100644 --- a/lab-6-api-gateway/service2/src/main/resources/application.yml +++ b/lab-6-api-gateway/service2/src/main/resources/application.yml @@ -1,3 +1,15 @@ server: - port: 8082 \ No newline at end of file + port: 8082 + address: 0.0.0.0 + + spring: + application: + name: service2 + + eureka: + client: + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + register-with-eureka: true + fetch-registry: true \ No newline at end of file