将扩展添加到 URL 时强制出现 404 错误
Force 404 error when extension is added to URL
我在 URL 映射方面需要一些帮助,在我的代码中,当我转到:
http://localhost:8080/register.asdf
http://localhost:8080/register.asddsdsd etc.
它总是 returns http://localhost:8080/register
但我想让它 404 NOT FOUND.
我该如何解决这个问题?
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(Config.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
@Controller
public class UserController {
@RequestMapping(path = "/register", method = RequestMethod.GET)
public String registerGet(Model model) {
return "register";
}
编辑: 我在 Config.java 中添加了以下代码并解决了谢谢。
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
您可以在 @RequestMapping
或 servlet.mapping
中限制映射更改。
更改请求映射:
@RequestMapping(path = "/register.htm", method = RequestMethod.GET)//for example
或servlet.mapping:
servlet.addMapping("*.htm");
编辑:
如果你使用 Spring 4.X 你可以使用这个:
<mvc:annotation-driven>
<mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>
来自 docs 使用下面的配置来限制不需要的扩展
<mvc:annotation-driven>
<mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>
我在 URL 映射方面需要一些帮助,在我的代码中,当我转到:
http://localhost:8080/register.asdf
http://localhost:8080/register.asddsdsd etc.
它总是 returns http://localhost:8080/register
但我想让它 404 NOT FOUND.
我该如何解决这个问题?
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(Config.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
@Controller
public class UserController {
@RequestMapping(path = "/register", method = RequestMethod.GET)
public String registerGet(Model model) {
return "register";
}
编辑: 我在 Config.java 中添加了以下代码并解决了谢谢。
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
您可以在 @RequestMapping
或 servlet.mapping
中限制映射更改。
更改请求映射:
@RequestMapping(path = "/register.htm", method = RequestMethod.GET)//for example
或servlet.mapping:
servlet.addMapping("*.htm");
编辑: 如果你使用 Spring 4.X 你可以使用这个:
<mvc:annotation-driven>
<mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>
来自 docs 使用下面的配置来限制不需要的扩展
<mvc:annotation-driven>
<mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>