一、概念
REST(Representational State Transfer,表述性状态转移)
REST并不是一种创新技术,它指的是一组架构约束条件和原则
符合REST的约束条件和原则的架构,就称它为RESTful架构
- RESTful核心内容
- 资源与URI
- 资源的表述
- 状态转移
- RESTful架构特点
- 统一了客户端访问资源的接口
- url 更加简洁,易于理解,便于扩展
- 有利于不同系统之间的资源共享
- RESTful 四种基本操作
- GET: 获取资源
- POST: 新建资源
- PUT: 修改资源
- DELETE: 删除资源
- RESTful 请求方式
-
get 请求
http:/localhost:8080/id method=‘get’
-
post 请求
http://localhost:8080/course method=‘post’
-
delete 请求
http://localhost:8080/id method=‘delete’
-
put 请求
http://localhost:8080/course method=‘put’
二、RESTful 实现
1、实体类
public class Course {
private int id;
private String name;
private double price;
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public double getPrice() {return price;}
public void setPrice(double price) {this.price = price;}
}
2、数据获取类
@Repository
public class CourseDAO {
private Map<Integer,Course> courses = new HashMap<Integer,Course>();
// 新增课程
public void add(Course course){
courses.put(course.getId(),course);
}
// 查询全部课程
public Collection<Course> getAll(){
return courses.values();
}
// 通过id查询课程
public Course getById(int id){
return courses.get(id);
}
// 修改课程
public void update(Course course){
courses.put(course.getId(),course);
}
// 删除课程
public void deleteById(int id){
courses.remove(id);
}
}
3、web.xml + html 实现Post 转化 put、delete
- web.xml 中添加如下过滤器
<!-- 该过滤器将post请求 转化为 put 请求、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>
- 在 form 表单中添加隐藏域,其 name 属性设置为 “_method” ,值 value 设为转换的 “PUT”、"DELETE"请求
<form action="${pageContext.request.contextPath}/delete/${course.id}" method="post">
<button class="btn" type="submit">
<%-- 设置隐藏域 结合过滤器将Post请求转换为put请求 --%>
<input type="hidden" name="_method" value="DELETE"/>
<span class="glyphicon glyphicon-trash">删除</span>
</button>
</form>
- springmvc.xml 消息转换器等配置
<mvc:annotation-driven >
<!-- 消息转换器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置自动扫描 -->
<context:component-scan base-package="com.moc"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
4、控制器类处理 RESTful 风格的响应
@Controller
public class CourseController {
@Autowired
private CourseDAO courseDAO;
// 添加课程 (只有 Post 请求才能进入该业务方法)
@PostMapping(value = "/add")
public String add(Course course) {
courseDAO.add(course);
return "redirect:/getAll"; // 重定向 请求到 /getAll
}
// 查询全部课程(只有 Get 请求才能进入该业务方法)
@GetMapping(value = "getAll")
public ModelAndView getAll() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("courses", courseDAO.getAll());
return modelAndView;
}
// 通过 id 查询课程(Get 请求进入该业务方法)
@GetMapping(value = "/getById/{id}") // id 是传入该业务方法的参数
public ModelAndView getById(@PathVariable(value = "id") int id) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("edit");
modelAndView.addObject("course", courseDAO.getById(id));
return modelAndView;
}
// 修改课程(Put 请求进入该业务方法)
@PutMapping(value = "/update")
public String update(Course course) {
courseDAO.update(course);
return "redirect:/getAll";
}
// 删除课程(Delete 请求进入该业务方法)
@DeleteMapping(value = "/delete/{id}")
public String delete(@PathVariable(value = "id") int id) {
courseDAO.deleteById(id);
return "redirect:/getAll";
}
}