依赖引入
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
</plugin>
</plugins>
</build>
三个配置文件
public class DispatcherServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
protected Class<?>[] getRootConfigClasses()
{
return new Class<?>[]{BeanConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses()
{
return new Class<?>[]{WebConfig.class};
}
@Override
protected String[] getServletMappings()
{
return new String[]{"/"};
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = {BasePackageMarker.class})
public class WebConfig extends WebMvcConfigurerAdapter
{
}
@Configuration
public class BeanConfig
{
}
测试
@RestController
public class IndexController
{
@GetMapping("/test")
public String test() {
return "Hello there.";
}
}