I'm having a problem, I want to create a program for extracting from an API data just to print them in console - this via Feign Client from Spring cloud. I'm facing from my Client's class a NullPointerException, but I'm not sure if I understand the issue...
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient(url="https://jsonplaceholder.typicode.com")
public interface UserServiceClient {
@GetMapping("/users")
List<User> getUsersList();
}
Is the method List getUsersList(); returning from external API, JSON so, String's items or is the mapping to my model User processed automatically (I'm pretty sure that not :( )?
Many thanks for any help, in my opinion this NullPointer is coming from the fact that the Client is giving me datas on Strings and not POJO, so he is not able to recognize.. But I'm not sure.
Here is the mistake I face:
Exception in thread "main" java.lang.NullPointerException
at UserService.print(UserService.java:14)
at Launcher.main(Launcher.java:11)
Launcher:
import org.springframework.stereotype.Component;
@Component
public class Launcher {
public static void main(String[] args) {
UserService userService = new UserService();
userService.print();
}
}
UserService:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserServiceClient userServiceClient;
void print() {
for (User user : userServiceClient.getUsersList()) {
System.out.println(
"User ID : " + user.getId() + " The name of the user : " + user.getName()
);
}
}
}
and the UserServiceClient
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient(url="https://jsonplaceholder.typicode.com")
public interface UserServiceClient {
@GetMapping("/users")
List<User> getUsersList();
}
Regards, Erick