我們將使用 Spring Boot 框架來快速構建API,并使用 Jsoup 庫來解析HTML。
1. 環(huán)境準備
需要以下工具:
JDK 11+:確保已安裝Java開發(fā)工具包。
Maven:用于管理項目依賴。
Spring Boot:用于快速構建API。
Jsoup:用于解析HTML。
可以通過 Spring Initializr 快速生成一個Spring Boot項目,選擇以下依賴:
Spring Web:用于構建Web API。
Spring Boot DevTools:用于開發(fā)時熱加載。
或者直接使用以下Maven依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.15.3</version> </dependency> </dependencies>
創(chuàng)建一個簡單的Java類來表示TDK(Title、Description、Keywords)。
public class TDK { private String title; private String description; private String keywords; // Getters and Setters public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } }
編寫一個服務類來獲取網(wǎng)站的TDK信息。
import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.springframework.stereotype.Service; @Service public class TDKService { public TDK getTDK(String url) { try { // 使用Jsoup獲取網(wǎng)頁內(nèi)容 Document doc = Jsoup.connect(url).get(); // 解析Title String title = doc.title(); // 解析Description Element descriptionElement = doc.select("meta[name=description]").first(); String description = descriptionElement != null ? descriptionElement.attr("content") : null; // 解析Keywords Element keywordsElement = doc.select("meta[name=keywords]").first(); String keywords = keywordsElement != null ? keywordsElement.attr("content") : null; // 返回TDK對象 TDK tdk = new TDK(); tdk.setTitle(title); tdk.setDescription(description); tdk.setKeywords(keywords); return tdk; } catch (Exception e) { throw new RuntimeException("Failed to fetch TDK: " + e.getMessage()); } } }
編寫一個控制器類來處理HTTP請求。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class TDKController { @Autowired private TDKService tdkService; @GetMapping("/tdk") public TDK getTDK(@RequestParam String url) { if (url == null || url.isEmpty()) { throw new IllegalArgumentException("URL parameter is required"); } return tdkService.getTDK(url); } }
確保主應用程序類能夠啟動Spring Boot應用。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TDKApplication { public static void main(String[] args) { SpringApplication.run(TDKApplication.class, args); } }
在項目根目錄下運行以下命令:
mvn spring-boot:run
或者直接在IDE中運行 TDKApplication
類。
啟動后,訪問以下URL來測試API:
http://localhost:8080/tdk?url=https://example.com
如果查詢成功,API將返回以下JSON:
{ "title": "Example Domain", "description": "This is an example domain.", "keywords": "example, domain" }
如果URL參數(shù)缺失或無效,API將返回錯誤信息:
{ "error": "URL parameter is required" }
你可以將項目打包為JAR文件并部署到服務器上:
mvn clean package java -jar target/your-project-name.jar
URL驗證:在實際應用中,建議對輸入的URL進行驗證,確保其格式正確。
異常處理:代碼中已經(jīng)包含了一些基本的異常處理,但你可以根據(jù)需求進一步擴展。
性能優(yōu)化:如果查詢的網(wǎng)站較大或響應較慢,可以考慮使用異步處理或設置超時。