Spring Boot官网Guide学习笔记

前言:

先从各种guide入手,先对Spring程序结构体系有一个宏观了解,再结合文档进行精细化学习,文档地址(2.6.2版本):https://docs.spring.io/spring-boot/docs/current/reference/html/

第一个demo

https://spring.io/quickstart

  1. @SpringBootApplication、@RestController、@GetMapping
  2. 本地配置tomcat要开权限,不然idea不能启动tomcat
  3. 代码示例:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    @SpringBootApplication
    @RestController
    public class DemoApplication {


    public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
    return String.format("Hello %s!", name);
    }

    }

构建RESTful风格的服务

https://spring.io/guides/gs/rest-service/#initial

  1. 通过 mvn clean package将app进行打包(jar),终端可以直接运行java -jar
  2. jar包和war包的区别,spring Boot项目的jar包内建服务器可以直接运行
  3. 不需要web.xml,纯Java

连接MySQL

https://spring.io/guides/gs/accessing-data-mysql/

  1. 需要添加新依赖,bash进入mysql时若用sudo命令要先输入sudo密码再输入root密码
  2. MySQL创建新用户后的%指允许在任何主机登陆,可换为ip或localhost(仅在本地)
  3. 在resources中修改application.properties文件,了解spring.jpa.hibernate.ddl-auto的四个类型,数据库用户和scheme也从这里配置
  4. 了解Hibernate ORM ,Hibernate 将 Java 类映射到数据库表中,从 Java 数据类型中映射到 SQL 数据类型中。此demo通过Hibernate将user类映射到MySQL中,可通过Datagrip查看
  5. 用postman发送请求测试接口
  6. 进行安全测试,防止sql注入攻击,将用户权限设置为只能对数据操作而无法涉及数据表结构(scheme)

使用RESTful风格的服务

https://spring.io/guides/gs/consuming-rest/

  1. 一般可以通过浏览器访问URL来使用一个web服务,但用变成方式来使用RESTful风格的服务是更有用的方法,这个demo要从接口中取数据放到springboot的控制台中以log形式显示
  2. 由于demo给定的接口无法访问(可能是墙的原因),CommandLineRunner启动会失败,故无法查看结果,接口应获得的内容如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    {

    type: "success",

    value: {

    id: 10,

    quote: "Really loving Spring Boot, makes stand alone Spring apps easy."

    }

    } 为一个两层结构的json对象
  3. @JsonIgnoreProperties表示任何未绑定在此类型中的属性都应被忽略,要将接口获得的数据与自定义的类型绑定(反序列化),需要使定义的变量名与json对象的键名完全一致,如果不匹配可以用@JsonProperty注释来指定json对象中的某个键(这也可以用于处理含特殊字符或其他不便于作为变量名出现的json键)

用spring Boot构建Web应用

https://spring.io/guides/gs/spring-boot/

  1. CommandLineRunner, ApplicationContext, DispatcherServlet
  2. 添加单元测试,先添加maven依赖. MockMvc, 两种测试。
  3. Spring Boot Actuator,帮助监控和管理web应用。添加依赖到maven,通过http://localhost:8080/actuator 可访问。详细见:https://docs.spring.io/spring-boot/docs/2.5.0/reference/htmlsingle/#actuator
  4. JMX,是Java Management Extensions(Java管理扩展)的缩写,是一个为应用程序植入管理功能的框架。
  5. For more details about each of these REST endpoints and how you can tune their settings with an application.properties file (in src/main/resources), see the https://docs.spring.io/spring-boot/docs/2.5.0/reference/htmlsingle/#production-ready-endpoints
  6. Groovy 另一门语言(基于Java)

使用 Spring MVC 提供 Web 内容

https://spring.io/guides/gs/serving-web-content/

  1. Model object, easy to use in view template
  2. Thymeleaf, 一个现代的服务器端 Java 模板引擎,适用于 Web 和独立环境。添加spring-boot-starter-thymeleaf
  3. Spring-boot-devtools, 可以实现指定目录(默认为classpath路径)下的文件进行更改后,项目自动重启,更改后的代码自动生效,热部署节省了“编写更改代码、重新启动应用程序并刷新浏览器以查看更改”这一过程的时间,添加依赖
  4. Spring Boot app serves static content from resources in the classpath at /static (or /public). The index.html resource is used as a welcome page.

保护 Web 应用程序,确认用户

https://spring.io/guides/gs/securing-web/

  1. 分两部分,先构建一个app(Spring MVC),再用Spring Security做登录界面
  2. Spring MVC负责请求的转发和视图管理
  3. If Spring Security is on the classpath, Spring Boot automatically secures all HTTP endpoints with “basic” authentication.
  4. 第二部分,首先添加ss的两个依赖(本身和测试),然后编写安全类
  5. 根据配置,Spring Security提供了一个过滤器,可以拦截请求和验证用户的过滤器。如果用户无法验证,页面将被重定向到/login?error,您的页面显示适当的错误信息。成功退出登录后,您的申请将发送到/login?logout,您的页面将显示适当的成功信息。
  6. 更新hello页面,We display the username by using Spring Security’s integration with HttpServletRequest#getRemoteUser(). The “Sign Out” form submits a POST to /logout. Upon successfully logging out, it redirects the user to /login?logout.
  7. 关于Thymeleaf:
    1
    2
    3
    4
    5
    ${user.name}表示“获取用户的变量,并调用其getName()方法”
    通过标签中的th:text属性来填充该标签的一段内容,意思是$表达式只能写在th标签内部,不然不会生效,使用th:text标签的值替换div标签里面的值,至于div里面的原有的值只是为了给前端开发时做展示用的.
    这样的话很好的做到了前后端分离.意味着div标签中的内容会被表达式${session.book}的值所替代,无论模板中它的内容是什么,之所以在模板中“多此一举“地填充它的内容,完全是为了它能够作为原型在浏览器中直接显示出来。
    访问spring-mvc中model的属性,语法格式为“${}”,如${user.id}可以获取model里的user对象的id属性
    为了模板更加易用,Thymeleaf还提供了一系列Utility对象(内置于Context中),可以通过#直接访问。例如${ #dates.format(dateVar, 'dd/MMM/yyyy HH:mm')} 使用java.util.Date的功能方法类
  8. 结果是智能允许名为user密码为password的用户登录

Spring Boot官网Guide学习笔记
http://silencezheng.top/2022/01/29/article30/
作者
SilenceZheng66
发布于
2022年1月29日
许可协议