我对 Spring MVC 的 url-pattern 映射如何工作感到困惑。
当 'getServletMappings' 返回 "/" 时,我可以通过 " http://localhost:8080/hello " 得到正确的响应。
但如果我将其更改为“/app”并将 url 更改为“ http://localhost:8080/app/hello ”则不起作用,它会返回 404 错误。
我是不是误会了什么,我也发现“/app/*”可以工作(我可以理解),但为什么不能“/app”?
请检查我的代码:
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.";
}
}