I need some help with URL mapping, in my code when i go to:
http://localhost:8080/register.asdf
http://localhost:8080/register.asddsdsd etc.
it always returns http://localhost:8080/register
but I want to make it 404 NOT FOUND.
How can I fix this?
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";
}
EDIT : i added following code in Config.java and solved thanks.
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
You can restrict your mapping changin the @RequestMapping
or the servlet.mapping
.
Change RequestMapping:
@RequestMapping(path = "/register.htm", method = RequestMethod.GET)//for example
Or servlet.mapping:
servlet.addMapping("*.htm");
EDIT: If you are using Spring 4.X you can use this:
<mvc:annotation-driven>
<mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>