Skip to content

Commit

Permalink
feat : applying spring boot best practices (#1612)
Browse files Browse the repository at this point in the history
* feat : applying spring boot best practices

* fix spotless format issue
  • Loading branch information
rajadilipkolli authored Jan 5, 2025
1 parent 44ac4cc commit f216c4b
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: boot-jpa-keyset-pagination-data-jpa
name: boot-keyset-pagination-window-jpa

on:
push:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ public Mono<ResponseEntity<GenericMessage>> 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class RestClientConfiguration {
}

@Bean
public HttpComponentsClientHttpRequestFactoryBuilder
httpComponentsClientHttpRequestFactoryBuilder() {
HttpComponentsClientHttpRequestFactoryBuilder httpComponentsClientHttpRequestFactoryBuilder() {

SSLContext sslContext;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ DataSource dataSource() throws IllegalArgumentException, NamingException {
}

@Bean
public TomcatServletWebServerFactory tomcatFactory() {
TomcatServletWebServerFactory tomcatFactory() {
return new TomcatServletWebServerFactory() {
@Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> for type safety
Comparable<Object> lowerBound =
(Comparable<Object>) rangeValues.get(0);
(Comparable<Object>) rangeValues.getFirst();
Comparable<Object> upperBound =
(Comparable<Object>) rangeValues.get(1);
return cb.between(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ class AnimalController {

@GetMapping
PagedResult<AnimalResponse> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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<Movie> movies = Flux.just(
new Movie(null, "DJ Tillu"),
new Movie(null, "Tillu Square"),
Expand Down

0 comments on commit f216c4b

Please sign in to comment.