Mapping many/all URL’s to a single spring controller using annotations

February 17th, 2009 | Tags:

Once in a while this kind of a question pops up on the spring forums. The requirement is almost always the same : Map many URLs to a single controller and/or map all URL’s without a defacto mapping to a default controller. Both have different solutions.

Let’s discuss the first one. The @RequestMapping annotation makes configuring spring controllers a breeze. The value param of the annotation accepts a string array of url’s, most of us miss this part. To map multiple URL’s to a single controller all you need to do is :

@Controller
@RequestMapping(value={"/changepassword.html","/admin/changepass.html"})
public class ChangePasswordForm {

// methods
}

You can also use ant-style regular expressions to map URL’s.

@Controller
@RequestMapping(value={"/changepassword.html","/admin/changepass.html", "/change*.html"})
public class ChangePasswordForm {

// methods
}

Now onto mapping ALL URL’s which do not have a default mapping to a particular controller. This is also quite simple, all you need to do is create a controller which maps to everything. The way spring-mvc works is that it looks for an absolute mapping to service every request, if it doesn’t find one, it then asks the best match to service the request. The controlller should look something like this :

@Controller
@RequestMapping("/*")
public class ChangePasswordForm {

// methods
}

Do leave a comment if you run into a scenario I haven’t covered here or if the post has been of help to you. :)

Share

No related posts.

  1. daniweil
    March 23rd, 2009 at 13:57
    Reply | Quote | #1

    hi. first of all great blog and nice style.
    just one more scenario:
    say we want to retrieve user with a specific mapping like
    mydomain/u/
    so we can map that like above described, but what about the username? how
    to get it? is there a possibility without splitting the http request?
    thanks for your comment!

    • Gaurav Arora
      March 24th, 2009 at 18:24
      Reply | Quote | #2

      How do you want to get the username? Do you mean something like /mydomain/gaurav where gaurav is my username?

      • daniweil
        March 27th, 2009 at 22:40
        Reply | Quote | #3

        yep, or like mydomain/user/gaurv to say that after /user/ there will be every time the username…
        thanks!

        • daniweil
          April 23rd, 2009 at 15:47
          Reply | Quote | #4

          would be great for an answer. have a look at netlog e.g. the use url“s like
          http://de.netlog.com//photo/photoid=
          do you have an idea how to realize that?

          thanks a lot!

          • daniweil
            April 23rd, 2009 at 15:48
            Quote | #5

            de.netlog.com/username/photo/photoid=photoid

  2. Adedayo
    March 30th, 2009 at 22:30
    Reply | Quote | #6

    Hi, i have got a littl e bit of a challenge. I av a controller with serverl mappings within it. Using method level request mappings annotations for an annotated Class. Now wen i a method with @RequestMapping(value={“/fbeasysms/*”,”/fbwelcome.do”}),thing is now i get all request with context /fbeasysms being mapped to this method a valid method handler with @RequestMapping(“/fbeasysms/fbmainpage”) is still being directed to the first method handler mentioned earlire. Any tip would be of help. Lastly meth web.xml looks like this:
    easybuy
    *.do

    easybuy
    /fbeasysms/*

    easybuy
    /fbeasysms

    when i tried add :

    easybuy
    /fbeasysms/*.do

    or
    easybuy
    /fbeasysms/*.fb

    I get error messages at startup. Tahnks!

  3. Adedayo
    March 30th, 2009 at 22:35
    Reply | Quote | #7

    Pardon my bad written english. But i see that my web.xml servlet mappings were not properly formated. Still waiting for your response. Thanks!

  4. Vikram
    August 22nd, 2009 at 14:47
    Reply | Quote | #8

    I find your articles and tutorial of extremely great help. Precise on knowing exactly what I want to know. I just had a problem though, I created @RequestMapping( “/user/* ) on my class level and then at the method level I defined individual RequestMapping with different urls such as @RequestMapping( “saveUser.htm” ). Spring docs say we can do this to filter out in the class which method should be invoked upon which URL.

    Somehow the mapping jst isn’t working for the class level. :(

  5. PUK
    December 20th, 2009 at 16:07
    Reply | Quote | #9

    The RequestMapping of /* at the controller-level doesn’t work. I’m using Spring 3.0.0.RC3. The container’s throwing a HTTP 500 with the following:

    javax.servlet.ServletException: No adapter for handler [org.actest.web.DefautController@1912965]: Does your handler implement a supported interface like Controller?

    If I put the /* at the method level, it works.

    Also, can you tell me where you read that Spring looks for more specific mapping before the more general case? I’m interested in having a default catch-all handler.

    Thanks,

    PUK

  6. erramun
    January 11th, 2011 at 00:33

    What about excluding paths? Is it possible? E.g. Handle all paths not matching “/test”, is there anything like @RequestMapping(value={“**”,”!test”})??

    Thanks