From b9c725f67567c5e68654ac8bc896b7c6c031700b Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Fri, 27 Dec 2024 18:32:24 +0100 Subject: [PATCH] Get rid of Lombok #251 (#300) --- README.md | 3 +- pom.xml | 9 -- spring-petclinic-api-gateway/pom.xml | 5 - .../application/CustomersServiceClient.java | 6 +- .../api/application/VisitsServiceClient.java | 6 +- .../boundary/web/ApiGatewayController.java | 24 +++-- .../petclinic/api/dto/OwnerDetails.java | 81 ++++++++++++---- .../samples/petclinic/api/dto/PetDetails.java | 58 +++++++++-- .../samples/petclinic/api/dto/PetType.java | 7 +- .../petclinic/api/dto/VisitDetails.java | 19 +--- .../samples/petclinic/api/dto/Visits.java | 13 ++- .../VisitsServiceClientIntegrationTest.java | 8 +- .../web/ApiGatewayControllerTest.java | 48 +++++----- spring-petclinic-customers-service/pom.xml | 5 - .../petclinic/customers/model/Owner.java | 76 +++++++++------ .../petclinic/customers/model/Pet.java | 74 ++++++++++++--- .../petclinic/customers/model/PetType.java | 30 +++--- .../customers/web/OwnerResource.java | 13 ++- .../petclinic/customers/web/PetResource.java | 12 ++- spring-petclinic-vets-service/pom.xml | 5 - .../petclinic/vets/model/Specialty.java | 24 ++--- .../samples/petclinic/vets/model/Vet.java | 51 +++++----- .../petclinic/vets/system/VetsProperties.java | 20 ++-- .../petclinic/vets/web/VetResource.java | 7 +- spring-petclinic-visits-service/pom.xml | 5 - .../samples/petclinic/visits/model/Visit.java | 95 +++++++++++++++---- .../petclinic/visits/web/VisitResource.java | 19 ++-- .../visits/web/VisitResourceTest.java | 8 +- 28 files changed, 458 insertions(+), 273 deletions(-) diff --git a/README.md b/README.md index 9e4ae08a6..54318e56c 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,8 @@ and the Eureka Service Discovery from the [Spring Cloud Netflix](https://github. ## Starting services locally without Docker -Every microservice is a Spring Boot application and can be started locally using IDE ([Lombok](https://projectlombok.org/) plugin has to be set up) or `../mvnw spring-boot:run` command. Please note that supporting services (Config and Discovery Server) must be started before any other application (Customers, Vets, Visits and API). +Every microservice is a Spring Boot application and can be started locally using IDE or `../mvnw spring-boot:run` command. +Please note that supporting services (Config and Discovery Server) must be started before any other application (Customers, Vets, Visits and API). Startup of Tracing server, Admin server, Grafana and Prometheus is optional. If everything goes well, you can access the following services at given location: * Discovery Server - http://localhost:8761 diff --git a/pom.xml b/pom.xml index 0ce4bc86c..19d556e08 100644 --- a/pom.xml +++ b/pom.xml @@ -33,10 +33,6 @@ 3.1.0 1.7.1 - - 1.18.30 - springcommunity 9090 ${basedir} @@ -69,11 +65,6 @@ jolokia-core ${jolokia-core.version} - - org.projectlombok - lombok - ${lombok.version} - diff --git a/spring-petclinic-api-gateway/pom.xml b/spring-petclinic-api-gateway/pom.xml index 4db970d78..802f34f30 100644 --- a/spring-petclinic-api-gateway/pom.xml +++ b/spring-petclinic-api-gateway/pom.xml @@ -71,11 +71,6 @@ org.jolokia jolokia-core - - org.projectlombok - lombok - provided - io.micrometer micrometer-registry-prometheus diff --git a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/application/CustomersServiceClient.java b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/application/CustomersServiceClient.java index 691254e00..8112b9651 100644 --- a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/application/CustomersServiceClient.java +++ b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/application/CustomersServiceClient.java @@ -15,7 +15,6 @@ */ package org.springframework.samples.petclinic.api.application; -import lombok.RequiredArgsConstructor; import org.springframework.samples.petclinic.api.dto.OwnerDetails; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; @@ -25,11 +24,14 @@ * @author Maciej Szarlinski */ @Component -@RequiredArgsConstructor public class CustomersServiceClient { private final WebClient.Builder webClientBuilder; + public CustomersServiceClient(WebClient.Builder webClientBuilder) { + this.webClientBuilder = webClientBuilder; + } + public Mono getOwner(final int ownerId) { return webClientBuilder.build().get() .uri("http://customers-service/owners/{ownerId}", ownerId) diff --git a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/application/VisitsServiceClient.java b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/application/VisitsServiceClient.java index d49dbf9b3..dcb222583 100644 --- a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/application/VisitsServiceClient.java +++ b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/application/VisitsServiceClient.java @@ -15,7 +15,6 @@ */ package org.springframework.samples.petclinic.api.application; -import lombok.RequiredArgsConstructor; import org.springframework.samples.petclinic.api.dto.Visits; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; @@ -29,7 +28,6 @@ * @author Maciej Szarlinski */ @Component -@RequiredArgsConstructor public class VisitsServiceClient { // Could be changed for testing purpose @@ -37,6 +35,10 @@ public class VisitsServiceClient { private final WebClient.Builder webClientBuilder; + public VisitsServiceClient(WebClient.Builder webClientBuilder) { + this.webClientBuilder = webClientBuilder; + } + public Mono getVisitsForPets(final List petIds) { return webClientBuilder.build() .get() diff --git a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/boundary/web/ApiGatewayController.java b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/boundary/web/ApiGatewayController.java index 4cacc6106..fba887553 100644 --- a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/boundary/web/ApiGatewayController.java +++ b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/boundary/web/ApiGatewayController.java @@ -15,7 +15,6 @@ */ package org.springframework.samples.petclinic.api.boundary.web; -import lombok.RequiredArgsConstructor; import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreaker; import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory; import org.springframework.samples.petclinic.api.application.CustomersServiceClient; @@ -28,14 +27,13 @@ import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; +import java.util.List; import java.util.function.Function; -import java.util.stream.Collectors; /** * @author Maciej Szarlinski */ @RestController -@RequiredArgsConstructor @RequestMapping("/api/gateway") public class ApiGatewayController { @@ -45,6 +43,14 @@ public class ApiGatewayController { private final ReactiveCircuitBreakerFactory cbFactory; + public ApiGatewayController(CustomersServiceClient customersServiceClient, + VisitsServiceClient visitsServiceClient, + ReactiveCircuitBreakerFactory cbFactory) { + this.customersServiceClient = customersServiceClient; + this.visitsServiceClient = visitsServiceClient; + this.cbFactory = cbFactory; + } + @GetMapping(value = "owners/{ownerId}") public Mono getOwnerDetails(final @PathVariable int ownerId) { return customersServiceClient.getOwner(ownerId) @@ -61,17 +67,17 @@ public Mono getOwnerDetails(final @PathVariable int ownerId) { private Function addVisitsToOwner(OwnerDetails owner) { return visits -> { - owner.getPets() - .forEach(pet -> pet.getVisits() - .addAll(visits.getItems().stream() - .filter(v -> v.getPetId() == pet.getId()) - .collect(Collectors.toList())) + owner.pets() + .forEach(pet -> pet.visits() + .addAll(visits.items().stream() + .filter(v -> v.petId() == pet.id()) + .toList()) ); return owner; }; } private Mono emptyVisitsForPets() { - return Mono.just(new Visits()); + return Mono.just(new Visits(List.of())); } } diff --git a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/OwnerDetails.java b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/OwnerDetails.java index 35391c026..18c58bf90 100644 --- a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/OwnerDetails.java +++ b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/OwnerDetails.java @@ -16,37 +16,82 @@ package org.springframework.samples.petclinic.api.dto; import com.fasterxml.jackson.annotation.JsonIgnore; -import lombok.Data; -import java.util.ArrayList; import java.util.List; -import static java.util.stream.Collectors.toList; - /** * @author Maciej Szarlinski */ -@Data -public class OwnerDetails { +public record OwnerDetails( + int id, + String firstName, + String lastName, + String address, + String city, + String telephone, + List pets) { - private int id; + @JsonIgnore + public List getPetIds() { + return pets.stream() + .map(PetDetails::id) + .toList(); + } - private String firstName; - private String lastName; + public static final class OwnerDetailsBuilder { + private int id; + private String firstName; + private String lastName; + private String address; + private String city; + private String telephone; + private List pets; - private String address; + private OwnerDetailsBuilder() { + } - private String city; + public static OwnerDetailsBuilder anOwnerDetails() { + return new OwnerDetailsBuilder(); + } - private String telephone; + public OwnerDetailsBuilder id(int id) { + this.id = id; + return this; + } - private final List pets = new ArrayList<>(); + public OwnerDetailsBuilder firstName(String firstName) { + this.firstName = firstName; + return this; + } - @JsonIgnore - public List getPetIds() { - return pets.stream() - .map(PetDetails::getId) - .collect(toList()); + public OwnerDetailsBuilder lastName(String lastName) { + this.lastName = lastName; + return this; + } + + public OwnerDetailsBuilder address(String address) { + this.address = address; + return this; + } + + public OwnerDetailsBuilder city(String city) { + this.city = city; + return this; + } + + public OwnerDetailsBuilder telephone(String telephone) { + this.telephone = telephone; + return this; + } + + public OwnerDetailsBuilder pets(List pets) { + this.pets = pets; + return this; + } + + public OwnerDetails build() { + return new OwnerDetails(id, firstName, lastName, address, city, telephone, pets); + } } } diff --git a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/PetDetails.java b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/PetDetails.java index bf064fe6f..a1bacf164 100644 --- a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/PetDetails.java +++ b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/PetDetails.java @@ -15,7 +15,6 @@ */ package org.springframework.samples.petclinic.api.dto; -import lombok.Data; import java.util.ArrayList; import java.util.List; @@ -23,17 +22,60 @@ /** * @author Maciej Szarlinski */ -@Data -public class PetDetails { +public record PetDetails( + int id, + String name, + String birthDate, + PetType type, + List visits) { - private int id; + public PetDetails { + if (visits == null) { + visits = new ArrayList<>(); + } + } - private String name; + public static final class PetDetailsBuilder { + private int id; + private String name; + private String birthDate; + private PetType type; + private List visits; - private String birthDate; + private PetDetailsBuilder() { + } - private PetType type; + public static PetDetailsBuilder aPetDetails() { + return new PetDetailsBuilder(); + } - private final List visits = new ArrayList<>(); + public PetDetailsBuilder id(int id) { + this.id = id; + return this; + } + public PetDetailsBuilder name(String name) { + this.name = name; + return this; + } + + public PetDetailsBuilder birthDate(String birthDate) { + this.birthDate = birthDate; + return this; + } + + public PetDetailsBuilder type(PetType type) { + this.type = type; + return this; + } + + public PetDetailsBuilder visits(List visits) { + this.visits = visits; + return this; + } + + public PetDetails build() { + return new PetDetails(id, name, birthDate, type, visits); + } + } } diff --git a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/PetType.java b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/PetType.java index e6b6d9e56..e6977b125 100644 --- a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/PetType.java +++ b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/PetType.java @@ -15,13 +15,8 @@ */ package org.springframework.samples.petclinic.api.dto; -import lombok.Data; - /** * @author Maciej Szarlinski */ -@Data -public class PetType { - - private String name; +public record PetType(String name) { } diff --git a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/VisitDetails.java b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/VisitDetails.java index e9f353544..da9747f5b 100644 --- a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/VisitDetails.java +++ b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/VisitDetails.java @@ -15,21 +15,12 @@ */ package org.springframework.samples.petclinic.api.dto; -import lombok.Data; -import lombok.NoArgsConstructor; - /** * @author Maciej Szarlinski */ -@Data -@NoArgsConstructor -public class VisitDetails { - - private Integer id = null; - - private Integer petId = null; - - private String date = null; - - private String description = null; +public record VisitDetails ( + Integer id, + Integer petId, + String date, + String description) { } diff --git a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/Visits.java b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/Visits.java index fb5851f1f..1ef90a0d2 100644 --- a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/Visits.java +++ b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/dto/Visits.java @@ -18,15 +18,14 @@ import java.util.ArrayList; import java.util.List; -import lombok.NoArgsConstructor; -import lombok.Value; /** * @author Maciej Szarlinski */ -@Value -public class Visits { - - private List items = new ArrayList<>(); - +public record Visits ( + List items +) { + public Visits() { + this(new ArrayList<>()); + } } diff --git a/spring-petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/application/VisitsServiceClientIntegrationTest.java b/spring-petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/application/VisitsServiceClientIntegrationTest.java index 62e74c490..3820ba9a2 100644 --- a/spring-petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/application/VisitsServiceClientIntegrationTest.java +++ b/spring-petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/application/VisitsServiceClientIntegrationTest.java @@ -49,10 +49,10 @@ void getVisitsForPets_withAvailableVisitsService() { private void assertVisitDescriptionEquals(Visits visits, int petId, String description) { - assertEquals(1, visits.getItems().size()); - assertNotNull(visits.getItems().get(0)); - assertEquals(petId, visits.getItems().get(0).getPetId()); - assertEquals(description, visits.getItems().get(0).getDescription()); + assertEquals(1, visits.items().size()); + assertNotNull(visits.items().get(0)); + assertEquals(petId, visits.items().get(0).petId()); + assertEquals(description, visits.items().get(0).description()); } private void prepareResponse(Consumer consumer) { diff --git a/spring-petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/boundary/web/ApiGatewayControllerTest.java b/spring-petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/boundary/web/ApiGatewayControllerTest.java index f7ca84d4e..566f45638 100644 --- a/spring-petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/boundary/web/ApiGatewayControllerTest.java +++ b/spring-petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/boundary/web/ApiGatewayControllerTest.java @@ -10,16 +10,15 @@ import org.springframework.context.annotation.Import; import org.springframework.samples.petclinic.api.application.CustomersServiceClient; import org.springframework.samples.petclinic.api.application.VisitsServiceClient; -import org.springframework.samples.petclinic.api.dto.OwnerDetails; -import org.springframework.samples.petclinic.api.dto.PetDetails; -import org.springframework.samples.petclinic.api.dto.VisitDetails; -import org.springframework.samples.petclinic.api.dto.Visits; +import org.springframework.samples.petclinic.api.dto.*; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; import reactor.core.publisher.Mono; import java.net.ConnectException; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; @ExtendWith(SpringExtension.class) @WebFluxTest(controllers = ApiGatewayController.class) @@ -38,32 +37,28 @@ class ApiGatewayControllerTest { @Test void getOwnerDetails_withAvailableVisitsService() { - OwnerDetails owner = new OwnerDetails(); - PetDetails cat = new PetDetails(); - cat.setId(20); - cat.setName("Garfield"); - owner.getPets().add(cat); + PetDetails cat = PetDetails.PetDetailsBuilder.aPetDetails() + .id(20) + .name("Garfield") + .visits(new ArrayList<>()) + .build(); + OwnerDetails owner = OwnerDetails.OwnerDetailsBuilder.anOwnerDetails() + .pets(List.of(cat)) + .build(); Mockito .when(customersServiceClient.getOwner(1)) .thenReturn(Mono.just(owner)); - Visits visits = new Visits(); - VisitDetails visit = new VisitDetails(); - visit.setId(300); - visit.setDescription("First visit"); - visit.setPetId(cat.getId()); - visits.getItems().add(visit); + VisitDetails visit = new VisitDetails(300, cat.id(), null, "First visit"); + Visits visits = new Visits(List.of(visit)); Mockito - .when(visitsServiceClient.getVisitsForPets(Collections.singletonList(cat.getId()))) + .when(visitsServiceClient.getVisitsForPets(Collections.singletonList(cat.id()))) .thenReturn(Mono.just(visits)); client.get() .uri("/api/gateway/owners/1") .exchange() .expectStatus().isOk() - //.expectBody(String.class) - //.consumeWith(response -> - // Assertions.assertThat(response.getResponseBody()).isEqualTo("Garfield")); .expectBody() .jsonPath("$.pets[0].name").isEqualTo("Garfield") .jsonPath("$.pets[0].visits[0].description").isEqualTo("First visit"); @@ -74,17 +69,20 @@ void getOwnerDetails_withAvailableVisitsService() { */ @Test void getOwnerDetails_withServiceError() { - OwnerDetails owner = new OwnerDetails(); - PetDetails cat = new PetDetails(); - cat.setId(20); - cat.setName("Garfield"); - owner.getPets().add(cat); + PetDetails cat = PetDetails.PetDetailsBuilder.aPetDetails() + .id(20) + .name("Garfield") + .visits(new ArrayList<>()) + .build(); + OwnerDetails owner = OwnerDetails.OwnerDetailsBuilder.anOwnerDetails() + .pets(List.of(cat)) + .build(); Mockito .when(customersServiceClient.getOwner(1)) .thenReturn(Mono.just(owner)); Mockito - .when(visitsServiceClient.getVisitsForPets(Collections.singletonList(cat.getId()))) + .when(visitsServiceClient.getVisitsForPets(Collections.singletonList(cat.id()))) .thenReturn(Mono.error(new ConnectException("Simulate error"))); client.get() diff --git a/spring-petclinic-customers-service/pom.xml b/spring-petclinic-customers-service/pom.xml index 0817f826f..2edf67460 100644 --- a/spring-petclinic-customers-service/pom.xml +++ b/spring-petclinic-customers-service/pom.xml @@ -65,11 +65,6 @@ org.jolokia jolokia-core - - org.projectlombok - lombok - provided - io.micrometer micrometer-registry-prometheus diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Owner.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Owner.java index f1919ee96..2a7adc7d2 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Owner.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Owner.java @@ -15,30 +15,15 @@ */ package org.springframework.samples.petclinic.customers.model; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import jakarta.persistence.CascadeType; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Table; +import jakarta.persistence.*; import jakarta.validation.constraints.Digits; import jakarta.validation.constraints.NotBlank; - -import lombok.Getter; -import lombok.Setter; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; import org.springframework.core.style.ToStringCreator; +import java.util.*; + /** * Simple JavaBean domain object representing an owner. * @@ -55,35 +40,24 @@ public class Owner { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Getter private Integer id; - @Getter - @Setter @Column(name = "first_name") @NotBlank private String firstName; - @Getter - @Setter @Column(name = "last_name") @NotBlank private String lastName; - @Getter - @Setter @Column(name = "address") @NotBlank private String address; - @Getter - @Setter @Column(name = "city") @NotBlank private String city; - @Getter - @Setter @Column(name = "telephone") @NotBlank @Digits(fraction = 0, integer = 12) @@ -121,4 +95,48 @@ public String toString() { .append("telephone", this.telephone) .toString(); } + + public Integer getId() { + return this.id; + } + + public String getFirstName() { + return this.firstName; + } + + public String getLastName() { + return this.lastName; + } + + public String getAddress() { + return this.address; + } + + public String getCity() { + return this.city; + } + + public String getTelephone() { + return this.telephone; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public void setAddress(String address) { + this.address = address; + } + + public void setCity(String city) { + this.city = city; + } + + public void setTelephone(String telephone) { + this.telephone = telephone; + } } diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Pet.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Pet.java index 2cb702c88..029b80494 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Pet.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Pet.java @@ -15,23 +15,13 @@ */ package org.springframework.samples.petclinic.customers.model; -import java.util.Date; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.Table; -import jakarta.persistence.Temporal; -import jakarta.persistence.TemporalType; - import com.fasterxml.jackson.annotation.JsonIgnore; -import lombok.Data; +import jakarta.persistence.*; import org.springframework.core.style.ToStringCreator; +import java.util.Date; +import java.util.Objects; + /** * Simple business object representing a pet. * @@ -41,7 +31,6 @@ * @author Maciej Szarlinski * @author Ramazan Sakin */ -@Data @Entity @Table(name = "pets") public class Pet { @@ -77,4 +66,59 @@ public String toString() { .toString(); } + public Integer getId() { + return this.id; + } + + public String getName() { + return this.name; + } + + public Date getBirthDate() { + return this.birthDate; + } + + public PetType getType() { + return this.type; + } + + public Owner getOwner() { + return this.owner; + } + + public void setId(Integer id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public void setBirthDate(Date birthDate) { + this.birthDate = birthDate; + } + + public void setType(PetType type) { + this.type = type; + } + + public void setOwner(Owner owner) { + this.owner = owner; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + Pet pet = (Pet) o; + return Objects.equals(id, pet.id) + && Objects.equals(name, pet.name) + && Objects.equals(birthDate, pet.birthDate) + && Objects.equals(type, pet.type) + && Objects.equals(owner, pet.owner); + } + + @Override + public int hashCode() { + return Objects.hash(id, name, birthDate, type, owner); + } } diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetType.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetType.java index 80a29c134..f390ef8d3 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetType.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetType.java @@ -15,15 +15,7 @@ */ package org.springframework.samples.petclinic.customers.model; -import lombok.Getter; -import lombok.Setter; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.Table; +import jakarta.persistence.*; /** * @author Juergen Hoeller @@ -36,12 +28,24 @@ public class PetType { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Getter - @Setter private Integer id; - @Getter - @Setter @Column(name = "name") private String name; + + public Integer getId() { + return this.id; + } + + public String getName() { + return this.name; + } + + public void setId(Integer id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } } diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/OwnerResource.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/OwnerResource.java index 1a3a2d70d..5b5a5315d 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/OwnerResource.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/OwnerResource.java @@ -18,8 +18,8 @@ import io.micrometer.core.annotation.Timed; import jakarta.validation.Valid; import jakarta.validation.constraints.Min; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.samples.petclinic.customers.web.mapper.OwnerEntityMapper; import org.springframework.samples.petclinic.customers.model.Owner; @@ -39,13 +39,18 @@ @RequestMapping("/owners") @RestController @Timed("petclinic.owner") -@RequiredArgsConstructor -@Slf4j class OwnerResource { + private static final Logger log = LoggerFactory.getLogger(OwnerResource.class); + private final OwnerRepository ownerRepository; private final OwnerEntityMapper ownerEntityMapper; + OwnerResource(OwnerRepository ownerRepository, OwnerEntityMapper ownerEntityMapper) { + this.ownerRepository = ownerRepository; + this.ownerEntityMapper = ownerEntityMapper; + } + /** * Create Owner */ diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java index 82241b363..204c1a7af 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java @@ -17,8 +17,8 @@ import io.micrometer.core.annotation.Timed; import jakarta.validation.constraints.Min; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.samples.petclinic.customers.model.*; import org.springframework.web.bind.annotation.*; @@ -34,13 +34,17 @@ */ @RestController @Timed("petclinic.pet") -@RequiredArgsConstructor -@Slf4j class PetResource { + private static final Logger log = LoggerFactory.getLogger(PetResource.class); + private final PetRepository petRepository; private final OwnerRepository ownerRepository; + PetResource(PetRepository petRepository, OwnerRepository ownerRepository) { + this.petRepository = petRepository; + this.ownerRepository = ownerRepository; + } @GetMapping("/petTypes") public List getPetTypes() { diff --git a/spring-petclinic-vets-service/pom.xml b/spring-petclinic-vets-service/pom.xml index 0d7dd9ce7..8da89402b 100644 --- a/spring-petclinic-vets-service/pom.xml +++ b/spring-petclinic-vets-service/pom.xml @@ -59,11 +59,6 @@ - - org.projectlombok - lombok - provided - javax.cache cache-api diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Specialty.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Specialty.java index 529b5a496..eeeea282e 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Specialty.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Specialty.java @@ -15,15 +15,7 @@ */ package org.springframework.samples.petclinic.vets.model; -import lombok.Getter; -import lombok.Setter; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.Table; +import jakarta.persistence.*; /** * Models a {@link Vet Vet's} specialty (for example, dentistry). @@ -37,12 +29,20 @@ public class Specialty { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Getter private Integer id; - @Getter - @Setter @Column(name = "name") private String name; + public Integer getId() { + return this.id; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } } diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Vet.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Vet.java index 2cec7f89d..150b19abf 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Vet.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Vet.java @@ -15,30 +15,14 @@ */ package org.springframework.samples.petclinic.vets.model; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.JoinTable; -import jakarta.persistence.ManyToMany; -import jakarta.persistence.Table; +import jakarta.persistence.*; import jakarta.validation.constraints.NotBlank; import jakarta.xml.bind.annotation.XmlElement; - -import lombok.Getter; -import lombok.Setter; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; +import java.util.*; + /** * Simple JavaBean domain object representing a veterinarian. * @@ -55,20 +39,14 @@ public class Vet { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Getter - @Setter private Integer id; @Column(name = "first_name") @NotBlank - @Getter - @Setter private String firstName; @Column(name = "last_name") @NotBlank - @Getter - @Setter private String lastName; @ManyToMany(fetch = FetchType.EAGER) @@ -98,4 +76,27 @@ public void addSpecialty(Specialty specialty) { getSpecialtiesInternal().add(specialty); } + public Integer getId() { + return this.id; + } + + public String getFirstName() { + return this.firstName; + } + + public String getLastName() { + return this.lastName; + } + + public void setId(Integer id) { + this.id = id; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } } diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/VetsProperties.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/VetsProperties.java index 017bffe71..f369a6f14 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/VetsProperties.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/VetsProperties.java @@ -15,8 +15,6 @@ */ package org.springframework.samples.petclinic.vets.system; -import lombok.Data; - import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -24,17 +22,13 @@ * * @author Maciej Szarlinski */ -@Data @ConfigurationProperties(prefix = "vets") -public class VetsProperties { - - private Cache cache; - - @Data - public static class Cache { - - private int ttl; - - private int heapSize; +public record VetsProperties( + Cache cache +) { + public record Cache( + int ttl, + int heapSize + ) { } } diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/VetResource.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/VetResource.java index eb5116663..09e4b19f8 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/VetResource.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/VetResource.java @@ -15,8 +15,6 @@ */ package org.springframework.samples.petclinic.vets.web; -import lombok.RequiredArgsConstructor; - import java.util.List; import org.springframework.cache.annotation.Cacheable; @@ -35,11 +33,14 @@ */ @RequestMapping("/vets") @RestController -@RequiredArgsConstructor class VetResource { private final VetRepository vetRepository; + VetResource(VetRepository vetRepository) { + this.vetRepository = vetRepository; + } + @GetMapping @Cacheable("vets") public List showResourcesVetList() { diff --git a/spring-petclinic-visits-service/pom.xml b/spring-petclinic-visits-service/pom.xml index 338ff0301..2b4b3f18e 100644 --- a/spring-petclinic-visits-service/pom.xml +++ b/spring-petclinic-visits-service/pom.xml @@ -50,11 +50,6 @@ - - org.projectlombok - lombok - provided - org.hsqldb hsqldb diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/Visit.java b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/Visit.java index ba24c1ce7..ef0fa1a2c 100644 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/Visit.java +++ b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/Visit.java @@ -16,20 +16,9 @@ package org.springframework.samples.petclinic.visits.model; import com.fasterxml.jackson.annotation.JsonFormat; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.Table; -import jakarta.persistence.Temporal; -import jakarta.persistence.TemporalType; +import jakarta.persistence.*; import jakarta.validation.constraints.Size; + import java.util.Date; /** @@ -41,17 +30,12 @@ */ @Entity @Table(name = "visits") -@Builder(builderMethodName = "visit") -@Data -@NoArgsConstructor -@AllArgsConstructor public class Visit { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; - @Builder.Default @Column(name = "visit_date") @Temporal(TemporalType.TIMESTAMP) @JsonFormat(pattern = "yyyy-MM-dd") @@ -64,4 +48,79 @@ public class Visit { @Column(name = "pet_id") private int petId; + public Integer getId() { + return this.id; + } + + public Date getDate() { + return this.date; + } + + public String getDescription() { + return this.description; + } + + public int getPetId() { + return this.petId; + } + + public void setId(Integer id) { + this.id = id; + } + + public void setDate(Date date) { + this.date = date; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setPetId(int petId) { + this.petId = petId; + } + + + public static final class VisitBuilder { + private Integer id; + private Date date; + private @Size(max = 8192) String description; + private int petId; + + private VisitBuilder() { + } + + public static VisitBuilder aVisit() { + return new VisitBuilder(); + } + + public VisitBuilder id(Integer id) { + this.id = id; + return this; + } + + public VisitBuilder date(Date date) { + this.date = date; + return this; + } + + public VisitBuilder description(String description) { + this.description = description; + return this; + } + + public VisitBuilder petId(int petId) { + this.petId = petId; + return this; + } + + public Visit build() { + Visit visit = new Visit(); + visit.setId(id); + visit.setDate(date); + visit.setDescription(description); + visit.setPetId(petId); + return visit; + } + } } diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/web/VisitResource.java b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/web/VisitResource.java index 1115dd7c0..07f42ba4c 100644 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/web/VisitResource.java +++ b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/web/VisitResource.java @@ -20,9 +20,8 @@ import jakarta.validation.constraints.Min; import io.micrometer.core.annotation.Timed; -import lombok.RequiredArgsConstructor; -import lombok.Value; -import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.samples.petclinic.visits.model.Visit; import org.springframework.samples.petclinic.visits.model.VisitRepository; @@ -43,13 +42,17 @@ * @author Ramazan Sakin */ @RestController -@RequiredArgsConstructor -@Slf4j @Timed("petclinic.visit") class VisitResource { + private static final Logger log = LoggerFactory.getLogger(VisitResource.class); + private final VisitRepository visitRepository; + VisitResource(VisitRepository visitRepository) { + this.visitRepository = visitRepository; + } + @PostMapping("owners/*/pets/{petId}/visits") @ResponseStatus(HttpStatus.CREATED) public Visit create( @@ -72,8 +75,8 @@ public Visits read(@RequestParam("petId") List petIds) { return new Visits(byPetIdIn); } - @Value - static class Visits { - List items; + record Visits( + List items + ) { } } diff --git a/spring-petclinic-visits-service/src/test/java/org/springframework/samples/petclinic/visits/web/VisitResourceTest.java b/spring-petclinic-visits-service/src/test/java/org/springframework/samples/petclinic/visits/web/VisitResourceTest.java index 25d33f24b..82392b59b 100644 --- a/spring-petclinic-visits-service/src/test/java/org/springframework/samples/petclinic/visits/web/VisitResourceTest.java +++ b/spring-petclinic-visits-service/src/test/java/org/springframework/samples/petclinic/visits/web/VisitResourceTest.java @@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.samples.petclinic.visits.model.Visit; import org.springframework.samples.petclinic.visits.model.VisitRepository; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -13,7 +14,6 @@ import static java.util.Arrays.asList; import static org.mockito.BDDMockito.given; -import static org.springframework.samples.petclinic.visits.model.Visit.visit; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -34,15 +34,15 @@ void shouldFetchVisits() throws Exception { given(visitRepository.findByPetIdIn(asList(111, 222))) .willReturn( asList( - visit() + Visit.VisitBuilder.aVisit() .id(1) .petId(111) .build(), - visit() + Visit.VisitBuilder.aVisit() .id(2) .petId(222) .build(), - visit() + Visit.VisitBuilder.aVisit() .id(3) .petId(222) .build()