JOHND
JOHND

Reputation: 2767

can anybody explain me difference between class level controller and method level controller..?

I am new to spring framework....while searching on google..I found few examples which has @RequestMapping annoted at the class level and few examples showing it at menthod level

When to use class level RequestMapping and menthod level RequestMapping annotation...can anybody explain me difference between class level RequestMapping and method level RequestMapping ..??

so I am little bit confused about their application at :

a) Class level

b) Method level

Also I found some @Requestmapping with type :GET/Post,whereas some examples doesn't have type parameter.

Which approach is better ..??

Is newer versions(>Spring 2.5) don't need parameter type for request mapping ???

Upvotes: 18

Views: 13999

Answers (3)

Ghasem Sadeghi
Ghasem Sadeghi

Reputation: 1854

One of the most important differences specially in Spring MVC is ABSOLUTE and RELATIVE path.

As you know:
In the Spring MVC you can return view as a String or ModelAndView object.

IMPORTANT NOTE:
In both cases you have to pay attention to relative/absolute path:

  1. If you declare / in the beginning of the view name, you are using absolute path.
    Namely it does not matter class level @RequestMapping and directly introduce itself as the final view name.
  2. If you do not declare / in the beginning of the view name, you are using relative path (relative to the class path) and therefore it appends to the class level @RequestMapping to construct final view name.

So, you have to consider the above notes when use the Spring MVC.

Example:
1. create two HTML file test1.html and test2.html in the static folder of spring (boot) structure:
Please note that the class level @RequestMapping behaves as a folder path in the case of relative path.

--- resources
    --- static
        --- classLevelPath     //behaves as a folder when we use relative path scenario in view names   
            --- test2.html      //this will be used for relative path [case (2)]
        --- test1.html          //this will be used for absolute path [case (1)]

  1. create a controller class like as the below. This example shows different cases with return String and ModelAndView in both relative and absolute path.

@Controller
@RequestMapping("/classLevelPath")
public class TestController {

    //case(1)
    @RequestMapping("/methodLevelAbsolutePath1")
    public String absolutePath1(Model model){
        //model.addAttribute();
        //...
        return "/test1.html";  
    }

    //case(1)
    @RequestMapping("/methodLevelAbsolutePath2")
    public ModelAndView absolutePath2(Model model){
        ModelAndView modelAndView = new ModelAndView("/test1.html");
        //modelAndView.addObject()
        //....
        return modelAndView; 
    }

    //case(2)
    @RequestMapping("/methodLevelRelativePath1")
    public String relativePath1(Model model){
        //model.addAttribute();
        //...
        return "test2.html";
    }

    //case(2)
    @RequestMapping("/methodLevelRelativePath2")
    public ModelAndView relativePath2(Model model){
        ModelAndView modelAndView = new ModelAndView("test2.html");
        //modelAndView.addObject()
        //....
        return modelAndView;  
    }


}

Note:
You can specify the suffix of your view files by a ViewResolver (for example InternalResourceViewResolver or spring.mvc.view.suffix=.html in the appliction.properties file of Spring Boot and do not declare .html suffix in the above code.

Best Regard

Upvotes: 1

Narendra Kalekar
Narendra Kalekar

Reputation: 118

To answer your last question i.e. which one is better, I would say in production we use combination of these two. For example if there is a user controller we map the class to "/user" and the methods say for getSettings() will map to "/settings", and method for getPermissions() will map to "/permissions" etc.

You can map these methods directly to "/user/settings" and "/user/permissions" as well skipping class mapping. But mostly we prefer the first approach.

Upvotes: -1

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298908

A controller must be marked as @Controller at the class level. The @RequestMapping annotation can be applied at both class and method level. If it is, method annotations will be interpreted as relative URLs (relative to the class-level URL). However, method level annotations must be present, otherwise the method won't be mapped.

In annotations, parameters can be marked as optional with default values. The method parameter is such a case: it defaults to GET, but can be explicitly set to POST or something else.

See:

Upvotes: 20

Related Questions