Java生成站点地图

111次阅读
没有评论

使用插件:com.redfin.sitemapgenerator

maven插件:

  <!--网站地图-->
        <dependency>
            <groupId>com.github.dfabulich</groupId>
            <artifactId>sitemapgen4j</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15to18</artifactId>
            <version>1.68</version>
        </dependency>

代码

public String createSiteMapXmlContent() {
        String siteUrl = configService.selectConfigByKey("site.url");
        String baseUrl = String.format(siteUrl);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        WebSitemapGenerator wsg = null;
        try {
            wsg = new WebSitemapGenerator(baseUrl);
            // 首页 url
            WebSitemapUrl url = new WebSitemapUrl.Options(baseUrl + "/")
                    .lastMod(dateTimeFormatter.format(LocalDateTime.now())).priority(1.0).changeFreq(ChangeFreq.DAILY).build();
            wsg.addUrl(url);

            // 查询所有的问题方案数据
            List<Article> articles = articleService.selectSimpleArticles();

            // 动态添加文章url
            for (Article article : articles) {
                WebSitemapUrl tmpUrl = new WebSitemapUrl
                        .Options(baseUrl + "/blog/article/" + article.getId())
                        .lastMod(DateUtil.format(article.getUpdateTime(), "yyyy-MM-dd"))
                        .priority(0.7)
                        .changeFreq(ChangeFreq.DAILY)
                        .build();
                wsg.addUrl(tmpUrl);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        return String.join("", wsg.writeAsStrings());
    }

正文完
 
评论(没有评论)