【发布时间】:2015-12-21 16:35:39
【问题描述】:
我需要一些有关 URL 映射的帮助,在我的代码中:
http://localhost:8080/register.asdf
http://localhost:8080/register.asddsdsd etc.
它总是返回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);
}
【问题讨论】:
标签: spring spring-mvc http-status-code-404 url-mapping