Skip to the content.

What is the @Controller annotation used for?

The @Controller annotation is a specialization of the @Component annotation. In a web application, the controllers work between the web layer and the core application layer. In the Spring MVC framework, controllers are also more like POJO classes with methods; these methods are known as handlers, because these are annotated with the @RequestMapping annotation. You could also use the @Component annotation instead of @Controller to create Spring beans in a web application, but in this case, that bean does not have the capability of the Spring MVC framework such as exception handling at web layer, handler mapping, and so on.

How is an incoming request mapped to a controller and mapped to a method?

When a request is issued to the application:

/articles/new Dispatcher servlet: / Controller: /articles Method: /new

What is the difference between @RequestMapping and @GetMapping?

@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

What is @RequestParam used for?

@Controller 
public  class  AccountController {
    @GetMapping(value = "/account")
    public String getAccountDetails (ModelMap model, HttpServletRequest request) {
        String accountId = request.getParameter("accountId");
        Account account = accountService.findOne(Long.valueOf(accountId));
        model.put("account", account);
        return "accountDetails";
    } 
}

The @RequestParam annotation is used to annotate parameters to handler methods in order to bind request parameters to method parameters. Assume there is a controller method with the following signature and annotations:

@RequestMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false) String inName) {
        ...
}

If then a request is sent to the URL http://localhost:8080/greeting?name=Ivan then the inName method parameter will contain the string “Ivan”.

What are the differences between @RequestParam and @PathVariable?

Spring MVC allows you to pass parameters in the URI instead of passing them through request parameters. The passed values can be extracted from the request URLs. It is based on URI templates. It allows clean URLs without request parameters. The following is an example:

@Controller
public class AccountController {
    @GetMapping("/accounts/{accountId}")
    public String show(@PathVariable("accountId") long accountId, Model model) {
        Account account = accountService.findOne(accountId);
        model.put("account", account);
        return "accountDetails";
    }
  ...
}

Difference The difference between the @RequestParam annotation and the @PathVariable annotation is that they map different parts of request URLs to handler method arguments.

What are the ready-to-use argument types you can use in a controller method?

###What other annotations might you use on a controller method parameter? (You can ignore form-handling annotations for this exam)

What are some valid return types of a controller method?

References

  1. MrR0807 Spring certification notes
  2. Moss Green Spring certification notes
  3. Spring Documentation
  4. Spring Boot Documentation