似乎有一些奇怪的行为,我似乎无法查明其原因。当我访问特定的 url 时,我会收到 404 响应,而同一 Controller 类处理的其他 url 可以正常工作。我必须在 url 末尾添加一个尾随/才能调用该方法。
访问 localhost:8080/newprofile 时不会调用此方法
@RequestMapping(value="/newprofile", method=RequestMethod.GET)
public String newProfile(Model model, Principal principal) {
return "newprofile";
}
但是,访问 localhost:8080/login 时会调用此函数
@GetMapping("/login")
public String login() {
return "login";
}
我尝试了 GetMapping 和 RequestMapping,但这些方法从未被调用。
这两种方法都包含在我的 Controller 类中
@Controller
public class HomeResources {
//login
//new profile
}
最佳答案
有一个设置负责此类行为:
只需将其关闭即可:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseTrailingSlashMatch(false);
}
}
关于java - Spring MVC RequestMapping 需要尾部斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59914965/