I am confused with how Spring MVC's url-pattern mapping works.
When 'getServletMappings' returns "/",I can get the right response with "http://localhost:8080/hello".
but not working if i change it to "/app" and change url to "http://localhost:8080/app/hello", it returns 404 error.
Am I misunderstanding something, I also find that "/app/*" can work(i can understand this), but why cannot "/app"?
Please check my code:
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
// works with http://localhost:8080/hello
return new String[] {
"/"
};
// NOT working with http://localhost:8080/app/hello
// return new String[] {
// "/app"
//};
}
}
@RestController
public class HTTPMethodsController {
@RequestMapping("/hello")
public String hello() {
return "Hello SpringMVC.";
}
}
According to the Servlet specification Chapter 12.2, the mapping of servlets must use the following syntax:
Therefore, mapping the
DispatcherServletwith the URL"/app", causes the servlet container to route requests to it only when there is an exact match, meaning only if you change your url to "http://localhost:8080/app". That leaves no room for adding extra path to target specific Spring Controllers (to be more accurate: you could actually hit yourhello()controller method if you mapped it with@RequestMapping("/app")becauseDispatcherServletfalls back to searching with the entire url, but practically that's not what you want).So the mapping "/app/*" is correct, or you could also map it as the default servlet with "/" as you noticed.