3
votes

There seems to be weird behavior which I can't seem to pinpoint the reason for. When I access a particular url I get a 404 response while other urls that are handled by the same controller class works. I have to add a trailing / to the end of the url in order for the method to be called.

This method DOES NOT get called when accessing localhost:8080/newprofile

 @RequestMapping(value="/newprofile", method=RequestMethod.GET)
    public String newProfile(Model model, Principal principal) {
        return "newprofile";
    }

However, this one DOES get called when accessing localhost:8080/login

@GetMapping("/login")
public String login() {
    return "login";
}

I have tried both GetMapping and RequestMapping but the methods are never called.

Both methods are contained in my controller class

    @Controller
    public class HomeResources {
    //login
    //new profile
        }
1

1 Answers

4
votes

There is a setting responsible for such behavior:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.html#setUseTrailingSlashMatch-boolean-

Just turn it off:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

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