简体   繁体   中英

Spring Boot GET request to API

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.

Use this model to map data

public class Covid19 {
  String date;
  Integer confirmed;
  Integer recovered;
  Integer deaths;
  ... setter getter
}

And to read the data use

    final String uri = "https://raw.githubusercontent.com/pomber/covid19/master/docs/timeseries.json";
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, List<Covid19>> countryMap = objectMapper.readValue(uri,new TypeReference<Map<String, List<Covid19>>>(){});

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

Use HTTP protocol to access the URL:

URL("http://raw.githubusercontent.com/pomber/covid19/master/docs/timeseries.json");

Also, use the following code to get the data

URL hh= new URL("http://raw.githubusercontent.com/pomber/covid19/master/docs/timeseries.json");
        URLConnection connection = hh.openConnection();
/*
If your URL is redirected to other URL. It's quite normal and web site may have many reasons to redirect you. Just follow the redirect based on "Location" HTTP header like that:
*/
        String redirect = connection.getHeaderField("Location");
        if (redirect != null){
            connection = new URL(redirect).openConnection();
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        System.out.println();
        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        }

Also, to send the data through your API, make changes in your POJO class and accordingly return the Object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM