Spring Boot - Sitemap xml 생성하기

SpringBoot 에서 RestController 호출로 사이트맵을 생성해 리턴하려고 합니다.

사용방법은 간단합니다.

사이트맵 규칙에 맞춰 xml을 생성하고, String으로 Response 해주면 끝이지요!


아래 예제를 보시고 한번 따라해보세요!

1. SiteMapController.java 생성

  1. @RestController
  2. public class SiteMapController {
  3. @Autowired
  4. private SiteMapService siteMapService;
  5. @RequestMapping(value="/sitemap.xml", produces= {"application/xml"})
  6. @ResponseBody
  7. public ResponseEntity<String> sitemap (HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
  8. return ResponseEntity.ok(siteMapService.getSystemicSiteMap());
  9. }
  10. }


2. SiteMapService.java 생성

  1. @Service
  2. public class SiteMapService {
  3. public static final String BASE_URL = "http://www.site.com";
  4. public static final String BEGIN_DOC = "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
  5. public static final String END_DOC = "</urlset>";
  6. public static final String CHANGEFREQ_ALWAYS = "always";
  7. public static final String CHANGEFREQ_HOURLY = "hourly";
  8. public static final String CHANGEFREQ_DAILY = "daily";
  9. public static final String CHANGEFREQ_WEEKLY = "weekly";
  10. public static final String CHANGEFREQ_MONTHLY = "monthly";
  11. public static final String CHANGEFREQ_YEARLY = "yearly";
  12. public static final String CHANGEFREQ_NEVER = "never";
  13. public String getSystemicSiteMap() throws UnsupportedEncodingException {
  14. Date now = new Date();
  15. StringBuffer sb = new StringBuffer();
  16. sb.append(BEGIN_DOC);
  17. sb.append(new SiteMap(BASE_URL, now, CHANGEFREQ_NEVER, "1.0"));
  18. return sb.toString();
  19. }
  20. }


3. Sitemap.java 생성

  1. public class SiteMap {
  2. private static final SimpleDateFormat SITE_MAP_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
  3. public SiteMap(String loc) {
  4. this.loc = loc;
  5. this.lastmod = new Date();
  6. this.changefreq = SiteMapService.CHANGEFREQ_NEVER;
  7. this.priority = "1.0";
  8. }
  9. public SiteMap(String loc, Date lastmod, String changefreq, String priority) {
  10. this.loc = loc;
  11. this.lastmod = lastmod;
  12. this.changefreq = changefreq;
  13. this.priority = priority;
  14. }
  15. /**
  16. * url
  17. */
  18. private String loc;
  19. /**
  20. * yyyy-MM-dd
  21. */
  22. private Date lastmod;
  23. /**
  24. * always hourly daily weekly monthly yearly never
  25. */
  26. private String changefreq;
  27. /**
  28. * 1.0 0.9 0.8
  29. */
  30. private String priority;
  31. public String getLoc() {
  32. return loc;
  33. }
  34. public void setLoc(String loc) {
  35. this.loc = loc;
  36. }
  37. public Date getLastmod() {
  38. return lastmod;
  39. }
  40. public void setLastmod(Date lastmod) {
  41. this.lastmod = lastmod;
  42. }
  43. public String getChangefreq() {
  44. return changefreq;
  45. }
  46. public void setChangefreq(String changefreq) {
  47. this.changefreq = changefreq;
  48. }
  49. public String getPriority() {
  50. return priority;
  51. }
  52. public void setPriority(String priority) {
  53. this.priority = priority;
  54. }
  55. @Override
  56. public String toString() {
  57. StringBuffer sb = new StringBuffer();
  58. sb.append("<url>");
  59. sb.append("<loc>" + loc + "</loc>");
  60. sb.append("<lastmod>" + SITE_MAP_DATE_FORMAT.format(lastmod) + "</lastmod>");
  61. sb.append("<changefreq>" + changefreq + "</changefreq>");
  62. sb.append("<priority>" + priority + "</priority>");
  63. sb.append("</url>");
  64. return sb.toString();
  65. }
  66. }


* 파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있음
작성자 소개
최찬희 프로필
WrapUp 블로거

최찬희

반려견을 좋아하고, 차를 좋아하고, 여행을 좋아하고, 맛집을 찾아 즐기는 웹 개발자 입니다^^

댓글작성

작성자의 다른 포스팅