Spring整合SpringMvc笔记

2023-11-08 17:48:47 浏览数 (1)

导坐标

代码语言:javascript复制
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.10.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.5</version>
    </dependency>

准备相关配置类

Servlet的配置类

代码语言:javascript复制
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {SpringMvcConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {SpringConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
}

Spring配置类

代码语言:javascript复制
@Configuration
@ComponentScan("com.itheima")  /*这里需要排除controller的扫描,我懒得弄了*/
public class SpringConfig {
}

SpringMvc配置类

代码语言:javascript复制
@Configuration
@ComponentScan("com.itheima.controller")
public class SpringMvcConfig {
}

控制器类

代码语言:javascript复制
@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("get")
    public String testOne() {
        return "Hello Test!";
    }
}

配置maven启动

启动成功

打开浏览器访问,访问成功

0 人点赞