创新实训(27)——有关推荐文章列表的后端接口实现

前言

这部分其实很简单,调用之前写好的方法,返回参数就好了,最主要的还是和前端的同学进行参数的协调,已经保重接口的不出错。
在这里插入图片描述

获取热榜列表,这个列表是根据博客系统所有文章的流行度和新鲜度给出的,相关数据被物化在了redis中

   /**
     *
     *  获取默认的文章列表,这个列表是根据博客系统所有文章的流行度和新鲜度给出的
     * @param page
     * @param size
     * @return
     */
    @PermitAll
    @RequestMapping(value = "/recommendArticleByFreshPopular", method = RequestMethod.GET, produces = "application/json")
    public Response recommendArticleByFreshPopular(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        if (page < 0) {
            page = 1;
        }
        if (size < 0) {
            size = 20;
        }
        List<Integer> articleIdList = recommendedUtil.recommendArticleByFreshPopular((page-1)*size, page*size);
        List<Article> articleList = new ArrayList<>();
        for(int articleId :articleIdList)
        {
            Article article = getArticleInfo(articleId);
            articleList.add(article);
        }
        return Response.success("获取文章成功", articleList);
    }

在这里插入图片描述

获取与其相似的其他文章,这个是在文章详细信息最下面显示的,使用计算文章相似度得到的,相关数据被物化在了redis中

 /**
     * 根据文章id  获取与其相似的其他文章,这个是在文章详细信息最下面显示的
     * @param articleId
     * @return  返回值是整个文章的内容,只需要选择需要的东西显示即可
     */
    @PermitAll
    @RequestMapping(value = "/recommendSimilarityArticle", method = RequestMethod.GET, produces = "application/json")
    public Response recommendSimilarityArticle(
            @RequestParam Integer articleId) {

        List<Integer> articleIdList = recommendedUtil.recommendSimilarityArticle(articleId);

        List<Article> articleList = new ArrayList<>();
        for(int id :articleIdList)
        {
            Article article = getArticleInfo(id);
            articleList.add(article);
        }
        return Response.success("获取文章成功", articleList);
    }

在这里插入图片描述

用户的个性推荐 根据用户浏览历史中喜欢的博客文章的分类 推荐相应分类的博客文章

在这里插入图片描述
由于有冷启动问题,当一个用户刚进入系统的时候,是没有用户数据的,所以这个列表会是空的,这时候就需要随即返回一些数据了

    @PermitAll
    @RequestMapping(value = "/recommendArticleByCategory", method = RequestMethod.GET, produces = "application/json")
    public Response recommendArticleByCategory(
            @RequestAttribute String username) throws IOException, TasteException {
        User ouser = userService.selectUserByName(username);
        List<Integer> articleIdList = recommendedUtil.recommendArticleByCategory(ouser.getId());
        List<Article> articleList = new ArrayList<>();
        for(int articleId :articleIdList)
        {
            Article article = getArticleInfo(articleId);
            articleList.add(article);
        }
        if(articleIdList.size() == 0)
        {
            articleList = articleService.selectArticleListRandomly();
            for(Article article :articleList)
            {
                getArticleInfo(article);
            }
            return  Response.success("已为您随机返回文章", articleList);
        }
        return Response.success("获取文章成功", articleList);

    }

用户的个性推荐,根据用户浏览历史中喜欢的博客文章的标签 ,推荐相应标签的博客文章

在这里插入图片描述
由于有冷启动问题,当一个用户刚进入系统的时候,是没有用户数据的,所以这个列表会是空的,这时候就需要随即返回一些数据了

    /**
     * 用户的个性推荐  根据用户浏览历史中喜欢的博客文章的标签  推荐相应标签的博客文章
     * @return    这个api每次只会返回20篇文章  不支持分页查询  并且每次刷新都会返回不一样的文章  且不会推荐浏览过的文章
     * @throws IOException
     * @throws TasteException
     */
    @PermitAll
    @RequestMapping(value = "/recommendArticleByTag", method = RequestMethod.GET, produces = "application/json")
    public Response recommendArticleByTag(
            @RequestAttribute String username) throws IOException, TasteException {
        User ouser = userService.selectUserByName(username);
        List<Integer> articleIdList = recommendedUtil.recommendArticleByTag(ouser.getId());
        List<Article> articleList = new ArrayList<>();
        for(int articleId :articleIdList)
        {
            Article article = getArticleInfo(articleId);
            articleList.add(article);
        }
        if(articleIdList.size() == 0)
        {
            articleList = articleService.selectArticleListRandomly();
            for(Article article :articleList)
            {
                getArticleInfo(article);
            }
            return  Response.success("已为您随机返回文章", articleList);
        }
        return Response.success("获取文章成功", articleList);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值