SpringBoot学习总结

1.2.5 关于请求常见异常
405 异常 ajax的请求类型与后端接收的请求类型不匹配.
400异常 参数类型不匹配
404异常 请求路径找不到
1.2.6 请求类型和业务关系

常见请求类型:
1.GET 查询操作
2.DELETE 删除操作 get/delete 用法相同
3.POST 1.表单数据提交 2.新增操作
4.PUT 修改操作 post/put 用法相同

1 SpringBoot 基本配置文件简要说明

springboot是spring提供的一个快速开发工具包,让程序员能更方便、更快速的开发spring+springmvc
应用,简化了配置(约定了默认配置),整合了一系列的解决方案(starter机制)、redis、
mongodb、es,可以开箱即用

定位: SpringBoot框架是框架的框架,简化常规框架的配置的方式,
只需要很少的代码,即可以实现大量的功能. 体现了**“开箱即用”** 思想

1.1pom.xml

 <!--maven坐标 必须唯一-->
    <groupId>com.jt</groupId>
    <artifactId>springboot_demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_demo1</name>
    <description>Demo project for Spring Boot</description>

    <!--
        配置信息:  JDK版本/设定了字符集/springboot版本信息
    -->
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

1.2 jar包依赖关系

springBoot内部已经完成了版本定义.所以无需再写具体版本号.

 <!-- 指定依赖项 -->
    <dependencies>
        <!--
            现象:
                springboot中的jar包文件缺少版本号
            原因:
                原来开发框架时需要自己手动添加版本号.SpringBoot框架
                引入之后,所有的版本号信息,由SpringBoot官网进行测试(springboot将市面上
                常用的框架进行了整合,并且测试了jar包的版本及依赖关系).springBoot内部已经完成
                了版本定义.所以无需再写.
         -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <!--springboot项目依赖管理器,其中定义了其它框架的jar包,
            以后使用直接添加jar包即可
    -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

1.3 springboot项目 maven 操作方式

<!--
        标签说明:
            maven操作springboot项目的工具
            1.编译工具 maven-compiler-plugin
              作用: maven执行指令时,通过插件管理SpringBoot项目
            2.项目打包工具 spring-boot-maven-plugin
                采用install的方式将springboot项目打成jar包.
                springboot的项目和常规项目不一样.所以需要添加插件
    -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.jt.SpringbootDemo1Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2.4 开箱即用原理说明

2.4.1简要介绍

springBoot将繁琐的配置封装到某些jar包中. 该jar包中的文件已经完成了配置.引入即可使用.只需要少量的配置就可以获取其功能的方式 叫做 “开箱即用”

2.4.2开箱即用规则-启动项

启动项: spring-boot-starter-xxxx
说明: 包中已经将框架进行了整合.用户拿来就用

 <!--springboot整合springmvc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2.4.3

说明: @ComponentScan(“包路径!!!”)
规则: 当前包扫描的路径 默认就是主启动类所在的包路径…
注意事项: 以后写代码 必须在主启动类所在包路径的 同包及子包中编辑
在这里插入图片描述

2.4.4 开箱即用-自动配置注解

注解名称: @EnableAutoConfiguration 启动自动化的配置
规则: 该注解的作用用来加载springBoot-start-xxx的启动项.当主启动类执行时,则会开始加载启动项中的配置.则功能加载成功.
在这里插入图片描述

2.5 SpringBoot-YML配置文件说明

2.5.1 properties文件说明

说明: SpringBoot项目中有多个配置文件.如果大量的重复的配置项都写到其中,则用户体验不好.
如果需要优化,则可以修改为yml文件
在这里插入图片描述

2.5.2 编辑application.yml

说明: 将application.properties的文件 修改名称为application.yml文件

# 基本语法
#   1.数据结构  key-value结构
#   2.写法     key: value    空格
#   3.有层级结构  注意缩进
server:
  port: 8080

3 lombok 插件

 <!--添加lombok的包-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
@Data   //Getter/Setter/RequiredArgsConstructor/ToString/EqualsAndHashCode
@Accessors(chain = true)//重写了set方法,链式加载
@NoArgsConstructor  //添加无参构造
@AllArgsConstructor //全参构造
    public Integer getId(){
        return this.id;
    }
    public void setId(Integer id){
        this.id=id;
    }
    
//    @Accessors(chain = true)  重写了set方法,链式加载
//    public User setId(Integer id){
//        this.id=id;
//        return this;//当前对象,运行期有效
//    }

案例测试

 @Value("${msg}")
    private String msg;//msg=我是SpringBoot-------

    @GetMapping("/hello")
    public String hello(){
        User user = new User();
        user.setId(1).setName("tom");//链式加载
        return "你好"+msg+user.getName()+user.getId();
    }

在这里插入图片描述

2 Ajax

async javascript and XML(异步JavaScript和XML)
是我们客户端给服务端发送消息,以及接受响应的⼯具
局部刷新,异步访问

什么是同步请求?(false)

同步请求是指当前发出请求后,浏览器什么都不能做,
必须得等到请求完成返回数据之后,才会执行后续的代码,
相当于生活中的排队,必须等待前一个人完成自己的事物,后一个人才能接着办。
也就是说,当JS代码加载到当前AJAX的时候会把页面里所有的代码停止加载,页面处于一个假死状态,
当这个AJAX执行完毕后才会继续运行其他代码页面解除假死状态

什么是异步请求?(默认:true)

默认异步:异步请求就当发出请求的同时,浏览器可以继续做任何事,
Ajax发送请求并不会影响页面的加载与用户的操作,相当于是在两条线上,各走各的,互不影响。
一般默认值为true,异步。异步请求可以完全不影响用户的体验效果,
无论请求的时间长或者短,用户都在专心的操作页面的其他内容,并不会有等待的感觉。

2.1 常见post请求种类

1.form表单提交 method=“post” 同步(页面是否刷新)
2.axios.post() 异步

axios简化写法

<script>
		// 简化1,抽取后端服务器地址
			axios.defaults.baseURL="http://localhost:8080"
			axios.get("/web/hello")
						//简化2,箭头函数:如果参数只有一个可以省略括号 
						//function(promise){  }   ----  promise=>{   }
						
					.then(promise=>
					{alert(promise.data)
				})
					/* 简化方式3: async await简化调用 重点
						   问题描述: 如果ajax如果嵌套的层级较多,则引发"回调地狱"问题
						   解决问题: 能否将axios中的then进行简化.
						   语法:
								1. 使用async关键字标识函数
								2. 通过await标识ajax请求
								3. 必须同时出现
						 */	
			async function getHHello(){
				let promise=await axios.get("/web/hello")
				alert(promise.data)
				let {data: result,status: code}=await axios.get("/web/hello")//从接收到返回的数据中提取属性
				alert(result)
				alert(code)
			}
			getHHello()//调用函数
			
		</script>

跨域问题

2.2.1 同源策略

要素:

  1. 浏览器URL中的地址 例子:http://localhost:8848/webDemo/demo/3-axios.html
  2. Ajax请求的URL地址 例子: http://localhost:8080/web/hello
    要求: 上述要素必须满足 协议/域名/端口号都相同时.表示满足同源策略.
    说明:
    **如果满足同源策略,则称之为 “同域访问” 反之称之为 “跨域访问” 跨域访问浏览器一般都会报错 **

2.2.2 关于跨域案例讲解

案例1:
浏览器地址: http://localhost:8080/xx/xxx
Ajax地址: https://localhost:8080/yy/yyyy 跨域请求:协议不同

案例2:
前提: 域名与IP地址对应
浏览器地址: http://www.jt.com:8080/xx/xxx
Ajax地址: http://10.4.5.2:8080/yy/yyyy 跨域请求:域名不同

案例3:
浏览器地址: http://www.jt.com/xx/xxx
Ajax地址: http://www.jt.com:80/yy/yyyy 同域请求:http端口号默认80

案例4:
浏览器地址: https://www.jt.com:443/xx/xxx
Ajax地址: https://www.jt.com:443/yy/yyyy 同域请求: https协议默认443端口

案例5:
迷惑: IP表示同一个网段
浏览器地址: http://192.168.10.2:80/xx/xxx
Ajax地址: http://192.168.11.2:80/yy/yy 跨域请求: 域名不匹配

2.2.3 跨域解决方案

1.jsonp 方式跨域 淘汰了.
2.CORS 跨域资源共享: CORS要求在服务器端标识哪个网址可以跨域

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值