• notice
  • Congratulations on the launch of the Sought Tech site

Detailed explanation of configuring Spring DispatcherServlet to use wildcards

Before we start to introduce the wildcards of Spring DispatcherServlet, let's briefly talk about the use of wildcards in servlet mapping

Wildcards (*) can be used to configure the url-pattern of servlet-mapping in the web.xml file, but the use of this wildcard has very strict restrictions and can only be used in the following two cases:

1. *.[extension], match the extension request, such as: /product/*.do;

2. Begin with "/" and end with "/*", such as: /product/*;

 

A servlet can configure multiple servlet-mappings in web.xml, which means that there may be conflicts between multiple servlet-mappings, so the priority between various mapping rules also needs to be considered during configuration. The priority matching rules are as follows:

1. Match the exact urlPattern first;

2. Priority matches longer urlPattern, such as: /product/list/* has higher priority than /product/*;

3. Match urlPattern with extension first;

 

Closer to home, back to DispatcherServlet.

Spring's DispatcherServlet itself inherits from HttpServlet, so configuring DispatcherServlet in web.xml is no different from configuring other servlets, so what should I pay attention to when using * when configuring the urlPattern of DispatcherServlet?

Let's assume we want to develop a restful style api, the url is: http://localhost/product/list, 

For this we have prepared a ProductController class which has a list method:

@Controller

@RequestMapping("/product")

public class ProductController {

    @RequestMapping("list")

    public String list(Map<String, Object> model){...}

}

In order to let DispatcherServlet only process requests under the /product path, we need to configure the urlPattern of DispatcherServlet as:

/product/*

It seems that there is no problem, but when we visit the following http://localhost/product/list with a browser, we must get: 404

We checked urlPattern and @RequestMapping and couldn't seem to find any problem, it seems that the problem is a bit baffling.

Well, let's debug it according to the following three methods:

1. Replace urlPattern with /product/list;

or

2. Visit http://localhost/product/product/list;

or

3. Remove @RequestMapping("/product")

You will find that you can access it.

Analyzing what we can determine is that in the case of using * to configure urlPattern, after receiving an http request, which controller DispatcherServlet will transfer to for processing is not entirely based on urlPattern (for details on how DispatcherServlet handles urlPattern, please pay attention to the source code of DispatcherSerlvet in the follow-up blog post). parsed url), but based on the part matched by *.

In this example:

The * in /product/* matches "list", and the DispatcherServlet will forward the request to the controller method annotated with @RequestMapping("list")



Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+