- UtilsController 提供 /utils/fetch-title 接口,供前端获取网页标题 - TitleFetcher 工具类支持 HTTP/HTTPS 请求、重定向跟踪、gzip/deflate 解压 - 支持 SSL 宽松配置、编码自动检测、批量并行抓取 - 支持裸 URL 自动嵌入为 [标题](url) Markdown 格式
27 lines
1002 B
Java
27 lines
1002 B
Java
package com.guo.learningprogresstracker.controller;
|
|
|
|
import com.guo.learningprogresstracker.entity.CommonResult;
|
|
import com.guo.learningprogresstracker.utils.TitleFetcher;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@RequestMapping("/utils")
|
|
public class UtilsController {
|
|
|
|
@GetMapping("/fetch-title")
|
|
@Operation(summary = "获取指定URL的页面标题")
|
|
public CommonResult<Map<String, String>> fetchTitle(@RequestParam String url) {
|
|
if (url == null || url.isBlank()) {
|
|
return CommonResult.error("url 参数不能为空");
|
|
}
|
|
String title = TitleFetcher.fetchTitle(url);
|
|
return CommonResult.success(Map.of("title", title != null ? title : url));
|
|
}
|
|
}
|