Difference between / and /* in servlet mapping url pattern

后端 未结 5 1035
忘了有多久
忘了有多久 2020-11-21 07:39

The familiar code:


    main
             


        
5条回答
  •  你的背包
    2020-11-21 07:49

    Perhaps you need to know how urls are mapped too, since I suffered 404 for hours. There are two kinds of handlers handling requests. BeanNameUrlHandlerMapping and SimpleUrlHandlerMapping. When we defined a servlet-mapping, we are using SimpleUrlHandlerMapping. One thing we need to know is these two handlers share a common property called alwaysUseFullPath which defaults to false.

    false here means Spring will not use the full path to mapp a url to a controller. What does it mean? It means when you define a servlet-mapping:

    
        viewServlet
        /perfix/*
    
    

    the handler will actually use the * part to find the controller. For example, the following controller will face a 404 error when you request it using /perfix/api/feature/doSomething

    @Controller()
    @RequestMapping("/perfix/api/feature")
    public class MyController {
        @RequestMapping(value = "/doSomething", method = RequestMethod.GET) 
        @ResponseBody
        public String doSomething(HttpServletRequest request) {
            ....
        }
    }
    

    It is a perfect match, right? But why 404. As mentioned before, default value of alwaysUseFullPath is false, which means in your request, only /api/feature/doSomething is used to find a corresponding Controller, but there is no Controller cares about that path. You need to either change your url to /perfix/perfix/api/feature/doSomething or remove perfix from MyController base @RequestingMapping.

提交回复
热议问题