Monday, September 25, 2017

Spring MVC – @RequestMapping

Let's start with a simple example - mapping HTTP request to method using some basic criteria.

1. @RequestMapping – by Path 


@RequestMapping(value = "/ex/greetings", method = RequestMethod.GET)
    @ResponseBody
    public String getGreetingsBySimplePath() {
        return "Get Greetings";
    }

 To test out this mapping with a simple curl command, run:


curl -i http://localhost:7001/springapp/ex/greetings

2. @RequestMapping – the HTTP Method

 

Previous example was based on HTTP GET method. Now mapped to HTTP POST method.


@RequestMapping(value = "/ex/greetings", method = RequestMethod.POST)
    @ResponseBody
    public String postGreetings() {
        return "Post Greetings";
    }

To test the POST via a curl command:


curl  -i  -X  POST  http://localhost:7001/springapp/ex/greetings

3. @RequestMapping and HTTP Headers


The mapping can be narrowed even further by specifying a header for the request:

@RequestMapping(value = "/ex/greetings", headers = "key=val", method = RequestMethod.GET)
    @ResponseBody
    public String getGreetingsWithHeader() {
        return "Get Greetings with some header";
    }

To test this via a curl command:


curl -i -H "key:val" "http://localhost:7001/springapp/ex/greetings"

For multiple headers via the header attribute of @RequestMapping:

@RequestMapping(value = "/ex/greetings", headers = { "key1=val1",
            "key2=val2" }, method = RequestMethod.GET)
    @ResponseBody
    public String getGreetingsWithMultipleHeader() {
        return "Get Greetings with multiple header";
    }

To test this via a curl command:


curl -i -H "key1:val1" -H "key2:val2" "http://localhost:7001/springapp/ex/greetings"

4. @RequestMapping Consumes and Produces


@RequestMapping annotation now has the produces and the consumes attributes, specifically for request based on Accept header.

@RequestMapping(value = "/ex/greetings", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public String getGreetingsAsJsonFromREST() {
        return "Get Greetings with header from REST";
    }

To test this via a curl command:


curl -i -H "Accept:application/json"  "http://localhost:7001/springapp/ex/greetings"

5. @RequestMapping with Path Variables


Parts of the mapping URI can be bound to variables via the @PathVariable annotation. A simple example with a single path variable:


@RequestMapping(value = "/ex/greetings/{greetingsId}/locations/{locationId}", method = RequestMethod.GET)
    @ResponseBody
    public String getGreetingsBySimplePathVariable(
            @PathVariable String greetingsId, @PathVariable String locationId) {
        return "Get Greetings with id=" + greetingsId + " and location="+locationId;
    }

To test this via a curl command:


curl "http://localhost:7001/springapp/ex/greetings/1/locations/Motijheel"

6. @RequestMapping with Request Parameters


We are now mapping a request to a URI such as:  http://localhost:7001/springapp/ex/location?id=w

@RequestMapping(value = "/ex/location", method = RequestMethod.GET)
    @ResponseBody
    public String getLocationBySimplePathWithRequestParam(
            @RequestParam("id") String id) {
        return "Get specific location with id = " + id;
    }

For more advanced scenarios, @RequestMapping can optionally define the parameters – as yet another way of narrowing the request mapping:

@RequestMapping(value = "/ex/location", params = "id", method = RequestMethod.GET)
    @ResponseBody
    public String getLocationBySimplePathWithExplicitRequestParam(
            @RequestParam("id") String id) {
        return "Get specific location params with id = " + id;
    }

Even more flexible mappings are allowed – multiple params values can be set to another URL, and not all of them have to be used:

@RequestMapping(value = "/ex/location", params = { "id", "name" }, method = RequestMethod.POST)
    @ResponseBody
    public String getLocationBySimplePathWithExplicitMultipleRequestParam(
            @RequestParam("id") String id) {
        return "Get specific location with multiple params with id = " + id;
    }

To test this via a curl command:


curl  "http://localhost:7001/springapp/ex/location?id=w&name=DHAKA

7. @RequestMapping – a fallback for all requests

 

For all requests:

@RequestMapping(value = "*", method = { RequestMethod.GET,
            RequestMethod.POST })
    @ResponseBody
    public String fallbackMessage() {
        return "Fallback Request";
    }

8. @RequestMapping – multiple paths mapped to the same controller method

 

Multiple requests using different HTTP verbs can be mapped to the same controller method:

@RequestMapping(value = { "/m0", "/m1", "/m2" })
    @ResponseBody
    public String method0() {
        System.out.println(messages.get("method0"));
        return "method0";
    }



No comments:

Post a Comment