Spring MVC框架:第七章:REST架构风格

简介: Spring MVC框架:第七章:REST架构风格

第一节 REST简介

1.概念

Representational State Transfer——表现层(资源)状态转化。是目前最流行的一种互联网软件架构风格。它倡导结构清晰、符合标准、易于理解、扩展方便的Web架构体系,主张严格按照HTTP协议中定义的规范设计结构严谨的Web应用架构体系。由于REST所倡导的理念让Web应用更易于开发和维护,更加优雅简洁,所以正得到越来越多网站的采用。

资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。要获取这个资源,访问它的URI就可以,因此 URI 即为每一个资源的独一无二的识别符。


表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层(Representation)。比如,文本可以用txt格式表现,也可以用HTML格式、XML格式、JSON格式表现,甚至可以采用二进制格式。


状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化”(State Transfer)。而这种转化是建立在表现层之上的,所以就是 “表现层状态转化”。具体说,就是 HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。


请求方式 作用

GET          查询

POST        保存

PUT          更新

DELETE     删除


2.REST风格的URL


REST风格要求我们不要再使用问号键值对的方式携带请求参数,而是从URL地址中获取。下面我们进行一下对比:



3.REST风格URL的好处

①含蓄,安全

使用问号键值对的方式给服务器传递数据太明显,容易被人利用来对系统进行破坏。使用REST风格携带数据不再需要明显的暴露数据的名称。

②风格统一


URL地址整体格式统一,从前到后始终都使用斜杠划分各个内容部分,用简单一致的格式表达语义。

③无状态


在调用一个接口(访问、操作资源)的时候,可以不用考虑上下文,不用考虑当前状态,极大的降低了系统设计的复杂度。

④严谨,规范


严格按照HTTP1.1协议中定义的请求方式本身的语义进行操作。

⑤简洁,优雅


过去做增删改查操作需要设计4个不同的URL,现在一个就够了


⑥丰富的语义

通过URL地址就可以知道资源之间的关系。

http://localhost:8080/shop 
http://localhost:8080/shop/product 
http://localhost:8080/shop/product/cellPhone 
http://localhost:8080/shop/product/cellPhone/iPhone

第二节 SpringMVC对四种请求方式的支持

1.说明


受HTML的限制,只有GET请求和POST请求是可以直接生成的。为了生成PUT和DELETE请求方式我们需要借助一个过滤器:org.springframework.web.filter.HiddenHttpMethodFilter,这个过滤器可以将POST请求转换为PUT或DELETE等其他形式。

2.HiddenHttpMethodFilter的使用方法

①在web.xml中进行配置,拦截所有资源

<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>

②在表单隐藏域中通过_method请求参数附带请求方式名称

jsp代码:

<input type="hidden" name="_method" value="put"/>

③通过点击超链接执行删除操作。

这是一个难点,超链接中没有表单隐藏域,所以需要将超链接转换为表单进行提交,这就需要借助于JavaScript。

[1]在页面上创建一个action属性为空的form表单

jsp代码:

<form method="POST">
  <input type="hidden" name="_method" value="DELETE" />
</form>

[2]给所有超链接绑定单击响应函数

jsp代码:

<a href="${pageContext.request.contextPath}/emp/ID" class="empRemove">删除</a>

jsp中jquery代码:

$(".empRemove").click(function(){
      //※※※※※※※※※以下操作将GET请求转换为POST请求※※※※※※※※※
      //1.先获取到当前超链接原本要访问的URL地址
      //this是当前被点击的超链接的引用,是DOM对象
      var targetUrl = this.href;
      //2.获取负责转换请求方式的表单的jQuery对象
      var $form = $("form");
      //3.将表单的action属性设置为超链接的URL地址
      $form.attr("action",targetUrl );
      //4.提交表单
      //将表单元素封装为jQuery对象后调用submit()方法可以提交表单,相当于点击表单的提交按钮
      $form.submit();
      //5.超链接不跳转
      return false;
});

@PathVariable注解

通过URL地址携带的数据需要通过@PathVariable注解来获取。它的用法是:

Handled代码:

//使用@PathVariable注解将URL地址中的变量匹配出来
@RequestMapping(value="/emp/{empId}", method=RequestMethod.DELETE)
public String testPathVariable(@PathVariable("empId") String empId) {
  System.out.println("empId="+empId);
  return "redirect:/result";
}

实战代码:

项目结构



web.xml

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
  <servlet>
    <servlet-name>springDispatcherServlet</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>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- Map all requests to the DispatcherServlet for handling -->
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
    <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>

spring-mvc.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    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-4.0.xsd">
  <!-- 包扫描 -->
  <context:component-scan base-package="com.*" />
  <!-- 加前缀后缀 -->
  <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/page/" />
    <property name="suffix" value=".jsp" />
  </bean>
  <!-- 保证常规资源可以访问 -->
  <mvc:annotation-driven></mvc:annotation-driven>
  <!-- 保证静态资源可以访问 -->
  <mvc:default-servlet-handler />
</beans>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/emp">去list页面</a>
</body>
</html>

dataList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
  $(function(){
    $(".remove").click(function(){
      var data = $(this).parents("tr").children("td:eq(1)").text();
      var confirm = window.confirm("你确定要"+data+"删除吗?");
      if(!confirm){
        return false;
      }
      //※※※※※※※※※以下操作将GET请求转换为POST请求※※※※※※※※※
      //1.先获取到当前超链接原本要访问的URL地址
      //this是当前被点击的超链接的引用,是DOM对象
      var targetUrl = this.href;
      //2.获取负责转换请求方式的表单的jQuery对象
      var $form = $("form");
      //3.将表单的action属性设置为超链接的URL地址
      $form.attr("action",targetUrl );
      //4.提交表单
      //将表单元素封装为jQuery对象后调用submit()方法可以提交表单,相当于点击表单的提交按钮
      $form.submit();
      //5.超链接不跳转
      return false;
    });
  });
</script>
</head>
<body>
  <form method="POST">
    <input type="hidden" name="_method" value="DELETE" />
  </form>
  <center>
    <table>
      <c:if test="${empty requestScope.list }">
        <tr>
          <td>没有查询到数据</td>
        </tr>
      </c:if>
      <c:if test="${!empty requestScope.list }">
        <tr>
          <td>ID</td>
          <td>姓名</td>
          <td>SSN</td>
          <td>部门名称</td>
          <td colspan="2">操作</td>
        </tr>
        <c:forEach items="${requestScope.list }" var="list">
          <tr>
            <td>${list.empId}</td>
            <td>${list.empName}</td>
            <td>${list.ssn }</td>
            <td>${list.department.deptName }</td>
            <td><a href="${pageContext.request.contextPath }/emp/${list.empId}" class="remove">删除</a></td>
            <td><a href="${pageContext.request.contextPath }/emp/${list.empId}">编辑</a></td>
          </tr>
        </c:forEach>
      </c:if>
      <tr>
        <td colspan="6" align="center">
        <a href="${pageContext.request.contextPath }/emp/list">添加</a></td>
      </tr>
    </table>
  </center>
</body>
</html>

dataEdit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <form action="${pageContext.request.contextPath }/emp"
    method="post">
    <input type="hidden" name="_method" value="put" />
    <input type="hidden" name="empId" value="${requestScope.emdit.empId }"/>
    <table>
      <tr>
        <td>姓名</td>
        <td><input type="text" name="empName" value="${emdit.empName }" />
        </td>
      </tr>
      <tr>
        <td>SSN</td>
        <td><input type="text" name="ssn" value="${emdit.ssn }" /></td>
      </tr>
      <tr>
        <td>所在部门</td>
        <td><select name="department.deptId">
            <c:if test="${!empty deptList }">
              <c:forEach items="${requestScope.deptList}" var="dept">
                <c:if test="${dept.deptId==requestScope.emdit.department.deptId }">
                  <option value="${dept.deptId }" selected="selected">${dept.deptName }</option>
                </c:if>
                <option value="${dept.deptId }" selected="selected">${dept.deptName }</option>
              </c:forEach>
            </c:if>
        </select></td>
      </tr>
      <tr>
        <td colspan="2"><input type="submit" value="保存" /></td>
      </tr>
    </table>
  </form>
</body>
</html>

dataAdd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/emp" method="post">
    <table>
      <tr>
        <td>姓名</td>
        <td>
          <input type="text" name="empName"/>
        </td>
      </tr>
      <tr>
        <td>SSN</td>
        <td>
          <input type="text" name="ssn"/>
        </td>
      </tr>
      <tr>
        <td>所在部门</td>
        <td>
          <select name="department.deptId">
            <c:if test="${empty deptList }">
              <option>部门数据查询失败!!!</option>
            </c:if>
            <c:if test="${!empty deptList }">
              <c:forEach items="${requestScope.deptList}" var="dept">
                <option value="${dept.deptId }">${dept.deptName }</option>
              </c:forEach>
            </c:if>
          </select>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <input type="submit" value="保存"/>
        </td>
      </tr>
    </table>
  </form>
</body>
</html>

DeptDao.java

package com.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import com.pojo.Department;
@Repository
public class DeptDao {
  private static Map<String, Department> dataMap;
  private static Map<String, Department> namedMap;
  static {
    dataMap = new HashMap<>();
    namedMap = new HashMap<>();
    Department department = new Department(UUID.randomUUID().toString(), "市场部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "销售部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "行政部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "人事部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "技术部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "公关部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
  }
  public static Department getDeptByName(String deptName) {
    return namedMap.get(deptName);
  }
  public static Department getDeptById(String uuid) {
    return dataMap.get(uuid);
  }
  public List<Department> getDeptList() {
    return new ArrayList<>(dataMap.values());
  }
}

EmpDao.java

package com.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import com.pojo.Department;
import com.pojo.Employee;
@Repository
public class EmpDao {
  private static Map<String, Employee> dataMap;
  static {
    dataMap = new HashMap<>();
    String empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "乔峰", "SSN001", DeptDao.getDeptByName("市场部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "虚竹", "SSN002", DeptDao.getDeptByName("市场部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "段誉", "SSN003", DeptDao.getDeptByName("市场部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "鸠摩智", "SSN004", DeptDao.getDeptByName("技术部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "萧远山", "SSN005", DeptDao.getDeptByName("技术部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "慕容复", "SSN006", DeptDao.getDeptByName("技术部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "段正淳", "SSN007", DeptDao.getDeptByName("公关部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "段延庆", "SSN008", DeptDao.getDeptByName("公关部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "丁春秋", "SSN009", DeptDao.getDeptByName("销售部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "无崖子", "SSN010", DeptDao.getDeptByName("人事部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "慕容博", "SSN011", DeptDao.getDeptByName("人事部")));
  }
  public void saveEmp(Employee employee) {
    String empId = UUID.randomUUID().toString();
    employee.setEmpId(empId);
    String deptId = employee.getDepartment().getDeptId();
    Department department = DeptDao.getDeptById(deptId);
    employee.setDepartment(department);
    dataMap.put(empId, employee);
  }
  public void removeEmp(String empId) {
    dataMap.remove(empId);
  }
  public void updateEmp(Employee employee) {
    String deptId = employee.getDepartment().getDeptId();
    Department department = DeptDao.getDeptById(deptId);
    employee.setDepartment(department);
    dataMap.put(employee.getEmpId(), employee);
  }
  public Employee getEmpById(String empId) {
    return dataMap.get(empId);
  }
  public List<Employee> getEmpList() {
    return new ArrayList<>(dataMap.values());
  }
}

Department

package com.pojo;
public class Department {
  private String deptId;
  private String deptName;
  public Department() {
  }
  public Department(String deptId, String deptName) {
    super();
    this.deptId = deptId;
    this.deptName = deptName;
  }
  public String getDeptId() {
    return deptId;
  }
  public void setDeptId(String deptId) {
    this.deptId = deptId;
  }
  public String getDeptName() {
    return deptName;
  }
  public void setDeptName(String deptName) {
    this.deptName = deptName;
  }
  @Override
  public String toString() {
    return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";
  }
}

Employee

package com.pojo;
public class Employee {
  private String empId;
  private String empName;
  private String ssn;
  private Department department;
  public Employee(String empId, String empName, String ssn, Department department) {
    super();
    this.empId = empId;
    this.empName = empName;
    this.ssn = ssn;
    this.department = department;
  }
  public Employee() {
  }
  public String getEmpId() {
    return empId;
  }
  public void setEmpId(String empId) {
    this.empId = empId;
  }
  public String getEmpName() {
    return empName;
  }
  public void setEmpName(String empName) {
    this.empName = empName;
  }
  public String getSsn() {
    return ssn;
  }
  public void setSsn(String ssn) {
    this.ssn = ssn;
  }
  public Department getDepartment() {
    return department;
  }
  public void setDepartment(Department department) {
    this.department = department;
  }
  @Override
  public String toString() {
    return "Employee [empId=" + empId + ", empName=" + empName + ", ssn=" + ssn + ", department=" + department
        + "]";
  }
}

Services

package com.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dao.DeptDao;
import com.dao.EmpDao;
import com.pojo.Department;
import com.pojo.Employee;
@Service
public class Services {
  @Autowired
  private DeptDao deptDao;
  @Autowired
  private EmpDao empDao;
  public List<Department> deptDaoList(){
    return deptDao.getDeptList();
  };
  public List<Employee> empDaoList(){
    return empDao.getEmpList();
  }
  public void saveData(Employee employee) {
    empDao.saveEmp(employee);
  }
  public void delectData(String empId) {
    empDao.removeEmp(empId);
  }
  public Employee emdit(String empId) {
    Employee empById = empDao.getEmpById(empId);
    return empById;
  }
  public void updateEmp(Employee employee) {
    empDao.updateEmp(employee);
  }
}

Handled

package com.handled;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.pojo.Department;
import com.pojo.Employee;
import com.services.Services;
import com.sun.tracing.dtrace.ProviderAttributes;
@Controller
public class Handled {
  @Autowired
  private Services services;
  @RequestMapping("/emp")
  public String toList(Model model) {
    List<Employee> empDaoList = services.empDaoList();
    model.addAttribute("list", empDaoList);
    return "dataList";
  }
  @RequestMapping("/emp/list")
  public String toAddData(Model model) {
    List<Department> deptDaoList = services.deptDaoList();
    model.addAttribute("deptList", deptDaoList);
    return "dataAdd";
  }
  @RequestMapping(value="/emp",method=RequestMethod.POST)
  public String addData(Employee employee) {
    services.saveData(employee);
    return "redirect:/emp";
  }
  @RequestMapping(value="/emp/{empId}",method=RequestMethod.DELETE)
  public String deleteData(@PathVariable("empId") String empId) {
    services.delectData(empId);
    return "redirect:/emp";
  }
  @RequestMapping(value="/emp/{empId}",method=RequestMethod.GET)
  public String emditData(@PathVariable("empId") String empId,Model model) {
    List<Department> deptDaoList = services.deptDaoList();
    Employee employee = services.emdit(empId);
    model.addAttribute("emdit",employee);
    model.addAttribute("deptList",deptDaoList);
    return "dataEdit";
  }
  @RequestMapping(value="/emp",method=RequestMethod.PUT)
  public String subemdData(Employee employee) {
    services.updateEmp(employee);
    return "redirect:/emp";
  }
}
相关文章
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
人工智能 JavaScript 前端开发
GenSX (不一样的AI应用框架)架构学习指南
GenSX 是一个基于 TypeScript 的函数式 AI 工作流框架,以“函数组合替代图编排”为核心理念。它通过纯函数组件、自动追踪与断点恢复等特性,让开发者用自然代码构建可追溯、易测试的 LLM 应用。支持多模型集成与插件化扩展,兼具灵活性与工程化优势。
1119 6
|
Java 数据库 数据安全/隐私保护
Spring Boot四层架构深度解析
本文详解Spring Boot四层架构(Controller-Service-DAO-Database)的核心思想与实战应用,涵盖职责划分、代码结构、依赖注入、事务管理及常见问题解决方案,助力构建高内聚、低耦合的企业级应用。
2200 1
|
前端开发 Java 开发者
MVC 架构模式技术详解与实践
本文档旨在全面解析软件工程中经典且至关重要的 MVC(Model-View-Controller) 架构模式。内容将深入探讨 MVC 的核心思想、三大组件的职责与交互关系、其优势与劣势,并重点分析其在现代 Web 开发中的具体实现,特别是以 Spring MVC 框架为例,详解其请求处理流程、核心组件及基本开发实践。通过本文档,读者将能够深刻理解 MVC 的设计哲学,并掌握基于该模式进行 Web 应用开发的能力。
2183 1
|
Kubernetes Java 微服务
Spring Cloud 微服务架构技术解析与实践指南
本文档全面介绍 Spring Cloud 微服务架构的核心组件、设计理念和实现方案。作为构建分布式系统的综合工具箱,Spring Cloud 为微服务架构提供了服务发现、配置管理、负载均衡、熔断器等关键功能的标准化实现。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
1096 0
|
人工智能 自然语言处理 JavaScript
Github又一AI黑科技项目,打造全栈架构,只需一个统一框架?
Motia 是一款现代化后端框架,融合 API 接口、后台任务、事件系统与 AI Agent,支持 JavaScript、TypeScript、Python 多语言协同开发。它提供可视化 Workbench、自动观测追踪、零配置部署等功能,帮助开发者高效构建事件驱动的工作流,显著降低部署与运维成本,提升 AI 项目落地效率。
1076 0
|
存储 Java 数据库连接
简单学Spring Boot | 博客项目的三层架构重构
本案例通过采用三层架构(数据访问层、业务逻辑层、表现层)重构项目,解决了集中式开发导致的代码臃肿问题。各层职责清晰,结合依赖注入实现解耦,提升了系统的可维护性、可测试性和可扩展性,为后续接入真实数据库奠定基础。
1039 0
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
1093 0
|
SQL Java 数据库连接
Spring、SpringMVC 与 MyBatis 核心知识点解析
我梳理的这些内容,涵盖了 Spring、SpringMVC 和 MyBatis 的核心知识点。 在 Spring 中,我了解到 IOC 是控制反转,把对象控制权交容器;DI 是依赖注入,有三种实现方式。Bean 有五种作用域,单例 bean 的线程安全问题及自动装配方式也清晰了。事务基于数据库和 AOP,有失效场景和七种传播行为。AOP 是面向切面编程,动态代理有 JDK 和 CGLIB 两种。 SpringMVC 的 11 步执行流程我烂熟于心,还有那些常用注解的用法。 MyBatis 里,#{} 和 ${} 的区别很关键,获取主键、处理字段与属性名不匹配的方法也掌握了。多表查询、动态
550 0