首页 > 解决方案 > 如何在其方法中访问此端点 @GetMapping("/catalog/**") 的 url?

问题描述

我有以下端点:

@GetMapping("/catalog/**")
public String getCatalog(final Model model){
    // code

}

这个端点处理这样的 url:catalog/clothescatalog/clothes/mencatalog/clothes/women等等。当我输入这样的网址时:

http://localhost:8080/catalog/clothes/women

这个端点被触发。我需要在方法中获取这个 url。我怎样才能做到这一点?

提前致谢。

标签: springspring-bootendpoint

解决方案


第二个片段看起来不错,它没有被调用的问题似乎在其他代码中,或者共享类/配置代码。

@Controller
public class CatalogController {
    private static final Logger logger = LoggerFactory.getLogger(CatalogController.class);

    @GetMapping("/catalog/**")
    public String getCatalog(final Model model, HttpServletRequest request, HttpServletResponse response){
        logger.debug(request.getRequestURL().toString());
        logger.debug(request.getQueryString());
        logger.debug("Inside catalog");
        return null;
    }
}

如果您输入的 URL 类似于 - http://localhost:8080/catalog/a/b/c/d?key=value 上面的代码将记录:

  • http://localhost:8080/catalog/a/b/c/d(网址)
  • 键=值(查询字符串)

http://localhost:8080/catalog/a/b/c/d


推荐阅读