13

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
        }

3 Answers 3

10

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);
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

To understand, true should match both /newprofile and /newprofile/ right ?
yes, so this is just part of your problem. Better to turn on verbose Spring logging and check whole process from request to response.
@AlexChernyshev If the default setting should match both, then how disabling it should solve the problem? It looks irrelevant.
7

Note that the default behaviour of setUseTrailingSlashMatch changed from true to false since 6.0 in order to support the deprecation of the property.

If you want this to be enabled you have to set it to true now. But as it is marked deprecated, probably best not to do it and instead follow the advice in this Spring Boot 3.0.0 M4 Release Notes

Developers should instead configure explicit redirects/rewrites through a proxy, a Servlet/web filter, or even declare the additional route explicitly on the controller handler (like @GetMapping("/some/greeting", "/some/greeting/") for more targeted cases.

Comments

3

Spring Boot 3+ TLDR

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

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

Be sure to use as above for Spring Boot 3+ (that uses Spring 6) to have URL ending with '/' still being processed by mapping in Controllers without ending '/'.

Longer answer:

Note for the snippet the value is exactly true
and @EnableWebMvc is not used with Spring Boot (as it would in fact disable autoconfiguration for web MVC)

That was exactly recommended in https://github.com/spring-projects-experimental/spring-boot-migrator/issues/206 "Spring Boot 3.0.0 M4 Release Notes"

And because there are reasons why Spring became less forgiving, ( see "Deprecate trailing slash match and change default value from true to false" https://github.com/spring-projects/spring-framework/issues/28552 ) I think we should better to think as bad habit to use URLs ending with '/', that now require extra attention.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.