Reputation: 1137
I'm wondering if it's possible to get access to a subdomain with Spring's @RequestMapping when the subdomain is a wildcard.
For example, I'd like to write one method that will be called when any number of subdomains are requested, and also have the ability to grab the actual subdomain that the person requested.
So if they visit sub1.example.com, it'll hit a method where I'll be able to map "sub1" to a String variable.
And if they visit sub2.example.com, it'll hit the same method as the previous example, and I'll also be able to map "sub2" to a String variable.
Is this possible?
Upvotes: 2
Views: 885
Reputation: 1137
What I ended up settling on was this approach with Spring MVC:
@RequestMapping(value="/someUrl")
public String someControllerMethod(ModelMap model, HttpServletRequest request)
{
String subdomain = request.getServerName().split("\\.")[0];
log.info("subdomain is: " + subdomain);
// more code here
}
I just make sure to pass in the HttpServletRequest to the method and then extract the subdomain from the request. The only issue with this code is that you can't have a dot (.) in your subdomain (otherwise it'll only pick up the first part of the subdomain).
This solution worked for my needs. But if you have a better solution, feel free to post it and if it's better, I'll make it as the correct answer.
Upvotes: 2