SpringBoot 做 sitemap.xml # 基于Xstream方式 返回xml

技术博客   |   2019-02-16 14:03:13  |   7096


Springboot 采用了Thymeleaf模板的方式,这对于使用springboot开发网站的开发工程师来说是一个头疼的事情,因为网站开发好了之后是需要做seo优化的,而百度和360搜索引擎#特别是360搜索引擎使用的是sitemap方式的抓取,sitemap提供了3个格式,一种是gz,一种是txt,还有一种是xml。那么下面就来介绍一下xml方式(并且可以一次编写,多次更新,O(∩_∩)O哈哈~)

直接先看效果:http://www.ziiia.com/sitemap.xml

 

下面看实现的代码:

1、在maven的pom.xml配置文件中增加maven依赖:

        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.10</version>
        </dependency>

2、编写接口代码:

@RequestMapping(value = "/sitemap",produces = {"application/xml;charset=UTF-8"},method = RequestMethod.GET)
    @ResponseBody
    public String sitemap(){
        String lastmod = DateFormatUtils.getDefaultFormatDate(new Date(),"yyyy-MM-dd");
        List<Url> urls = new ArrayList<>();
        Url url = new Url();
        url.setLoc(sitemapurl);
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("1.00");
        urls.add(url);

        url = new Url();
        url.setLoc(sitemapurl + "/minapp.html");
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("0.5");
        urls.add(url);

        url = new Url();
        url.setLoc(sitemapurl + "/blogs/1.html");
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("0.5");
        urls.add(url);

        url = new Url();
        url.setLoc(sitemapurl + "/page/personTax.html");
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("0.5");
        urls.add(url);

        url = new Url();
        url.setLoc(sitemapurl + "/page/personSafe.html");
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("0.5");
        urls.add(url);

        url = new Url();
        url.setLoc(sitemapurl + "/page/personHouse.html");
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("0.5");
        urls.add(url);

        url = new Url();
        url.setLoc(sitemapurl + "/page/personCar.html");
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("0.5");
        urls.add(url);

        url = new Url();
        url.setLoc(sitemapurl + "/minapp/detail/10000.html");
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("0.25");
        urls.add(url);

        url = new Url();
        url.setLoc(sitemapurl + "/minapp/detail/10001.html");
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("0.25");
        urls.add(url);

        url = new Url();
        url.setLoc(sitemapurl + "/blog/detail/100000.html");
        url.setLastmod(lastmod);
        url.setChangefreq(Changefreq);
        url.setPriority("0.25");
        urls.add(url);
        XStream xstream = new XStream(new StaxDriver());
        xstream.alias("url",Url.class);
        xstream.alias("urlset",List.class);
        return xstream.toXML(urls);
    }

 

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("url")
public class Url {
    private String loc;
    private String lastmod;
    private String changefreq;
    private String priority;

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public String getLastmod() {
        return lastmod;
    }

    public void setLastmod(String lastmod) {
        this.lastmod = lastmod;
    }

    public String getChangefreq() {
        return changefreq;
    }

    public void setChangefreq(String changefreq) {
        this.changefreq = changefreq;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }
}

 

下面展示一下在线转换好的效果

<urlset>
<url>
<loc>http://www.ziiia.com</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
<url>
<loc>http://www.ziiia.com/minapp.html</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.ziiia.com/blogs/1.html</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.ziiia.com/page/personTax.html</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.ziiia.com/page/personSafe.html</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.ziiia.com/page/personHouse.html</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.ziiia.com/page/personCar.html</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.ziiia.com/minapp/detail/10000.html</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>0.25</priority>
</url>
<url>
<loc>http://www.ziiia.com/minapp/detail/10001.html</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>0.25</priority>
</url>
<url>
<loc>http://www.ziiia.com/blog/detail/100000.html</loc>
<lastmod>2019-02-16</lastmod>
<changefreq>weekly</changefreq>
<priority>0.25</priority>
</url>
</urlset>

 

如有疑问,点击下面的 联系站长

 

蜂猴在线小程序

 

个税热门城市

北京 上海 深圳 广州 苏州 南京 杭州 重庆 成都 武汉 西安 郑州 宁波 石家庄

 

蜂猴在线

赞赏  |   联系站长  |   微信小程序  |   免责声明    

Copyright © 2019 蜂猴在线 Inc. All rights reserved.  访问量次  备案号:苏ICP备17048977号