Would you be so kind and help me with this piece of code? I got NullPointerException. I have tried lot of stuffs without success. I am trying to fetch data from open API to RestTemplate. At this point i would be happy even for output to console. So i am trying to write Astronauts to console in my AstroController.
//EDIT: Anotation @Autowired added, still i am getting empty list (during debuging)
Thank you for your help, Ondrej
AstroService
package com.example.mydemo.service;
import com.example.mydemo.model.AstroResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class AstroService {
private final RestTemplate restTemplate;
@Autowired
public AstroService(RestTemplateBuilder rtBuilder) {
restTemplate = rtBuilder.build();
}
public AstroResult getAstronauts(){
String url = "http://api.open-notify.org/astros.json";
return restTemplate.getForObject(url, AstroResult.class);
}
}
AstroController
package com.example.mydemo.controller;
import com.example.mydemo.model.Astronaut;
import com.example.mydemo.service.AstroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class AstroController {
@Autowired
private AstroService astroService;
@GetMapping("/astronauts")
void getListOfAstronauts (){
List<Astronaut> list = astroService.getAstronauts().getAstronautList();
for (Astronaut astronaut : list) {
System.out.printf(astronaut.getName());
}
}
}
Astro (model)
package com.example.mydemo.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter @AllArgsConstructor @NoArgsConstructor @Setter
public class Astronaut {
private String name;
private String craft;
}
AstroResult (model)
package com.example.mydemo.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.List;
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Setter
public class AstroResult {
private String message;
private int number;
private List<Astronaut> astronautList;
}
AstroServiceTest
package com.example.mydemo;
import com.example.mydemo.model.AstroResult;
import com.example.mydemo.model.Astronaut;
import com.example.mydemo.service.AstroService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
public class AstroServiceTest {
@Autowired
private AstroService astroService;
@Test
void getAstronauts() {
AstroResult astroResult = astroService.getAstronauts();
int count = astroResult.getNumber();
System.out.printf("There are: " + count + " people in space.");
List<Astronaut> astroResultList = astroResult.getAstronautList();
astroResultList.forEach(System.out::println);
assertAll(
() -> assertTrue(count > 0),
() -> assertEquals(count, astroResultList.size())
);
}
}