From be49cb4cb8038f51aef1467ef1ddd891ccc5ec25 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Mon, 19 Aug 2024 15:32:56 +0000 Subject: [PATCH] polish --- .../bootbatchjpa/config/BatchConfig.java | 7 ++++- .../config/GlobalExceptionHandler.java | 2 +- .../bootbatchjpa/config/Initializer.java | 2 +- .../bootbatchjpa/config/SwaggerConfig.java | 2 +- .../bootbatchjpa/config/WebMvcConfig.java | 26 +++++++++++-------- .../web/controllers/CustomerController.java | 19 +++++++------- .../web/controllers/JobInvokerController.java | 15 +++++++---- .../common/TestContainersConfig.java | 2 +- 8 files changed, 45 insertions(+), 30 deletions(-) diff --git a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/BatchConfig.java b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/BatchConfig.java index 02297243f..e5dab9b94 100644 --- a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/BatchConfig.java +++ b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/BatchConfig.java @@ -9,7 +9,12 @@ import java.util.Map; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.batch.core.*; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobExecutionListener; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.StepScope; import org.springframework.batch.core.job.builder.JobBuilder; diff --git a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/GlobalExceptionHandler.java b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/GlobalExceptionHandler.java index 583c50c1f..9e1b3758e 100644 --- a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/GlobalExceptionHandler.java +++ b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/GlobalExceptionHandler.java @@ -16,7 +16,7 @@ @Order(Ordered.HIGHEST_PRECEDENCE) @ControllerAdvice -public class GlobalExceptionHandler { +class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) diff --git a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/Initializer.java b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/Initializer.java index a481a6dde..528dfb399 100644 --- a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/Initializer.java +++ b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/Initializer.java @@ -14,7 +14,7 @@ @Component @RequiredArgsConstructor @Slf4j -public class Initializer implements CommandLineRunner { +class Initializer implements CommandLineRunner { private final CustomerRepository customerRepository; diff --git a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/SwaggerConfig.java b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/SwaggerConfig.java index 46dc46766..83908c1ba 100644 --- a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/SwaggerConfig.java +++ b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/SwaggerConfig.java @@ -9,4 +9,4 @@ @OpenAPIDefinition( info = @Info(title = "batch-boot-jpa", version = "v1"), servers = @Server(url = "/")) -public class SwaggerConfig {} +class SwaggerConfig {} diff --git a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/WebMvcConfig.java b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/WebMvcConfig.java index bb667beb6..86205d192 100644 --- a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/WebMvcConfig.java +++ b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/config/WebMvcConfig.java @@ -1,21 +1,25 @@ package com.example.bootbatchjpa.config; -import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; +import org.springframework.lang.NonNull; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -@Configuration -@RequiredArgsConstructor -public class WebMvcConfig implements WebMvcConfigurer { - private final ApplicationProperties properties; +@Configuration(proxyBeanMethods = false) +class WebMvcConfig implements WebMvcConfigurer { + private final ApplicationProperties applicationProperties; + + WebMvcConfig(ApplicationProperties applicationProperties) { + this.applicationProperties = applicationProperties; + } @Override - public void addCorsMappings(CorsRegistry registry) { - registry.addMapping(properties.getCors().getPathPattern()) - .allowedMethods(properties.getCors().getAllowedMethods()) - .allowedHeaders(properties.getCors().getAllowedHeaders()) - .allowedOriginPatterns(properties.getCors().getAllowedOriginPatterns()) - .allowCredentials(properties.getCors().isAllowCredentials()); + public void addCorsMappings(@NonNull CorsRegistry registry) { + ApplicationProperties.Cors propertiesCors = applicationProperties.getCors(); + registry.addMapping(propertiesCors.getPathPattern()) + .allowedMethods(propertiesCors.getAllowedMethods()) + .allowedHeaders(propertiesCors.getAllowedHeaders()) + .allowedOriginPatterns(propertiesCors.getAllowedOriginPatterns()) + .allowCredentials(propertiesCors.isAllowCredentials()); } } diff --git a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/web/controllers/CustomerController.java b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/web/controllers/CustomerController.java index 85c519d2a..9bed924d3 100644 --- a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/web/controllers/CustomerController.java +++ b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/web/controllers/CustomerController.java @@ -4,7 +4,6 @@ import com.example.bootbatchjpa.model.response.PagedResult; import com.example.bootbatchjpa.services.CustomerService; import com.example.bootbatchjpa.utils.AppConstants; -import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; @@ -21,13 +20,16 @@ @RestController @RequestMapping("/api/customers") -@RequiredArgsConstructor -public class CustomerController { +class CustomerController { private final CustomerService customerService; + CustomerController(CustomerService customerService) { + this.customerService = customerService; + } + @GetMapping - public PagedResult getAllCustomers( + PagedResult getAllCustomers( @RequestParam(defaultValue = AppConstants.DEFAULT_PAGE_NUMBER, required = false) int pageNo, @RequestParam(defaultValue = AppConstants.DEFAULT_PAGE_SIZE, required = false) @@ -40,7 +42,7 @@ public PagedResult getAllCustomers( } @GetMapping("/{id}") - public ResponseEntity getCustomerById(@PathVariable Long id) { + ResponseEntity getCustomerById(@PathVariable Long id) { return customerService .findCustomerById(id) .map(ResponseEntity::ok) @@ -49,13 +51,12 @@ public ResponseEntity getCustomerById(@PathVariable Long id) { @PostMapping @ResponseStatus(HttpStatus.CREATED) - public Customer createCustomer(@RequestBody @Validated Customer customer) { + Customer createCustomer(@RequestBody @Validated Customer customer) { return customerService.saveCustomer(customer); } @PutMapping("/{id}") - public ResponseEntity updateCustomer( - @PathVariable Long id, @RequestBody Customer customer) { + ResponseEntity updateCustomer(@PathVariable Long id, @RequestBody Customer customer) { return customerService .findCustomerById(id) .map( @@ -67,7 +68,7 @@ public ResponseEntity updateCustomer( } @DeleteMapping("/{id}") - public ResponseEntity deleteCustomer(@PathVariable Long id) { + ResponseEntity deleteCustomer(@PathVariable Long id) { return customerService .findCustomerById(id) .map( diff --git a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/web/controllers/JobInvokerController.java b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/web/controllers/JobInvokerController.java index 6d2ef1f73..86caa0840 100644 --- a/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/web/controllers/JobInvokerController.java +++ b/batch-boot-jpa-sample/src/main/java/com/example/bootbatchjpa/web/controllers/JobInvokerController.java @@ -1,28 +1,33 @@ package com.example.bootbatchjpa.web.controllers; import com.example.bootbatchjpa.config.logging.Loggable; -import lombok.RequiredArgsConstructor; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController -@RequiredArgsConstructor @Loggable -public class JobInvokerController { +@RequestMapping("/api/job") +class JobInvokerController { private final JobLauncher jobLauncher; // Declared in BatchConfig private final Job allCustomersJob; - @GetMapping("/run-allCustomers-job") - public String allCustomersJobHandle(@RequestParam Long minId, @RequestParam Long maxId) + JobInvokerController(JobLauncher jobLauncher, Job allCustomersJob) { + this.jobLauncher = jobLauncher; + this.allCustomersJob = allCustomersJob; + } + + @GetMapping("/customers") + String allCustomersJobHandle(@RequestParam Long minId, @RequestParam Long maxId) throws Exception { JobParameters jobParameters = diff --git a/batch-boot-jpa-sample/src/test/java/com/example/bootbatchjpa/common/TestContainersConfig.java b/batch-boot-jpa-sample/src/test/java/com/example/bootbatchjpa/common/TestContainersConfig.java index fd0d02d97..d2b6b923b 100644 --- a/batch-boot-jpa-sample/src/test/java/com/example/bootbatchjpa/common/TestContainersConfig.java +++ b/batch-boot-jpa-sample/src/test/java/com/example/bootbatchjpa/common/TestContainersConfig.java @@ -8,5 +8,5 @@ public interface TestContainersConfig { @ServiceConnection PostgreSQLContainer postgreSQLContainer = - new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.3-alpine")); + new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("16.4-alpine")); }