Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7841351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:04:54+00:00 2026-06-02T16:04:54+00:00

As an example , take subdomain mapping . This article: Managing multiple Domain and

  • 0

As an example, take subdomain mapping.

This article: Managing multiple Domain and Sub Domain on Google App Engine for Same Application
recommends to resolve subdomain on Filter and assign variable to ServletRequest headers.

Then the mapping will look like this:

@RequestMapping(value = "/path", headers="subdomain=www")
 public String subsiteIndexPage(Model model,HttpServletRequest request) { ... }

If we’d like to create custom @RequestMapping property, such as subdomain, eg. to create mapping like this:

@RequestMapping(value = "/some/action", subdomain = "www")
public String handlerFunction(){ ... }

we should override @RequestMapping @interface definition and override RequestMappingHandlerMapping protected methods, with our own implementation
(as stated on JIRA: “Allow custom request mapping conditions SPR-7812“).

Is it right? Can anybody provide a hint, how to achieve this functionality?


Idea 1:
As suggested on original jira thread, is to create own implementation of RequestCondition

There is an project which uses this solution available on github: https://github.com/rstoyanchev/spring-mvc-31-demo/

And related SO question: Adding custom RequestCondition's in Spring mvc 3.1

Maybe mapping like @Subdomain("www") for both Type and Method, is possible solution?


Link to same question on forum.springsource.com

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:04:56+00:00Added an answer on June 2, 2026 at 4:04 pm

    I’ve created solution based on referenced spring-mvc-31-demo

    This solution can be used to map only single RequestCondition as of now. I’ve created two Issues to notify, this should be changed:
    https://github.com/rstoyanchev/spring-mvc-31-demo/issues/5
    https://jira.springsource.org/browse/SPR-9350

    This solution uses custom @RequestCondition feature of Spring 3.1.1.RELEASE platform

    USAGE

    Example 1:

    @Controller
    @SubdomainMapping(value = "subdomain", tld = ".mydomain.com")
    class MyController1 {
        // Code here will be executed only on address match:
        // subdomain.mydomain.com
    }
    

    Example 2:

    @Controller
    class MyController2 {
    
        @RequestMapping("/index.html")
        @SubdomainMapping("www")
        public function index_www(Map<Object, String> map){
            // on www.domain.com
            // where ".domain.com" is defined in SubdomainMapping.java
        }
    
        @RequestMapping("/index.html")
        @SubdomainMapping("custom")
        public function index_custom(Map<Object, String> map){
            // on custom.domain.com
            // where ".domain.com" is defined in SubdomainMapping.java
        }
    }
    

    We need three files

    • SubdomainMapping.java
    • SubdomainRequestCondition.java
    • SubdomainRequestMappingHandlerMapping.java

    SubdomainMapping.java

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.METHOD, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface SubdomainMapping {
    
        /**
        * This param defines single or multiple subdomain
        * Where the Method/Type is valid to be called
        */
        String[] value() default {};
        /**
        * This param defines site domain and tld
        * It's important to put the leading dot
        * Not an array, so cannot be used for mapping multiple domains/tld
        */
        String tld() default ".custom.tld";
    }
    

    SubdomainRequestCondition.java

    import java.net.URL;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.LinkedHashSet;
    import java.util.Set;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.web.servlet.mvc.condition.RequestCondition;
    
    public class SubdomainRequestCondition implements
            RequestCondition<SubdomainRequestCondition> {
    
        private final Set<String> subdomains;
        private final String tld;
    
        public SubdomainRequestCondition(String tld, String... subdomains) {
            this(tld, Arrays.asList(subdomains));
        }
    
        public SubdomainRequestCondition(String tld, Collection<String> subdomains) {
            this.subdomains = Collections.unmodifiableSet(new HashSet<String>(
                    subdomains));
            this.tld = tld;
        }
    
        @Override
        public SubdomainRequestCondition combine(SubdomainRequestCondition other) {
            Set<String> allRoles = new LinkedHashSet<String>(this.subdomains);
            allRoles.addAll(other.subdomains);
            return new SubdomainRequestCondition(tld, allRoles);
        }
    
        @Override
        public SubdomainRequestCondition getMatchingCondition(
                HttpServletRequest request) {
            try {
                URL uri = new URL(request.getRequestURL().toString());
                String[] parts = uri.getHost().split(this.tld);
                if (parts.length == 1) {
                    for (String s : this.subdomains) {
                        if (s.equalsIgnoreCase(parts[0])) {
                            return this;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace(System.err);
            }
            return null;
        }
    
        @Override
        public int compareTo(SubdomainRequestCondition other,
                HttpServletRequest request) {
            return org.apache.commons.collections.CollectionUtils.removeAll(other.subdomains, this.subdomains).size();
        }
    
    }
    

    SubdomainRequestMappingHandlerMapping.java

    import java.lang.reflect.Method;
    
    import org.springframework.core.annotation.AnnotationUtils;
    import org.springframework.web.servlet.mvc.condition.RequestCondition;
    import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
    
    public class CustomRequestMappingHandlerMapping extends
            RequestMappingHandlerMapping {
    
        @Override
        protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
            SubdomainMapping typeAnnotation = AnnotationUtils.findAnnotation(
                    handlerType, SubdomainMapping.class);
            return createCondition(typeAnnotation);
        }
    
        @Override
        protected RequestCondition<?> getCustomMethodCondition(Method method) {
            SubdomainMapping methodAnnotation = AnnotationUtils.findAnnotation(
                    method, SubdomainMapping.class);
            return createCondition(methodAnnotation);
        }
    
        private RequestCondition<?> createCondition(SubdomainMapping accessMapping) {
            return (accessMapping != null) ? new SubdomainRequestCondition(
                    accessMapping.tld(), accessMapping.value()) : null;
        }
    
    }
    

    Instalation

    IMPORTANT: So far, it is not possible to use this solution with XML element
    <mvc:annotation-driven />, see JIRA https://jira.springsource.org/browse/SPR-9344 for explanation

    • You have to register custom MappingHandler bean, pointing at this custom implementation SubdomainRequestMappingHandlerMapping class
    • You have to set it’s order to be lower than default RequestMappingHandlerMapping
      OR
      Replace the registered RequestMappingHandlerMapping (possibly on order=0)

    For more wide explanation on implementing this solution, see the related github project

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take this example code (ignore it being horribly inefficient for the moment) let listToString
Take for example an application which has users, each of which can be in
Take this example Proc: proc = Proc.new {|x,y,&block| block.call(x,y,self.instance_method)} It takes two arguments, x
I'm trying to setup a subdomain elstest1 on my example.com domain to redirect to
I am trying to mod-rewrite based on a sub-domain. How do I take the
Let's take these URLs as an example: http://www.youtube.com/watch?v=8GqqjVXhfMU&feature=youtube_gdata_player http://www.youtube.com/watch?v=8GqqjVXhfMU This PHP function will NOT
As an example take this chunk: The cat sat on the mat, and the
For example take this paper [PDF]. If I wanted to add Weather degradation into
take example: -(void)setName:(NSString *)name age:(int)age; How would you call this method (in other words,
Background I'm building a web application for a client. This app will be accessible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.