Microtribute
Microtribute

Reputation: 1062

What are the use cases of the `name` property of the `RequestMapping` annotation in Spring Framework?

Apologies, if you find this question dumb... I am new to Spring framework. I spent hours looking up an answer...

According to the official Spring Framework documentation, you can assign a name to a RequestMapping annotation using the name property.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#name--

So the question is what's the point of giving a name to a route mapping?

In Symfony framework, we can use the mapping name to generate a URL.

class BlogController
{
    /**
     * @Route(name="BlogComments", path="blog/{blog}/comments/{page}")
     */
    public function listBlogComments(Blog blog, page)
    {
        ...
    }
}

And then we can generate a URL based on the route name.

// This will generate a string "blog/27/comments/1".
$url = $this->generateUrl('BlogComments', [
    'blog' => 27,
    'page' => 1
]);

This is possible thanks to the Symfony\Component\Routing\Router component.

The controller class can be written like the following in the Spring framework.

@Controller
@ResponseBody
class BlogController {

    @RequestMapping(name="BlogComments", path="blog/{blog}/comments/{page}")
    public List<Comment> listBlogComments(@PathVariable Blog blog, @PathVariable Long page) {
        ...
    }
}

Now how do I generate a URL based on the name of the mapping which is "BlogComments" in this case? Is there any Spring component or Service available as in the Symfony framework? What other possible use cases are available?

Upvotes: 4

Views: 1849

Answers (2)

backdoor
backdoor

Reputation: 901

From documentation:

public abstract String name

Assign a name to this mapping. Supported at the type level as well as at the method level! When used on both levels, a combined name is derived by concatenation with "#" as separator.

The key moment is When used on both levels

So you should assign a name for the controller as well as well and it should start working.

@Controller
@ResponseBody
@RequestMapping(name = "AdminController")
class BlogController {

    @RequestMapping(name="BlogComments", path="blog/{blog}/comments/{page}")
    public List<Comment> listBlogComments(@PathVariable Blog blog, @PathVariable Long page) {
        ...
    }
}

And then you can access URL using #

<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>

<a href="${s:mvcUrl('AdminController#BlogComments').arg("1","123").build()}">Get Person</a>

Applications can build a URL to a controller method by name with the help of the static method MvcUriComponentsBuilder#fromMappingName or in JSPs through the "mvcUrl" function registered by the Spring tag library.

Upvotes: 1

Eklavya
Eklavya

Reputation: 18420

@RequestMapping annotation's name attribute can be used to assign a name to the mapping of controller class and method.

@Controller
@ResponseBody
@RequestMapping(name = "BlogController")
class BlogController {

    @RequestMapping(name="BlogComments", path="blog/{name}")
    public List<Comment> listBlogComments(@PathVariable String name) {
        ...
    }
}

The Spring JSP tag library provides a function called mvcUrl that can be used to prepare links to controller methods based on this mechanism.

<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
...
<a href="${s:mvcUrl('BlogController#BlogComments').arg("test").buildAndExpand()}">Get Coments</a>

Upvotes: 1

Related Questions