首先,确保在pom.xml
中引入了必要的依赖项,特别是Spring Boot的Web Starter:
org.springframework.boot
spring-boot-starter-web
在NeteaseVODController
类中,我们创建了一个RESTful API的端点,用于接受客户端的请求并返回视频文件的信息:
import com.ruoyi.ruoyity.sy.netease.service.INeteaseVODService;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* 网易云信视频文件查询Controller
*/
@RestController
@RequestMapping("/sy")
public class NeteaseVODController {
@Autowired
private INeteaseVODService neteaseVODService;
/**
* 查询视频文件信息
*
* @param vid 视频Id
* @return 视频信息JSON字符串
*/
@GetMapping("/queryVideo")
public String queryVideo(@RequestParam("vid") String vid) {
return neteaseVODService.queryVideo(vid);
}
}
这里的/sy/queryVideo
端点通过GET请求接受视频ID (vid
) 参数,然后调用INeteaseVODService
服务层来查询视频信息。
在服务层,我们实现了INeteaseVODService
接口,并在NeteaseVODServiceImpl
类中提供了具体的查询逻辑。这个类负责与网易云信的VOD API进行交互:
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.security.MessageDigest;
@Service
public class NeteaseVODServiceImpl implements INeteaseVODService {
private static final String API_URL = "https://vcloud.163.com/app/vod/video/get";
private static final String APP_KEY = "你的AppKey"; // 替换为你的AppKey
private static final String APP_SECRET = "你的AppSecret"; // 替换为你的AppSecret
private static final String NONCE = "1";
@Override
public String queryVideo(String vid) {
try {
String curTime = String.valueOf(System.currentTimeMillis() / 1000L);
String checkSum = getCheckSum(APP_SECRET, NONCE, curTime);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("AppKey", APP_KEY);
headers.set("Nonce", NONCE);
headers.set("CurTime", curTime);
headers.set("CheckSum", checkSum);
String requestBody = "{\"vid\":" + vid + "}";
HttpEntity entity = new HttpEntity<>(requestBody, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity response = restTemplate.exchange(
API_URL,
HttpMethod.POST,
entity,
String.class
);
return response.getBody();
} catch (Exception e) {
e.printStackTrace();
return "Error: " + e.getMessage();
}
}
private String getCheckSum(String appSecret, String nonce, String curTime) {
try {
String input = appSecret + nonce + curTime;
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] hash = messageDigest.digest(input.getBytes("UTF-8"));
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
在与网易云信的API交互时,需要生成CheckSum
以确保请求的安全性。这通过SHA-1算法来实现:
private String getCheckSum(String appSecret, String nonce, String curTime) {
try {
String input = appSecret + nonce + curTime;
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] hash = messageDigest.digest(input.getBytes("UTF-8"));
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
项目运行后,可以使用Postman或浏览器来测试API,例如:
GET http://localhost:8080/sy/queryVideo?vid=123456
请访问: 一线网资源-全网一站式平台
如何使用Spring Boot来集成网易云信的视频点播API,并实现了查询视频信息的功能。