I am using rest template in Spring boot to build a REST client in spring boot to consume and parse it into a java Model Object. My code snippet is like below for the Controller that fetches the API Response and parses it into a Model Object using the Jackson Parser every 3000 ms in a scheduled job:
@RestController
public class RestAPIConsumerController {
@Qualifier("restTemplateConsumer")
private RestTemplate restTemplateConsumer;
@Bean
public RestTemplate restTemplateConsumer() {
RestTemplateBuilder builderConsumer = new RestTemplateBuilder();
RestTemplate buildConsumer = builderConsumer.build();
return buildConsumer;
}
@Scheduled(fixedRate = 3000)
public void consumeAPI() {
Model c = new Model();
try {
System.out.println("Started to Fetch API");
c = restTemplateConsumer.getForObject("https://jsonplaceholder.typicode.com/posts/1", Model.class);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
My Model.java is like below
public class Model {
int userId;
int id ;
String title;
String body;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
I couldn't figure out the reason for null pointer exception is it because of my object initialisation or it could not able to parse the string to my Model Object.
Stacktrace is below:
java.lang.NullPointerException at com.test.RestAPIConsumerController.consumeAPI(RestAPIConsumerController.java:40) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514) at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:844)
Excpected behaviour: The expected behaviour of the code is to parse the response JSON successfuly to the Model Object.
Any help is appreciated.
Adding @Autowired solved the issue
@Autowired
@Qualifier("restTemplateConsumer")
private RestTemplate restTemplateConsumer;