基于Spring Boot的协同过滤算法电影推荐系统
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/recommendation")
public class RecommendationController {
private final RecommenderService recommenderService;
@Autowired
public RecommendationController(RecommenderService recommenderService) {
this.recommenderService = recommenderService;
}
@GetMapping("/movies/{userId}")
public List<RecommendedMovie> getRecommendedMovies(@PathVariable("userId") int userId) {
return recommenderService.getRecommendations(userId);
}
@PostMapping("/rate/{userId}/{movieId}/{rating}")
public void rateMovie(@PathVariable("userId") int userId,
@PathVariable("movieId") int movieId,
@PathVariable("rating") double rating) {
recommenderService.rateMovie(userId, movieId, rating);
}
@GetMapping("/similarity/{userId}")
public Map<Integer, Double> getItemSimilarities(@PathVariable("userId") int userId) {
return recommenderService.getItemSimilarities(userId);
}
@GetMapping("/user-similarity/{userId}")
public Map<Integer, Double> getUserSimilarities(@PathVariable("userId") int userId) {
return recommenderService.getUserSimilarities(userId);
}
@GetMapping("/train")
public void trainModel() {
recommenderService.trainModel();
}
}
这个代码实例提供了一个简单的Spring Boot REST控制器,它封装了对推荐系统服务RecommenderService
的调用。它展示了如何使用Spring的依赖注入和注解来创建RESTful API,以及如何将推荐系统作为后端服务集成到Spring Boot应用程序中。
评论已关闭