Java Spring引导-确定子域(使用通配符)?
新的春天启动在这里。SpringMVC提供了@SubdomainMapping注释,从我在SpringBoot中看到的内容来看,它似乎不可用。我见过一些人讨论使用过滤器来处理这个问题。或者其他看起来过于复杂的方法 是否有一种(简单/更干净的)方法来处理标准控制器内的所有子域,例如:Java Spring引导-确定子域(使用通配符)?,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,新的春天启动在这里。SpringMVC提供了@SubdomainMapping注释,从我在SpringBoot中看到的内容来看,它似乎不可用。我见过一些人讨论使用过滤器来处理这个问题。或者其他看起来过于复杂的方法 是否有一种(简单/更干净的)方法来处理标准控制器内的所有子域,例如: @SubdomainMapping(value = {"**"} public String data(ModelMap modelMap, HttpServletRequest request) { //Code
@SubdomainMapping(value = {"**"}
public String data(ModelMap modelMap, HttpServletRequest request) {
//Code to handles subdomain logic here ....
}
这将是一种简单的方法,所有值都被平等对待,只有微小的差异
任何建议都会有帮助 我自己也做过这方面的工作,我有一个答案没有你想要的那么简单,但我认为没有一个答案这么简单 因此,您可以创建一个处理程序侦听器适配器,该适配器将在每个请求到达控制器并捕获和处理子域之前捕获它。这需要这样的东西:
@Component
public class SubDomainInterceptor extends HandlerInterceptorAdapter {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception arg3)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView model)
throws Exception {
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
String mysub = request.getRequestURL().toString();
//...
// Do whatever you need to do with the subdomain
//
if (isGoodSubdomain){
session.sendAttribute("subdomain", mysub);
} else {
response.sendRedirect("http://www.basesite.com"):
}
return true;
}
然后在控制器中使用该会话变量来过滤值或任何需要使用它们的内容。我知道这不是你想要的简单答案,但这是迄今为止我找到的最好的答案 谢谢你的例子。我提出了一个使用定制拦截器的解决方案,该拦截器专门处理子域问题。虽然在控制器中处理这个问题确实有效,但我想进一步删除它并将其推到拦截器级别。您是否可以发布您使用的解决方案,以防我们两人以外的任何人对此感兴趣?