Force 404 error while adding extension to url

I need help mapping urls, 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 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 the following code to Config.java and solved it thanks.

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
}

      

+3


source to share


2 answers


You can limit your display by changing the value @RequestMapping

or 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>

      

0


source


From the docs use below config to restrict unwanted extensions



<mvc:annotation-driven>
    <mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>

      

0


source







All Articles