You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为部署在DigitalOcean的Spring Boot应用生成Google站点地图

Hey there! Let's tackle this sitemap issue for your Spring Boot app. The reason third-party tools like the one you used only picked up your homepage is likely because your other pages aren't discoverable via crawlable links (maybe they're not linked from the homepage or other accessible pages), or your app uses client-side routing that crawlers can't easily parse. Here are a few practical approaches to generate a sitemap that includes all your desired pages:

1. Manually Create & Host sitemap.xml

This is the simplest fix if you only have a handful of static pages:

  • In your Spring Boot project, navigate to src/main/resources/static and create a sitemap.xml file.
  • Populate it with the standard sitemap format, like this:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://hostname.com/</loc>
    <lastmod>2024-05-20</lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://hostname.com/page1</loc>
    <lastmod>2024-05-20</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://hostname.com/page2</loc>
    <lastmod>2024-05-20</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>
  • Repackage your WAR file and redeploy it to DigitalOcean. You’ll now be able to access the full sitemap at https://hostname.com/sitemap.xml. Just remember to update this file manually if you add or modify pages later.
2. Dynamically Generate the Sitemap

If you expect your page count to grow or want automatic updates, use a Spring Boot controller to generate the sitemap on the fly:

  1. Create a controller class like this:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@RestController
public class SitemapController {

    @GetMapping("/sitemap.xml")
    public void generateSitemap(HttpServletResponse response) throws IOException {
        response.setContentType("application/xml");
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today = LocalDate.now().format(dateFormatter);

        StringBuilder xmlBuilder = new StringBuilder();
        xmlBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        xmlBuilder.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
        
        // Add homepage
        addUrl(xmlBuilder, "https://hostname.com/", today, "daily", "1.0");
        // Add page1
        addUrl(xmlBuilder, "https://hostname.com/page1", today, "weekly", "0.8");
        // Add page2
        addUrl(xmlBuilder, "https://hostname.com/page2", today, "weekly", "0.8");
        
        // For future pages, add more addUrl() calls here, or pull URLs from a database/config
        
        xmlBuilder.append("</urlset>");
        response.getWriter().write(xmlBuilder.toString());
    }

    private void addUrl(StringBuilder xmlBuilder, String loc, String lastmod, String changefreq, String priority) {
        xmlBuilder.append("  <url>\n");
        xmlBuilder.append("    <loc>").append(loc).append("</loc>\n");
        xmlBuilder.append("    <lastmod>").append(lastmod).append("</lastmod>\n");
        xmlBuilder.append("    <changefreq>").append(changefreq).append("</changefreq>\n");
        xmlBuilder.append("    <priority>").append(priority).append("</priority>\n");
        xmlBuilder.append("  </url>\n");
    }
}
  1. Repackage and redeploy your app. Now when you visit https://hostname.com/sitemap.xml, you’ll get a dynamically generated sitemap. Adding new pages just requires updating the controller.
3. Make Pages Crawlable for Third-Party Tools

If you want tools to auto-discover your pages later, fix crawlability:

  • Add links to page1 and page2 from your homepage (e.g., in a navigation menu) so crawlers can follow them.
  • If you’re using client-side routing (like React/Vue), set up server-side rendering (SSR) or static site generation (SSG). For HTML5 mode routing, configure your server (Nginx/Apache) to redirect all requests to index.html so crawlers can parse routes correctly.

Also, don’t forget to submit your sitemap to search engine tools (like Google Search Console) to speed up indexing.

内容的提问来源于stack exchange,提问作者Taj Masindi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:19:18