{"id":5623,"date":"2025-09-18T13:17:40","date_gmt":"2025-09-18T13:17:40","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5623"},"modified":"2025-09-18T13:21:17","modified_gmt":"2025-09-18T13:21:17","slug":"spring-batch-reading-from-rest-endpoint","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/spring-batch-reading-from-rest-endpoint\/","title":{"rendered":"Spring Batch Reading from REST Endpoint"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2025\/09\/RestData.png\" alt=\"\" \/><\/p>\n<p><strong>BatchConfig.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Configuration\r\npublic class BatchConfig {\r\n    @Autowired\r\n    RestItemReader restItemReader;\r\n\r\n    @Autowired\r\n    ItemWriter restFileWriter;\r\n\r\n    @Autowired\r\n    ItemProcessor simpleJobProcessor;\r\n\r\n    @Bean\r\n    public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) {\r\n        return new JobBuilder(&quot;job&quot;, jobRepository)\r\n                .incrementer(new RunIdIncrementer())\r\n                .start(simpleChunkStepRest(jobRepository, transactionManager))\r\n                .build();\r\n    }\r\n\r\n\r\n    @Bean\r\n    public Step simpleChunkStepRest(JobRepository jobRepository, PlatformTransactionManager transactionManager) {\r\n        return new StepBuilder(&quot;Chunk Oriented Step&quot;, jobRepository)\r\n                .&lt;String, String&gt;chunk(10, transactionManager)\r\n                .reader(restItemReader)\r\n                .processor(simpleJSONJobProcessor)\r\n                .writer(restFileWriter)\r\n                .build();\r\n    }\r\n}\r\n\r\n<\/pre>\n<p><strong>Employee.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Getter\r\n@Setter\r\n@NoArgsConstructor\r\n@AllArgsConstructor\r\n@ToString\r\npublic class Employee {\r\n    private Integer id;\r\n    private String name;\r\n    private String location;\r\n    private Integer age;\r\n}\r\n<\/pre>\n<p><strong>SimpleJSONJobProcessor.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Component\r\npublic class SimpleJSONJobProcessor implements ItemProcessor&lt;Employee, String&gt; {\r\n    @Override\r\n    public String process(Employee item) throws Exception {\r\n        System.out.println(&quot;Inside JSON Job Processor&quot;);\r\n\r\n        String employeeDetails = &quot;The Employee Name is &quot; + item.getName() + &quot;, his Age is &quot; + item.getAge() + &quot; and location is &quot; + item.getLocation();\r\n\r\n        return employeeDetails;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>RestItemReader.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Component\r\npublic class RestItemReader implements ItemReader {\r\n\r\n    @Autowired\r\n    EmployeeService employeeService;\r\n\r\n    private List&lt;Employee&gt; arrEmployee;\r\n\r\n    @Override\r\n    public Employee read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {\r\n        if(arrEmployee == null){\r\n            arrEmployee = employeeService.getEmpList();\r\n        }\r\n\r\n        if(arrEmployee != null &amp;&amp; !arrEmployee.isEmpty()){\r\n            return arrEmployee.remove(0);\r\n        }\r\n\r\n        return null;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>EmployeeService.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">4\r\n@Service\r\npublic class EmployeeService {\r\n    List&lt;Employee&gt; arrEmp = null;\r\n\r\n    public List&lt;Employee&gt; getEmpList(){\r\n        RestTemplate restTemplate = new RestTemplate();\r\n\r\n        Employee&#x5B;] employeeArray = restTemplate.getForObject(&quot;http:\/\/localhost:8080\/employee&quot;, Employee&#x5B;].class);\r\n\r\n\r\n        List&lt;Employee&gt; arrEmp = Arrays.stream(employeeArray)\r\n                .collect(Collectors.toCollection(ArrayList::new));\r\n\r\n        return arrEmp;\r\n    }\r\n\r\n    public Employee getEmployee(){\r\n        if(arrEmp == null){\r\n            getEmpList();\r\n        }\r\n\r\n        if(arrEmp != null &amp;&amp; !arrEmp.isEmpty()){\r\n            return getEmpList().remove(0);\r\n        }\r\n        return null;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>RestFileWriter.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Component\r\npublic class RestFileWriter implements ItemWriter&lt;String&gt; {\r\n    @Override\r\n    public void write(Chunk&lt;? extends String&gt; items) throws Exception {\r\n        System.out.println(&quot;Inside Job Writer&quot;);\r\n        items.getItems().forEach(System.out::println);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInside JSON Job Processor\r\nInside JSON Job Processor\r\nInside JSON Job Processor\r\nInside Job Writer\r\nThe Employee Name is Mugil, his Age is 25 and location is Chennai\r\nThe Employee Name is Mani, his Age is 25 and location is Bangalore\r\nThe Employee Name is Shiva, his Age is 20 and location is Rajsatan\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>BatchConfig.java @Configuration public class BatchConfig { @Autowired RestItemReader restItemReader; @Autowired ItemWriter restFileWriter; @Autowired ItemProcessor simpleJobProcessor; @Bean public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) { return new JobBuilder(&quot;job&quot;, jobRepository) .incrementer(new RunIdIncrementer()) .start(simpleChunkStepRest(jobRepository, transactionManager)) .build(); } @Bean public Step simpleChunkStepRest(JobRepository jobRepository, PlatformTransactionManager transactionManager) { return new StepBuilder(&quot;Chunk Oriented Step&quot;, jobRepository) .&lt;String, String&gt;chunk(10, transactionManager) .reader(restItemReader) .processor(simpleJSONJobProcessor) .writer(restFileWriter) .build(); }&hellip; <a href=\"https:\/\/codethataint.com\/blog\/spring-batch-reading-from-rest-endpoint\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[371],"tags":[],"class_list":["post-5623","post","type-post","status-publish","format-standard","hentry","category-spring-batch"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5623"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5623\/revisions"}],"predecessor-version":[{"id":5626,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5623\/revisions\/5626"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}