How can I do Google Sitemap for Spring boot Application

3.1k views Asked by At

I have a spring application deployed on the cloud server, digital ocean.

i tried to generate the sitemap.xml file using https://www.xml-sitemaps.com/ but it only gives me one page. which can be access on https://hostname.com

i would like my sitemap to have something like

  1. https://hostname.com/page1
  2. https://hostname.com/page2

how can i make a sitemap for other pages which are bundle in a war file in a spring boot application.

1

There are 1 answers

1
Ranjith On

If you are looking at exporting URLs mentioned in controllers, then you can get the URLs list from RequestMappingHandlerMapping.

@RestController
public class TestController {

    @Autowired
    private RequestMappingHandlerMapping re;

    @GetMapping("/sitemap.xml")
    public String getSitemap() {
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = re.getHandlerMethods();
        List<String> urls = new ArrayList<>();
        for (Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
            urls.addAll((entry.getKey().getPatternsCondition().getPatterns()));
        }
        // Construct XML response from urls and return it
    }

}