SSM整合-Rest风格开发SpringMVC 05
电脑版发表于:2022/8/15 18:14
一、
二、环境
1、配置
1.1、新建spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描sprig的bean--> <!-- 只扫描controller下的--> <context:component-scan base-package="com.itheima.controller"/> <!-- mvc驱动--> <mvc:annotation-driven/> </beans>
1.2、修改applicationContext.xml,为了避免重复扫描
1.3、修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!--用于解决中文乱码--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--拦截--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2、centroller层
@RestController @RequestMapping("/user") public class UserController { @PostMapping public String save(User user) { System.out.println("save" + user); return ""; } @PutMapping public String update(User user) { System.out.println("update" + user); return ""; } @DeleteMapping("/{uuid}") public String delete(@PathVariable int uuid) { System.out.println("delete" + uuid); return ""; } @GetMapping("/{uuid}") public User get(@PathVariable int uuid) { System.out.println("get" + uuid); return null; } @GetMapping("/{page}/{size}") public List<User> getAll(@PathVariable int page, @PathVariable int size) { System.out.println("getAll" + page + ":" + size); return null; } @PostMapping("/login")//为了和save的请求做区分 public User login(String userName, String password) { System.out.println("login"+userName +";"+ password); return null; } }
3、在pom.xml添加tomcat插件坐标
<!--tomcat插件,启动当前的应用--> <build> <finalName>springmvc_ssm</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <!--http默认端口改为80端口--> <configuration> <!--修改tomcat启动的端口号--> <port>80</port> <!-- 修改tomcat部署当前项目的虚拟目录(项目的访问根路径) 这改为 / 就表示你访问localhost/就是你的index页面 --> <path>/</path> </configuration> </plugin> </plugins> </pluginManagement> </build>
4、用postman测试