Search code examples
javaspringspring-bootrestspring-restcontroller

RESTful API endpoint using Spring


At school I got a task in which I have to do the following task in the Java programming language:

Implement the RESTful endpoint API, which simultaneously makes calls to the following websites:

The input for the endpoint is ‘integer’, which represents the number of simultaneous calls to the above web pages (min 1 represents all consecutive calls, max 4 represents all simultaneous calls).

Extracts a short title text from each page and saves this text in a common global structure (array, folder (). The program should also count successful calls. Finally, the service should list the number of successful calls, the number of failed calls and the saved address texts from all web pages.

I followed some YouTube tutorials and only managed to display data from a random API URL to my local host site.

TestApplication.java (Main file)

   package com.example.test;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    public class TestApplication {
    
        @Bean
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
    
        public static void main(String[] args) {
            SpringApplication.run(TestApplication.class, args);
        }
    }

APIcontroller.java (Second file)

package com.example.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@RestController
public class APIcontroller {
    
    @Autowired
    private RestTemplate restTemplate;

    private static String url = "https://www.result.si/projekti/";

    @GetMapping("/countries")
    public List<Object> getCountries() {
        Object[] countries = restTemplate.getForObject(url, Object[].class);
        return Arrays.asList(countries);
    }

}

Please help me with some basic guidance or suggestions.


Solution

  • If I understood this correctly you need to have an endpoint with a query parameter as follows:

    @RestController
    public class APIcontroller {
        
        @Autowired
        private RestTemplate restTemplate;
    
        List<String> websites = Arrays.asList("https://pizzerijalimbo.si/meni/", 
            "https://pizzerijalimbo.si/kontakt/", 
            "https://pizzerijalimbo.si/my-account/", 
            "https://pizzerijalimbo.si/o-nas/");
    
        @GetMapping("/data-scraping")
        public List<Object> getData(@RequestParam(required = true) int numberOfWebsites) {
            List<String> websitesToScrape = websites.subList(0, numberOfWebsites);
        
            for (String website : websitesToScrape) {
                Document doc = Jsoup.connect(website).get();
                // here you need to extract a the title text from each page and store it in a common global variable
            }
        }
    }
    

    Update

    Maybe it is even better to get the websites' content using Jsoup, so that it is easier to get the data that you need from it. More details at: