RESTful API 端点使用 Spring
RESTful API endpoint using Spring
在学校我接到了一个任务,我必须用 Java 编程语言完成以下任务:
Implement the RESTful endpoint API, which simultaneously makes calls
to the following websites:
- https://pizzerijalimbo.si/meni/
- https://pizzerijalimbo.si/kontakt/
- https://pizzerijalimbo.si/my-account/
- https://pizzerijalimbo.si/o-nas/
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.
我遵循了一些 YouTube 教程,但只设法将来自随机 API URL 的数据显示到我的本地主机站点。
TestApplication.java
(主文件)
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
(第二个文件)
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);
}
}
请帮我一些基本的指导或建议。
获取输入整数作为查询参数并编写一个 switch case。如果它是 4,则使用 Executor 框架,创建所需的线程并并行地使其余模板调用 4 个唯一的 URLS。然后抓取 html 内容和 http 状态。将其存储在 HashMap 中并打印结果。
如果我理解正确,您需要一个带有查询参数的端点,如下所示:
@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
}
}
}
更新
也许使用 Jsoup 获取网站内容会更好,这样更容易从中获取您需要的数据。更多详细信息,请访问:
在学校我接到了一个任务,我必须用 Java 编程语言完成以下任务:
Implement the RESTful endpoint API, which simultaneously makes calls to the following websites:
- https://pizzerijalimbo.si/meni/
- https://pizzerijalimbo.si/kontakt/
- https://pizzerijalimbo.si/my-account/
- https://pizzerijalimbo.si/o-nas/
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.
我遵循了一些 YouTube 教程,但只设法将来自随机 API URL 的数据显示到我的本地主机站点。
TestApplication.java
(主文件)
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
(第二个文件)
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);
}
}
请帮我一些基本的指导或建议。
获取输入整数作为查询参数并编写一个 switch case。如果它是 4,则使用 Executor 框架,创建所需的线程并并行地使其余模板调用 4 个唯一的 URLS。然后抓取 html 内容和 http 状态。将其存储在 HashMap 中并打印结果。
如果我理解正确,您需要一个带有查询参数的端点,如下所示:
@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
}
}
}
更新
也许使用 Jsoup 获取网站内容会更好,这样更容易从中获取您需要的数据。更多详细信息,请访问: