3

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);
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Oğuz Tanrıkulu
  • 239
  • 2
  • 12

2 Answers2

0

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>
David Herrero
  • 704
  • 5
  • 17
0

From docs use below config to restrict the unwanted extensions

<mvc:annotation-driven>
    <mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>
Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59