From f216c4b0d3c107e9b697b12a71d38ff9767e548d Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Sun, 5 Jan 2025 14:32:03 +0530 Subject: [PATCH] feat : applying spring boot best practices (#1612) * feat : applying spring boot best practices * fix spotless format issue --- ...yml => boot-jpa-keyset-pagination-window-jpa.yml} | 2 +- .../mongoes/web/controller/RestaurantController.java | 12 +++++++----- .../mongoes/web/service/RestaurantService.java | 2 +- .../src/main/resources/application.properties | 3 ++- .../controller/StrategyController.java | 2 +- .../rest/proxy/config/RestClientConfiguration.java | 3 +-- .../common/ContainersConfiguration.java | 2 +- .../java/com/example/jndi/config/TomcatConfigs.java | 2 +- .../keysetpagination/utils/QueryOperatorHandler.java | 4 ++-- .../boot-data-window-pagination/Dockerfile | 2 +- .../keysetpagination/model/query/SearchCriteria.java | 4 ++-- .../keysetpagination/model/query/SortRequest.java | 2 +- .../web/controllers/AnimalController.java | 12 ++++-------- .../web/controllers/AnimalControllerIT.java | 8 ++++---- .../src/main/resources/application.properties | 3 ++- .../java/com/example/cache/config/Initializer.java | 2 +- 16 files changed, 32 insertions(+), 33 deletions(-) rename .github/workflows/{boot-jpa-keyset-pagination-data-jpa.yml => boot-jpa-keyset-pagination-window-jpa.yml} (95%) diff --git a/.github/workflows/boot-jpa-keyset-pagination-data-jpa.yml b/.github/workflows/boot-jpa-keyset-pagination-window-jpa.yml similarity index 95% rename from .github/workflows/boot-jpa-keyset-pagination-data-jpa.yml rename to .github/workflows/boot-jpa-keyset-pagination-window-jpa.yml index 67d2fe804..bf6d5d162 100644 --- a/.github/workflows/boot-jpa-keyset-pagination-data-jpa.yml +++ b/.github/workflows/boot-jpa-keyset-pagination-window-jpa.yml @@ -1,4 +1,4 @@ -name: boot-jpa-keyset-pagination-data-jpa +name: boot-keyset-pagination-window-jpa on: push: diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/RestaurantController.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/RestaurantController.java index caede0a4c..9a4e4282e 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/RestaurantController.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/RestaurantController.java @@ -87,11 +87,13 @@ public Mono> createRestaurant( restaurant -> ResponseEntity.created( URI.create( - String.format( - "/api/restaurant/name/%s", - URLEncoder.encode( - restaurantRequest.name(), - StandardCharsets.UTF_8)))) + "/api/restaurant/name/%s" + .formatted( + URLEncoder.encode( + restaurantRequest + .name(), + StandardCharsets + .UTF_8)))) .body( new GenericMessage( "restaurant with name %s created" diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/RestaurantService.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/RestaurantService.java index 76a910c8f..54402060e 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/RestaurantService.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/RestaurantService.java @@ -101,7 +101,7 @@ private Restaurant documentToRestaurant(Document document) { address.setStreet(addressDoc.get("street", String.class)); address.setZipcode(Integer.valueOf(addressDoc.get("zipcode", String.class))); List coord = addressDoc.getList("coord", Double.class); - Point geoJsonPoint = new Point(coord.get(0), coord.get(1)); + Point geoJsonPoint = new Point(coord.getFirst(), coord.get(1)); address.setLocation(geoJsonPoint); restaurant.setAddress(address); diff --git a/boot-mongodb-elasticsearch/src/main/resources/application.properties b/boot-mongodb-elasticsearch/src/main/resources/application.properties index 21a9daee6..5e63e1d73 100644 --- a/boot-mongodb-elasticsearch/src/main/resources/application.properties +++ b/boot-mongodb-elasticsearch/src/main/resources/application.properties @@ -26,7 +26,8 @@ spring.data.mongodb.uri=mongodb://localhost:27017/mongoes?replicaSet=rs0&readPre spring.elasticsearch.uris=localhost:9200 spring.elasticsearch.socket-timeout=10s -spring.elasticsearch.webclient.max-in-memory-size=512MB +# This property is deprecated: Reactive Elasticsearch client no longer uses WebClient. +# spring.elasticsearch.webclient.max-in-memory-size=512MB spring.threads.virtual.enabled=true spring.testcontainers.beans.startup=parallel diff --git a/boot-strategy-plugin/src/main/java/com/example/plugin/strategyplugin/controller/StrategyController.java b/boot-strategy-plugin/src/main/java/com/example/plugin/strategyplugin/controller/StrategyController.java index ccfa5cd4c..03c1b814c 100644 --- a/boot-strategy-plugin/src/main/java/com/example/plugin/strategyplugin/controller/StrategyController.java +++ b/boot-strategy-plugin/src/main/java/com/example/plugin/strategyplugin/controller/StrategyController.java @@ -20,7 +20,7 @@ class StrategyController { } @GetMapping("/fetch") - GenericDTO fetchData(@RequestParam("type") String type) { + GenericDTO fetchData(@RequestParam String type) { logger.info("Requested to fetch of type :{}", type); return this.strategyService.fetchData(type); } diff --git a/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/config/RestClientConfiguration.java b/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/config/RestClientConfiguration.java index 3dcca2932..c0a597828 100644 --- a/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/config/RestClientConfiguration.java +++ b/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/config/RestClientConfiguration.java @@ -42,8 +42,7 @@ class RestClientConfiguration { } @Bean - public HttpComponentsClientHttpRequestFactoryBuilder - httpComponentsClientHttpRequestFactoryBuilder() { + HttpComponentsClientHttpRequestFactoryBuilder httpComponentsClientHttpRequestFactoryBuilder() { SSLContext sslContext; try { diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java index 03bf36208..4865e7e76 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java @@ -26,7 +26,7 @@ PostgreSQLContainer postgreSQLContainer() { } @Bean - public DynamicPropertyRegistrar databaseProperties( + DynamicPropertyRegistrar databaseProperties( MySQLContainer mySQLContainer, PostgreSQLContainer postgreSQLContainer) { return (properties) -> { // Connect our Spring application to our Testcontainers instances diff --git a/jpa/boot-jndi-sample/src/main/java/com/example/jndi/config/TomcatConfigs.java b/jpa/boot-jndi-sample/src/main/java/com/example/jndi/config/TomcatConfigs.java index 29628f01e..d157514ee 100644 --- a/jpa/boot-jndi-sample/src/main/java/com/example/jndi/config/TomcatConfigs.java +++ b/jpa/boot-jndi-sample/src/main/java/com/example/jndi/config/TomcatConfigs.java @@ -33,7 +33,7 @@ DataSource dataSource() throws IllegalArgumentException, NamingException { } @Bean - public TomcatServletWebServerFactory tomcatFactory() { + TomcatServletWebServerFactory tomcatFactory() { return new TomcatServletWebServerFactory() { @Override protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) { diff --git a/jpa/keyset-pagination/blaze-persistence/src/main/java/com/example/keysetpagination/utils/QueryOperatorHandler.java b/jpa/keyset-pagination/blaze-persistence/src/main/java/com/example/keysetpagination/utils/QueryOperatorHandler.java index 3b0de148f..d892bc39c 100644 --- a/jpa/keyset-pagination/blaze-persistence/src/main/java/com/example/keysetpagination/utils/QueryOperatorHandler.java +++ b/jpa/keyset-pagination/blaze-persistence/src/main/java/com/example/keysetpagination/utils/QueryOperatorHandler.java @@ -77,14 +77,14 @@ public class QueryOperatorHandler { throw new IllegalArgumentException( "BETWEEN operator requires exactly 2 values"); } - if (!(rangeValues.get(0) instanceof Comparable) + if (!(rangeValues.getFirst() instanceof Comparable) || !(rangeValues.get(1) instanceof Comparable)) { throw new IllegalArgumentException( "Values for BETWEEN operator must be Comparable"); } // Casting to Comparable for type safety Comparable lowerBound = - (Comparable) rangeValues.get(0); + (Comparable) rangeValues.getFirst(); Comparable upperBound = (Comparable) rangeValues.get(1); return cb.between( diff --git a/jpa/keyset-pagination/boot-data-window-pagination/Dockerfile b/jpa/keyset-pagination/boot-data-window-pagination/Dockerfile index b18ac367e..f363febec 100644 --- a/jpa/keyset-pagination/boot-data-window-pagination/Dockerfile +++ b/jpa/keyset-pagination/boot-data-window-pagination/Dockerfile @@ -11,4 +11,4 @@ COPY --from=builder application/dependencies/ ./ COPY --from=builder application/spring-boot-loader/ ./ COPY --from=builder application/snapshot-dependencies/ ./ COPY --from=builder application/application/ ./ -ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"] +ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"] diff --git a/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/model/query/SearchCriteria.java b/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/model/query/SearchCriteria.java index 64ead6ca1..e0821545d 100644 --- a/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/model/query/SearchCriteria.java +++ b/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/model/query/SearchCriteria.java @@ -124,7 +124,7 @@ && getQueryOperator() != QueryOperator.NOT_IN) { throw new IllegalArgumentException("BETWEEN operator requires exactly two values"); } yield criteriaBuilder.between( - root.get(getField()), (Comparable) typedValues.get(0), (Comparable) typedValues.get(1)); + root.get(getField()), (Comparable) typedValues.getFirst(), (Comparable) typedValues.get(1)); } case IN -> root.get(getField()).in(typedValues); case NOT_IN -> criteriaBuilder.not(root.get(getField()).in(typedValues)); @@ -177,7 +177,7 @@ private Predicate combinePredicates( .map(predicateFunction) .reduce(combiner::apply) .orElseThrow(() -> new IllegalArgumentException( - String.format("No predicates could be generated from values: %s", values))); + "No predicates could be generated from values: %s".formatted(values))); } @Override diff --git a/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/model/query/SortRequest.java b/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/model/query/SortRequest.java index 610ff2649..e2fbe6086 100644 --- a/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/model/query/SortRequest.java +++ b/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/model/query/SortRequest.java @@ -25,7 +25,7 @@ public void setDirection(String direction) { @Override public String toString() { - return String.format("SortRequest{field='%s', direction='%s'}", field, direction); + return "SortRequest{field='%s', direction='%s'}".formatted(field, direction); } @Override diff --git a/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/web/controllers/AnimalController.java b/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/web/controllers/AnimalController.java index 39b76502b..49d355c3b 100644 --- a/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/web/controllers/AnimalController.java +++ b/jpa/keyset-pagination/boot-data-window-pagination/src/main/java/com/example/keysetpagination/web/controllers/AnimalController.java @@ -44,14 +44,10 @@ class AnimalController { @GetMapping PagedResult getAllAnimals( - @RequestParam(value = "pageNo", defaultValue = AppConstants.DEFAULT_PAGE_NUMBER, required = false) - int pageNo, - @RequestParam(value = "pageSize", defaultValue = AppConstants.DEFAULT_PAGE_SIZE, required = false) - int pageSize, - @RequestParam(value = "sortBy", defaultValue = AppConstants.DEFAULT_SORT_BY, required = false) - String sortBy, - @RequestParam(value = "sortDir", defaultValue = AppConstants.DEFAULT_SORT_DIRECTION, required = false) - String sortDir) { + @RequestParam(defaultValue = AppConstants.DEFAULT_PAGE_NUMBER, required = false) int pageNo, + @RequestParam(defaultValue = AppConstants.DEFAULT_PAGE_SIZE, required = false) int pageSize, + @RequestParam(defaultValue = AppConstants.DEFAULT_SORT_BY, required = false) String sortBy, + @RequestParam(defaultValue = AppConstants.DEFAULT_SORT_DIRECTION, required = false) String sortDir) { FindAnimalsQuery findAnimalsQuery = new FindAnimalsQuery(pageNo, pageSize, sortBy, sortDir); return animalService.findAllAnimals(findAnimalsQuery); } diff --git a/jpa/keyset-pagination/boot-data-window-pagination/src/test/java/com/example/keysetpagination/web/controllers/AnimalControllerIT.java b/jpa/keyset-pagination/boot-data-window-pagination/src/test/java/com/example/keysetpagination/web/controllers/AnimalControllerIT.java index 6b15b8d38..f776064fd 100644 --- a/jpa/keyset-pagination/boot-data-window-pagination/src/test/java/com/example/keysetpagination/web/controllers/AnimalControllerIT.java +++ b/jpa/keyset-pagination/boot-data-window-pagination/src/test/java/com/example/keysetpagination/web/controllers/AnimalControllerIT.java @@ -521,13 +521,13 @@ void shouldReturnResultForEndsWithOperator() throws Exception { @Test void shouldReturnResultForBetweenOperator() throws Exception { // Since 'Animal' doesn't have a numeric field, we'll use 'id' for BETWEEN operator - Long minId = animalList.get(0).getId(); + Long minId = animalList.getFirst().getId(); Long maxId = animalList.get(4).getId(); this.mockMvc .perform(post("/api/animals/search") .contentType(MediaType.APPLICATION_JSON) - .content(String.format( + .content( """ { "searchCriteriaList": [ @@ -541,8 +541,8 @@ void shouldReturnResultForBetweenOperator() throws Exception { } ] } - """, - minId, maxId))) + """ + .formatted(minId, maxId))) .andExpect(status().isOk()) .andExpect(jsonPath("$.content.size()", is(5))) // Animals with IDs between minId and maxId .andExpect(jsonPath("$.last", is(true))); diff --git a/open-api-spring-boot/src/main/resources/application.properties b/open-api-spring-boot/src/main/resources/application.properties index 44b1675ca..2b1606aa9 100644 --- a/open-api-spring-boot/src/main/resources/application.properties +++ b/open-api-spring-boot/src/main/resources/application.properties @@ -6,4 +6,5 @@ server.port=8080 #openapi.springBootJpaCustomsequence.base-path=http://localhost:8081 spring.jackson.date-format=org.openapitools.RFC3339DateFormat -spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false +spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false +spring.threads.virtual.enabled=true diff --git a/r2dbc/boot-r2dbc-reactive-cache/src/main/java/com/example/cache/config/Initializer.java b/r2dbc/boot-r2dbc-reactive-cache/src/main/java/com/example/cache/config/Initializer.java index 24054e5f6..acf737d32 100644 --- a/r2dbc/boot-r2dbc-reactive-cache/src/main/java/com/example/cache/config/Initializer.java +++ b/r2dbc/boot-r2dbc-reactive-cache/src/main/java/com/example/cache/config/Initializer.java @@ -18,7 +18,7 @@ class Initializer { private static final Logger log = LoggerFactory.getLogger(Initializer.class); @Bean - public ApplicationRunner saveMovies(MovieService movieService) { + ApplicationRunner saveMovies(MovieService movieService) { Flux movies = Flux.just( new Movie(null, "DJ Tillu"), new Movie(null, "Tillu Square"),