So, I have this JSON data which is in raw format. It simply denotes the current covid-19 records around the world day by day. I need to send a GET request to it and display the data in the browser by using Spring Boot. I have tried getForObject(url, class)
method, but it gave an error with a message no suitable HttpMessageConverter found for response type
. I have tried to solve it but could not. Then I have tried the ObjectMapper.readValue(url, class)
method with the URL of JSON data and Covid19.class
. This time, I get an error with a message no protocol
. Following is the structure of the project:
Covid19.java:
public class Covid19 implements Serializable {
private final String country;
public Covid19(String country){
this.country = country;
}
public String getCountry(){
return country;
}
}
Covid19Controller.java:
@RestController
public class Covid19Controller {
@GetMapping(value = "/covid", produces = MediaType.APPLICATION_JSON_VALUE)
public Covid19 covid19() throws IOException {
URL url = new URL("raw.githubusercontent.com/pomber/covid19/master/docs/timeseries.json");
Covid19 covid19 = new ObjectMapper().readValue(url, Covid19.class);
return covid19;
}
Main class:
public static void main(String[] args) {
SpringApplication.run(DataminingWebserviceApplication.class, args);
}
Actually my aim was to send a GET Request with parameter country
but stuck at the very beginning. I have started to study Spring Boot yesterday and trying to learn it. There are plenty of tutorials but none fits my case so well. Thanks in advance.
The class you have defined is not correct, Create a class like this
@JsonIgnoreProperties(ignoreUnknown = true)
public class Covid19{
private int confirmed;
private int deaths;
....
// add other fields and Getters & Setters
}
And the code to read the data should look like this
URL url = new URL("https://raw.githubusercontent.com/pomber/covid19/master/docs/timeseries.json");
final Map<String, List<Covid19>> covid19Map= new ObjectMapper().readValue(url, new TypeReference<Map<String, List<Covid19>>>() {});
covid19Map will have the key as country and the valus as date wise list as indicated in the json