配置信息
package com.example.mysite.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index").setViewName("index");
}
}
- 通过该配置,在浏览器中直接输入
localhost:8080/
即可访问,无需编写Controller中的@RequestMapping("/")
。 - 映射时,以Controller中的映射关系优先。假如此时有
@PostMapping("/")
,则返回405错误。