Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a default method in SpringMVC using annotations?

I can't find a solution to this, and it's driving me crazy. I have @Controller mapped that responds to several methods using @RequestMapping. I'd like to tag one of those methods as default when nothing more specific is specified. For example:

@Controller
@RequestMapping("/user/*")
public class UserController {

   @RequestMapping("login")
   public String login( MapModel model ) {}

   @RequestMapping("logout")
   public String logout( MapModel model ) {}

   @RequestMapping("authenticate")
   public String authenticate( MapModel model ) {}
}

So /user/login -> login method, /user/logout -> logout, etc. I'd like to make it so that if someone goes to /user then it routes to one of these methods. However, I don't see anything on @RequestMapping that would allow me to specify one of these methods as a default handler. I also don't see any other annotations that might be used on the class either to do this. I'm beginning to suspect it doesn't exist.

I'm using Spring 2.5.6. Is this solved in 3.0.0? I might just hack Spring to make it work because it's tremendously annoying this isn't more straightforward.

Thanks in Advance.

like image 525
chubbsondubs Avatar asked Jul 25 '10 01:07

chubbsondubs


People also ask

What is @RequestMapping annotation used for?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

What are the uses of @RequestMapping and @RestController annotations in spring boot?

Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It's used to mark a class as a web request handler.

What is @GetMapping annotation in spring boot?

Annotation for mapping HTTP GET requests onto specific handler methods. Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. GET) .

What is the use of @PathVariable annotation in Spring rest?

The @PathVariable annotation is used to extract the value from the URI. It is most suitable for the RESTful web service where the URL contains some value. Spring MVC allows us to use multiple @PathVariable annotations in the same method. A path variable is a critical part of creating rest resources.


2 Answers

Take a look at this answer:

Spring MVC and annotated controllers issue

What if you annotate a method with:

@RequestMapping(method = RequestMethod.GET)

You can see an example here:

Spring 3.0 MVC + Hibernate : Simplified with Annotations – Tutorial

The same behavior can be seen here:

Spring Framework 3.0 MVC by Aaron Schram (look at page 21)

like image 53
Leniel Maccaferri Avatar answered Oct 21 '22 21:10

Leniel Maccaferri


Short answer: I do not know how to simply specify one method as default with a simple tag.

But there is this ...

I do not know in which version of Spring this was implemented, but you can provide multiple values to @RequestMapping in 3.1.2. So I do this:

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value = {"", "/", "/list"}, method = RequestMethod.GET)
    public String listUsers(ModelMap model) { }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView add(HttpServletRequest request, ModelMap model) { }

}

The following URLs then map to listUsers():

  • http://example.com/user
  • http://example.com/user/
  • http://example.com/user/list
like image 26
vegemite4me Avatar answered Oct 21 '22 19:10

vegemite4me