VIP歌曲獲取完美解決方案
阿新 • • 發佈:2021-06-30
- 知識點
- List 集合入參
- 字串前後綴剔除
- 好用的JSON轉換工具
- 請求URL
- Controller
@GetMapping("/PasadenaAll") public List<Pasadena> PasadenaAll(@RequestParam List<Integer> newArr) { // 建立返參 List<Pasadena> list = new ArrayList<>(); //組裝查詢條件 List<Integer> macon = new ArrayList<>(); newArr.forEach(ar -> Collections.addAll(macon, ar)); //獲取解析歌曲路徑 PasadenaUtil.list(macon).forEach(item -> list.add(item)); return list; }
- 非同步呼叫工具類
import cn.hutool.http.HttpRequest; import com.alibaba.fastjson.JSON; import com.example.demo.pojo.Pasadena; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component public class PasadenaUtil { public static final String PREFIX = "https://www.9ku.com/html/playjs/58/"; public static final String SUFFIX = ".js"; public static List<Pasadena> list(List<Integer> item) { //建立返參 List<Pasadena> list = new ArrayList<>(); // 遍歷組裝引數非同步請求 item.forEach(arg -> { String url = PREFIX + arg + SUFFIX; String body = HttpRequest.post(url).timeout(2000) .execute() .body(); // 剔除字串第一位 String before = body.substring(1); // 剔除字串最後一位 String after = before.substring(0, before.length() - 1); // 反序列化轉載至集合 list.add(JSON.parseObject(after, Pasadena.class)); }); return list; } }
- 引入依賴
<!--hutool工具類 用於傳送非同步請求-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.3.2</version>
</dependency>
- 方法JSON返回
[ { "id": "89467", "mname": "日不落", "singer": "蔡依林", "wma": "https://mp3.9ku.com/hot/2007/09-18/89467.mp3", "status": "1" }, { "id": "64540", "mname": "風雨彩虹鏗鏘玫瑰", "singer": "田震", "wma": "https://mp3.9ku.com/hot/2005/03-01/64540.mp3", "status": "1" }, { "id": "81668", "mname": "我的中國心", "singer": "張明敏", "wma": "https://mp3.9ku.com/hot/2007/02-24/81668.mp3", "status": "1" }, { "id": "49180", "mname": "一千零一夜", "singer": "邰正宵", "wma": "https://mp3.9ku.com/hot/2004/07-17/49180.mp3", "status": "1" } ]