-3

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;
Ricky
  • 2,662
  • 5
  • 25
  • 57
  • 1
    Is there a stack trace from your exception? – khelwood Jul 24 '18 at 07:51
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Jul 24 '18 at 07:57
  • @GhostCat Thanks for the guide I am improving the quality of the question – Ricky Jul 24 '18 at 08:02
  • @khelwood attached the stack trace – Ricky Jul 24 '18 at 08:03
  • Which line is line 40? – Stephen C Jul 24 '18 at 08:05
  • 2
    So it's from `restTemplateConsumer.getForObject` is it? You're `restTemplateConsumer` is null? – khelwood Jul 24 '18 at 08:06
  • @StephenC c = restTemplateConsumer.getForObject("https://jsonplaceholder.typicode.com/posts/1", Model.class); is the line 40. SO if I am not wrong it could not parse the response to the Model object right. – Ricky Jul 24 '18 at 08:07
  • Nope. See khelwood's comment. – Stephen C Jul 24 '18 at 08:08
  • Oh yeah my restTemplateConsumer is null – Ricky Jul 24 '18 at 08:08
  • Adding @Autowired solved the issue – Ricky Jul 24 '18 at 08:10
  • @khelwood Thanks a lot and you can write the answer as well I will accept it – Ricky Jul 24 '18 at 08:12
  • 2
    @Ricky There's no need. The [NPE dupe](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) covers this. – khelwood Jul 24 '18 at 08:18

1 Answers1

4

the exception will throw because the restTemplateConsumer object is null. you should use @Qualifier annotation along with @Autowired

you can read more about how to use @Qualifier here