直接先看效果:https://veddy.cn/sitemap.xml
下面看实现的代码:
1、在maven的pom.xml配置文件中增加maven依赖:
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.11.1</version>
</dependency>
2、编写代码:
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;
}
}
@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);
}
下面展示一下在线转换好的效果
<urlset>
<url>
<loc>https://veddy.cn/article/31</loc>
<lastmod>2020-04-10</lastmod>
<changefreq>hourly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://veddy.cn/article/30</loc>
<lastmod>2020-04-10</lastmod>
<changefreq>hourly</changefreq>
<priority>0.9</priority>
</url>
</urlset>