Web接口就是一个urlRestful风格的API是一种软件架构风格,是设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。在Restful风格中,用户请求的url使用同一个url,而用请求方式:get,post,delete,put...等对请求的处理方法进行区分,这样可以在前后台分离式的开发中使得前端开发人员不会对请求的资源地址产生混淆和大量的检查方法名的麻烦,形成一个统一的接口。传统方式:/adduser /user/add.do/queryUser?id=1/delUser?id=1/upadeUser?id=1Restful: /user 提交方式post 添加/user/1 提交方式 get 查询/user/1 提交方式 delete 删除/user/1 提交方式put 修改/user 提交方式get/user 提交方式put/dept/1/emp/1 提交方式delete在Restful风格中,现有规定如下:GET(SELECT):从服务器查询,可以在服务器通过请求的参数区分查询的方式。POST(CREATE):在服务器新建一个资源,调用insert操作。PUT(UPDATE):在服务器更新资源,调用update操作。DELETE(DELETE):从服务器删除资源,调用delete语句。要求:除了登录是post 其他都是get,针对于查询来说例如:/users 查询所有用户GET/users/2 查询指定id的用户 GET/users/3 删除指定id的用户 DELETE/users 添加用户 POST/users/3 更新指定id的用户 PUT
pom文件
<packaging>war</packaging><dependencies><!-- servlet jar --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.5.RELEASE</version></dependency><!-- springmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.1.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.5.RELEASE</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.2</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.0.1.Final</version></dependency><!-- 文件上传 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.3</version></dependency></dependencies><build><resources><!--需要设置打包的文件 特别是非java文件--><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes></resource></resources><plugins><!-- 编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target><compilerVersion>1.8</compilerVersion><encoding>UTF-8</encoding></configuration></plugin><!-- tomcat插件 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><uriEncoding>UTF-8</uriEncoding><path>/springmvc</path></configuration></plugin></plugins></build></project>
web.xml配置文件
<!-- post 方式的中文乱码解决 -->
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--引入spring mvc的核心控制器 配置 -->
<servlet>
<servlet-name>DispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载哪个配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc1.xml</param-value>
</init-param>
<!-- tomcat服务器启动时,创建servlet对象 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatchServlet</servlet-name>
<url-pattern>/</url-pattern>
<!--<url-pattern>*.do</url-pattern>-->
</servlet-mapping>
web.xml需要增加如下配置:
<!—配置HiddenHttpMethodFilter,将 post转为 put or delete提交方式 —
<!-- 配置HiddenHttpMethodFilter,将 post转为 put or delete提交方式 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
springmvc各项配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描控制器的注解-->
<context:component-scan base-package="com.qfedu"></context:component-scan>
<!-- 在日期格式化的时候遇到的-->
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- 第一种方式:将静态资源的处理经由Spring MVC框架交回Web应用服务器处理-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- 处理静态资源的方案2,但是我不用。-->
<!-- 静态资源的映射
location项目中的真实路径
mapping将真实的路径映射给当前起名字的路径
-->
<!-- <mvc:resources mapping="/js123/**" location="/js/"></mvc:resources>-->
</beans>
控制层
package com.qfedu.rest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class RestController {
//现在是get请求 查询一条数据
// @RequestMapping(value = "/user", method = RequestMethod.GET)
@GetMapping("/user")
public String findInfo(){
System.out.println("get info");
return "/index.jsp";
}
//增加一条数据
//@RequestMapping(value = "/user", method = RequestMethod.POST)
@PostMapping("/user")
public String addUser(){
System.out.println("post info");
return "/index.jsp";
}
//删除某一条数据
//@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
@DeleteMapping("/user/{id}")
//PathVariable 获取路径中的变量
public String deleteInfo (@PathVariable("id") Integer id) {
System.out.println(id);
return "/index.jsp";
}
//修改某一条数据
//@RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
@PutMapping("/user/{id}")
//PathVariable 获取路径中的变量
public String updateinfo (@PathVariable("id") Integer id) {
System.out.println(id);
return "/index.jsp";
}
}
前端jsp文件
<%--
Created by IntelliJ IDEA.
User: wangbo
Date: 2021/12/8
Time: 9:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="user" method="get">
<input type="submit" value="get">
</form>
<form action="user" method="post">
<input type="submit" value="post">
</form>
<form action="user/2" method="post">
<input type="hidden" name="_method" value="delete">
<input type="submit" value="delete">
</form>
<form action="user/4" method="post">
<input type="hidden" name="_method" value="put">
<input type="submit" value="update">
</form>
</body>
</html>
