VIP歌曲获取完美解决方案

  1. 知识点
  • List 集合入参
  • 字符串前后缀剔除
  • 好用的JSON转换工具
  1. 请求URL
  1. 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;
}

  1. 异步调用工具类
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;
    }
}

  1. 引入依赖
<!--hutool工具类 用于发送异步请求-->
<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>4.3.2</version>
</dependency>

  1. 方法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"
    }
]
原文地址:https://www.cnblogs.com/Twittery/p/14955690.html