You have to build job with JobbuilderFactory:

    @Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    public SomeReader<Some> reader() {

        // some reader configuration
        return reader;
    }

    @Bean
    public SomeProcessor processor() {
        return new SomeProcessor();
    }

    @Bean
    public SomeWriter<Person> writer() {
        // some config
        return writer;
    }

    @Bean
    public Job someJob() {
        return jobBuilderFactory.get("someJob")
                .flow(step1())
                .end()
                .build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<Some, Some> chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }
    }

Start job in rest controller:

@RestController
@AllArgsConstructor
@Slf4j
public class BatchStartController {

    JobLauncher jobLauncher;

    Job job;

    @GetMapping("/job")
    public void startJob() {
    //some parameters
        Map<String, JobParameter> parameters = new HashMap<>();
        JobExecution jobExecution = jobLauncher.run(job, new JobParameters(parameters));
        }    }

And one important detail - add in application.properties:

spring.batch.job.enabled=false

to prevent job self start.

Answer from Valeriy K. on Stack Overflow
🌐
Petri Kainulainen
petrikainulainen.net › home › blog › spring batch tutorial: reading information from a rest api
Spring Batch Tutorial: Reading Information From a REST API - Petri Kainulainen
December 21, 2020 - If you want to read the input data of your batch job from a REST API, you can read this information by using the RestTemplate class. The next part of this tutorial describes how you can read the input data of your batch job from an Excel spreadsheet. P.S. You can get the example application ...
Top answer
1 of 2
2

You have to build job with JobbuilderFactory:

    @Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    public SomeReader<Some> reader() {

        // some reader configuration
        return reader;
    }

    @Bean
    public SomeProcessor processor() {
        return new SomeProcessor();
    }

    @Bean
    public SomeWriter<Person> writer() {
        // some config
        return writer;
    }

    @Bean
    public Job someJob() {
        return jobBuilderFactory.get("someJob")
                .flow(step1())
                .end()
                .build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<Some, Some> chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }
    }

Start job in rest controller:

@RestController
@AllArgsConstructor
@Slf4j
public class BatchStartController {

    JobLauncher jobLauncher;

    Job job;

    @GetMapping("/job")
    public void startJob() {
    //some parameters
        Map<String, JobParameter> parameters = new HashMap<>();
        JobExecution jobExecution = jobLauncher.run(job, new JobParameters(parameters));
        }    }

And one important detail - add in application.properties:

spring.batch.job.enabled=false

to prevent job self start.

2 of 2
0

Solved by myself, as suggested here: Spring Boot: Cannot access REST Controller on localhost (404)

@SpringBootApplication
@EnableBatchProcessing
@EnableScheduling
@ComponentScan(basePackageClasses = JobStatusApi.class)
public class UpdateInfoBatchApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(UpdateInfoBatchApplication.class, args);
    }
    
}
🌐
Medium
medium.com › @andrejtaneski › using-spring-batch-in-a-rest-api-application-493b812d80d1
Using Spring Batch in a REST API Application | by Andrej Taneski | Medium
March 24, 2023 - In this post we’ll take a look at the case where a user interaction is required in order to start off a batch processing job and we’ll enable users to do so through a REST API. This post is meant to be a basic guide on using Spring Batch in your projects, but as with any technology, the official documentation can take you much further.
🌐
GitHub
github.com › chrisgleissner › spring-batch-rest
GitHub - chrisgleissner/spring-batch-rest: REST API for Spring Batch using Spring Boot 2.2 · GitHub
REST API for Spring Batch based on Spring Boot 2.2 and Spring HATOEAS.
Starred by 91 users
Forked by 44 users
Languages   Java 99.9% | Procfile 0.1%
🌐
Medium
prateek-ashtikar512.medium.com › spring-batch-read-from-rest-api-990d03208c1
Spring Batch — read from REST API - Prateek
September 4, 2023 - In this example, we’ll see how to make the call to REST API and feed that information into the reader. Student.java · import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @AllArgsConstructor @NoArgsConstructor @Data @Builder public class Student { private String emailAddress; private String name; private String purchasedPackage; } SpringBatchJob.java · import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.spri
🌐
GitHub
github.com › nicoraynaud › spring-batch-rest-api
GitHub - nicoraynaud/spring-batch-rest-api: Spring Boot library that exposes a REST API to manage Spring batch (start, stop, list, search jobs, job executions and history) · GitHub
swagger: '2.0' info: description: Spring Batch REST API endpoints documentation version: 0.1.0 title: Spring Batch REST API contact: {} license: {} host: localhost:8080 basePath: "/" tags: - name: jobs-resource description: Jobs Resource paths: "/management/jobs": get: tags: - jobs-resource summary: getJobs operationId: getJobsUsingGET produces: - "*/*" parameters: - name: jobName in: query description: jobName required: false type: string responses: '200': description: OK schema: type: array items: "$ref": "#/definitions/JobDescriptionDTO" '401': description: Unauthorized '403': description:
Starred by 6 users
Forked by 15 users
Languages   Java 99.7% | Shell 0.3%
🌐
Baeldung
baeldung.com › home › spring › spring web › implement bulk and batch api in spring
Implement Bulk and Batch API in Spring | Baeldung
July 17, 2024 - We’ll implement a bulk API that takes the bulk requests and processes based on the bulkActionType enum and then returns the bulk status and customer data together. First, we’ll create an EnumMap in the BulkController class and map the ...
🌐
BlackSlate
blackslate.io › articles › data-dump-using-spring-batch
Data dumping through REST API using Spring Batch
Most of the cloud services provide API to fetch their data. But data will be given as paginated results as returning the complete data will overshoot the response payload. To discover the complete list of books or e-courses or cloud machine ...
Find elsewhere
🌐
YouTube
youtube.com › watch
Spring Batch with REST API and SQL DB using Spring Boot - YouTube
In this video, I will explain the architecture, when to use spring batch, how to trigger the spring batch either automatically or by rest API with SQL DB, an...
Published   September 12, 2022
🌐
Keitaro
keitaro.com › insights › 2023 › 03 › 23 › using-spring-batch-in-a-rest-api-application
Using Spring Batch in a REST API Application – Keitaro
March 23, 2023 - Spring Batch offers so much more than the example described in this post, so be sure to refer to the official documentation. Here we looked at an example of how to define job steps, jobs, and how to run jobs asynchronously from a REST controller. What you might want to do next is provide means for the API ...
🌐
Javainuse
javainuse.com › spring › springbootbatchtaskscheduler
Spring Boot Batch Job + Scheduler Simple Example
In this post we develop a simple Spring Boot Batch application where batch job gets triggered using a scheduler. Consider the simple use case where the user wants to delete files from a particular location everyday at a particular time. We will schedule this batch job using the scheduler.
🌐
DZone
dzone.com › software design and architecture › integration › spring boot/batch tutorial: integration with hbase rest api and data ingestion
Spring Boot/Batch Tutorial: Integration With HBASE REST API and Data Ingestion
April 5, 2019 - A developer gives a quick tutorial on how to work with Java to integrate the Spring Boot/Spring Batch frameworks in an app and ingest data from a RESTful API
🌐
GitHub
github.com › jmformenti › spring-batch-rest-cli-example
GitHub - jmformenti/spring-batch-rest-cli-example: Example of execution same spring batch job via command line or REST Api · GitHub
batch-cli execute jobs via command line using Spring Boot. batch-rest execute jobs via REST Api using spring-batch-rest and Spring Boot.
Author   jmformenti
🌐
GitHub
github.com › chaitanyaankam › spring-batch-using-rest
GitHub - chaitanyaankam/spring-batch-using-rest: Spring Batch and Spring REST example; Batch controlled and monitored through REST API's exposed
Spring Batch and Spring REST example (Loading Data form CSV to Database using spring batch framework). Batch controlled and monitored through REST API's exposed (Starting, Aborting, Restarting and Monitoring).
Author   chaitanyaankam
Top answer
1 of 2
2

It is possible to create your own custom ItemWriter.

In your case, please add the spring-boot-starter-web dependency to either your pom.xml or build.gradle

Example:

package com.example.batch;

import lombok.extern.log4j.Log4j2;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Log4j2
public class RestItemWriter implements ItemWriter<String> {
    @Autowired
    RestTemplate restTemplate;

    public RestItemWriter(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Override
    public void write(List<? extends String> items) throws Exception {
        ResponseEntity<Users> users = restTemplate.getForEntity("https://jsonplaceholder.typicode.com/users/1", Users.class);

        log.info("Status code is: " + users.getStatusCode());
    }
}


package com.example.batch;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Users {

    public String id;
    public String name;
    public String username;
    public String email;
    public String phone;
}

More information about custom item writers here

2 of 2
1

Of course! you can send the processed records to another system using REST call in ItemWriter.

Use the below code in your RestItemWriter class.

private RestTemplate restTemplate;

@Override
public void write(@NonNull Chunk<? extends Payload> chunk) throws Exception {

    for((Payload payload : chunk){

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "xx-tokenString-xx");
                    
        HttpEntity<Payload> requestEntity = new HttpEntity<>(payload, headers);

        try{
            ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, Object.class);
            LOGGER.info("Request hits the server {}", response.getBody());
        } catch(HttpClientErrorException e){
            LOGGER.error("HttpClientErrorException occured during connection {}", e.getMessage());
        } catch (Exception e) {
            LOGGER.error("Exception occured during connection {}", e.getMessage());
        }

    }
}

To make HTTP Patch request using RestTemplate, below configurations are mandatory, for other HTTP calls you may ignore it.

 @Bean
    public RestTemplate restTemplate() {
        LOGGER.info("restTemplate Bean has bean created");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
        RestTemplate restTemplate = new RestTemplate(factory);
        return restTemplate;
    }

You have to add the below dependency in pom.xml to use CloseableHttpClient, and HttpClients.

<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
</dependency>
🌐
DEV Community
dev.to › rey5137 › power-up-your-rest-service-with-batch-api-3773
Power up your REST service with Batch API - DEV Community
July 13, 2020 - Although GraphQL is a powerful tool, it still requires you to define all the schemas & functions before the client can use it. In this post, I will show you a different approach, by creating a Batch Request API, that can quickly integrate into your REST service without the need to define any schema.
🌐
Letterpad
jacksparow.letterpad.app › post › data-dumping-through-rest-api-using-spring-batch
Data dumping through REST API using Spring Batch | by Ganesh M
March 10, 2024 - In Spring Batch, tasklet We can use tasklet which will give free-handed to kick start the task and repeat it as per our designed logic. Tasklet will be a single task executed inside a step. The traditional step will have a reader, processor and writer, which works well for file transformation or loading. Fitting our paginated get and dump scenario will be a bit cumbersome. Tasklet gives us the free-hand of placing the GET API request inside the execute and repeat logic till we reach the end of data.
🌐
DZone
dzone.com › data engineering › data › spring batch: processing data from web service to mongodb
Spring Batch: Processing Data From Web Service to MongoDB
December 12, 2018 - Batch reader implement @LineMapper. You can adapt the reader to our data sources (Example): import java.util.List; import org.springframework.batch.item.file.LineMapper; import com.ahajri.batch.beans.BCountry; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.CollectionType; public class BCountryJsonLineMapper implements LineMapper<List<BCountry>> { private final ObjectMapper mapper = new ObjectMapper(); @Override public List<BCountry> mapLine(String line, int lineNumber) throws Exception { CollectionType collectionType =mapper.getTypeFactory().constructCollectionType(List.class, BCountry.class); return mapper.readValue(line, collectionType); } }