springBoot与Web后端基础

🚗🚗🚗🚗🚗🚗🚗 数据结构专栏🚗🚗🚗🚗🚗🚗🚗🚗🚗🚗

🛹🛹🛹🛹🛹🛹🛹小知识总结分享🛹🛹🛹🛹🛹🛹🛹🛹🛹🛹

🚀🚀🚀🚀🚀🚀🚀题目历练场🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀

🚢🚢🚢🚢🚢🚢🚢功能包与集合类介绍🚢🚢🚢🚢🚢🚢🚢🚢🚢

🛞🛞🛞🛞🛞🛞🛞java EE🛞🛞🛞🛞🛞🛞🛞🛞🛞🛞🛞🛞🛞

一、基本架构与spring Boot

1.1 两个基本架构

  • b/s 架构 : 客户端只需要浏览器,应用程序的逻辑与数据都在服务器上.
  • c/s 架构 : 单独开发与维护客户端.

1.2 spring Boot入门

可以帮助我们非常快速的构建应用程序,简化开发,提高效率.

二 、入门程序

需求 : 向着端口发送请求,得到回复数据

2.1 创建一个spring Boot工程,勾选相关依赖

勾选maven

2.2 定义一个controller类,添加注解,表明当前是一个请求处理类



3.3为什么一个main方法就将web应用启动了?

  • spring starts 起步依赖,因为依赖存在连续性质,来包含着tomcat起步依赖,将程序启动在tomcat服务器中.

三、HTTP协议

3.1 http特点

超文本传输协议,规定了浏览器与服务器之间的数据传输规则

  • 基于TCP协议:面向连接,安全。
  • 一次请求一次响应,对事务没有记忆能力,每次请求与响应都是独立的。

请求协议部分与响应协议部分

3.2.1 请求数据格式

  • 一般分为请求行,请求头,请求体三部分


3.2.2 请求数据获取

服务器会对http协议的请求进行解析,并进行封装(Http Servlet Request),在调用controller方法的时候传递给方法.

Java 复制代码
package com.six;  
  
import jakarta.servlet.http.HttpServletRequest;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
  
@RestController  
public class requestController {  
    @RequestMapping("/request")  
    public String request(HttpServletRequest request){  
        //1.获得请求方式  
        String method = request.getMethod();  //  GET  
        System.out.println("method" + method);  
        //2.获得请求路径  
        String path = request.getRequestURI();   // /requset  
        System.out.println("path" + path);  
  
        StringBuffer path2 = request.getRequestURL();  // http://localhost:8080/requset  
        System.out.println("path2" + path2);  
  
        //3.获得请求参数name ,age  
        String name = request.getParameter("name");  
        String age = request.getParameter("age");  
        System.out.println("name" + name);  
        System.out.println("age" + age);  
  
        //4.获得请accept求头  
        String accept = request.getHeader("accept");  
        System.out.println("accept" + accept);  
  
        //5.获得请求协议  
         String protocol = request.getProtocol();  
         System.out.println("protocol" + protocol);  
  
  
    return "ok";  
    }  
  
}

3.3.1响应数据格式

  • 状态码总共分为5类

  • 重定向

3.3.2响应数据设置

服务器会对http协议的请求进行解析,并进行封装(Http Servlet Response),在调用controller方法的时候传递给方法.通常情况下,不需要设置状态码与相应头。服务器会根据具体情况进行设置。

Java 复制代码
package com.six;  
  
import jakarta.servlet.http.HttpServletResponse;  
import org.springframework.http.ResponseEntity;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
  
import java.io.IOException;  
  
@RestController  
public class responseController {  
  
    @RequestMapping("/response")  
    public void response(HttpServletResponse response) throws IOException {  
        //设置响应头  
        response.setHeader("name", "duoduo");  
  
        //设置响应状态码  
        response.setStatus(200);  
  
        //设置响应体  
        response.getWriter().write("<h1>duoduo is very pretty</h1>");  
  
    }  
  
  
  
    // 使用spring自带进行设置  
  
    @RequestMapping("/response2")  
    public ResponseEntity<String> response2(){    // 泛型表示响应体的数据类型  
        return ResponseEntity.status(401).  
                header("name", "duoduo").  
                body("<h1>duoduo is very pretty</h1>");  
    }  
  
}

前后端数据读取基本步骤

四、分层解耦

4.1三层架构

  • Controller : 控制层,接受前段发送的请求,对请求进行处理,并响应数据
  • Service : 业务逻辑层
  • Dao : 数据访问层,负责数据的访问操作,增删改查...


4.2分层解耦

  • 高内聚,低耦合

    耦合 : 层与层,模块与模块之间的关连程度.

    内聚 : 各个模块之间功能联系

  • 层与层之间的解耦

4.3 IOC 与 DI

@Component : 将当前类交给IOC管理。查看bean是默认是类名字母小写,当然在注解时也可以添加val值进行命名.

@Autowired : 在实例化语句上添加注释,会从IOC中挑选相对应的对象,并赋值到新建对象上。

  • 属性注入 (最为简单快捷 但是隐藏了类之间的以来环境,可能破摔类的封装性)
  • 构造函数注入
Java 复制代码
  //构造器注入  
private final userService userService;  

@Autowired  
public userController(userService userService) {  
    this.userService = userService;  
}
  • setter注入
Java 复制代码
private userService userService;  
@Autowired  
public void setUserService(userService userService) {  
    this.userService = userService;  
}
当一个接口实现了多个实现类,怎么指定导入那个类

方案2,3均为bean 的名字,不是类名

resource 与 autowired的区别

  • 前者是Java EE提供的规范,后者是spring框架提供的。
  • 前者是按照名字进行注入,后者是按照类型进行注入。

总结

  • 到这里我的分享就先结束了~,希望对你有帮助
  • 我是dylan 下次见~
    • 无限进步
相关推荐
广州华水科技2 小时前
单北斗变形监测应用于水库的精准GNSS技术解析
前端
fengxin_rou2 小时前
黑马点评项目万字总结:从redis基础到实战应用详解
java·开发语言·分布式·后端·黑马点评
skiy2 小时前
SpringBoot项目中读取resource目录下的文件(六种方法)
spring boot·python·pycharm
不甘先生2 小时前
Go context 实战指南:从入门到生产级并发控制(架构师避坑手册)
开发语言·后端·golang
2401_878454532 小时前
HTML和CSS的复习2
前端·css·html
salipopl2 小时前
Spring Boot 整合 Druid 并开启监控
java·spring boot·后端
We་ct2 小时前
吃透现代CSS全技术体系
前端·css·css3·sass·postcss·预处理器
ZC跨境爬虫2 小时前
跟着 MDN 学 HTML day_11:(语义化容器全站重构+独立CSS拆分+字体合规引入)
前端·css·ui·重构·html·edge浏览器
ZC跨境爬虫2 小时前
跟着 MDN 学 HTML day_10:(超链接核心语法+路径规则)
前端·css·笔记·ui·html·edge浏览器