Sunteți pe pagina 1din 3

Spring MVC Annotations

Saturday, August 19, 2017 8:13 PM

@RequestMapping annotation is used to map HTTP request method (GET/POST) to specific a class/method in a Controller
which will
handle the respective request.

@RequestMapping annotation can be applied at class level and method level. In class level we can map the URL of the
request and in method level we can map the URL of the request as well as HTTP methods (GET/POST)

@RequestMapping - single path is mapped to one Controller method.

@RequestMapping(value="/sayHi",method=Req
public String sayHello()
{

@RequestMapping Use Cases

Case 1: Mapping multiple request paths to same method of controller.

In previous approach, @RequestMapping is mapped single HTTP request to one method in a Controller.
there are some cases where mapping multiple requests to the same method may be necessary. For that case, the value
attribute of @RequestMapping does accept multiple mappings, not just a single one:

@Controller
public class DemoController {
@RequestMapping(value={"/hi","bye"},method=RequestMethod.GET)
public String sayHello(ModelMap modelMap)
{
modelMap.addAttribute("message","Welcome to Spring4MVC");
return "welcome";
}
}

Case 2: Mapping multiple HTTP request methods to same method of Controller.

@Controller
public class DemoController {
@RequestMapping(value={"/hi"},method={RequestMethod.GET,RequestMethod.POST})
public String sayHello(ModelMap modelMap)
{
modelMap.addAttribute("message","Welcome to Spring4MVC");
return "welcome";
}
}

@RequestParam annotation is used to map the request parameter to method parameter.

The following are the list of parameters supported by the @RequestParam annotation:
defaultValue This is the default value as a fallback mechanism if request is not having the value or it is empty.
name Name of the parameter to bind

SpringMVCAnnotation Page 1
name Name of the parameter to bind
required Whether the parameter is mandatory or not. If it is true, failing to send that parameter will fail.
value This is an alias for the name attribute

URL : http://localhost:8083/Spring4MVCRequestMapping/hi?name=Sudheer

@Controller
public class DemoController {
@RequestMapping(value="/hi")
public String sayHello(@RequestParam(name="name",required=true) String name,ModelMap modelMap)
{
modelMap.addAttribute("message","Welcome to Spring4MVC--->"+name);
return "welcome";
}

@PathVariable identifies the pattern that is used in the URI for the incoming request.

http://localhost:8083/Spring4MVCRequestMapping/hi/sudheer

@Controller
public class DemoController {
@RequestMapping(value={"/hi/{name}"},method={RequestMethod.GET,RequestMethod.POST})
public String sayHello(@PathVariable("name") String name,ModelMap modelMap)
{
modelMap.addAttribute("message","Welcome to Spring4MVC->"+name);
return "welcome";
}
}

Working with multiple @PathVariable

http://localhost:8083/Spring4MVCRequestMapping/hi/sudheer/version/4.1
@Controller
public class DemoController {
@RequestMapping(value={"/hi/{name}/version/{version}"},method={RequestMethod.GET,RequestMethod.POST})
public String sayHello(@PathVariable("name") String name,ModelMap modelMap,@PathVariable("version") float
version)
{
modelMap.addAttribute("message","Welcome to Spring4MVC->"+name+" version -> "+version);
return "welcome";
}
}

Differences between @RequestParam and @PathVariable

@RequestParam and @PathVariable annotations are used for accessing the values from the request. Both the the
annotations have the significant purpose and use respectively. The key difference between @RequestParam and
@PathVariable is that @RequestParam used for accessing the values of the query parameters where as @PathVariable used
for accessing the values from the URI template.

Annotation indicating a method parameter should be bound to the body of the HTTP request.
For example:
@RequestMapping(path = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
SpringMVCAnnotation Page 2
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
@ResponseBody annotation can be put on a method and indicates that the return type should be written straight to the
HTTP response body (and not placed in a Model, or interpreted as a view name).
For example:
@RequestMapping(path = "/something", method = RequestMethod.PUT)
public @ResponseBody String helloWorld() {
return "Hello World";
}
Alternatively, we can use @RestController annotation in place of @Controller annotation. This will remove the need to using
@ResponseBody.

SpringMVCAnnotation Page 3

S-ar putea să vă placă și