• Spring Boot 2.X 对 web 的支持开发

    上章节的 Spring Boot 的入门案例,我们感受到 Spring Boot 简单的配置即可运行项目。

    今天了解 Spring Boot 对 web 的支持。

    Spring Boot 对 Web 开发的支持很全面,包括开发、测试和部署阶段都做了支持。spring-boot-starter-web

    是 Spring Boot 对 Web 开发提供支持的组件,主要包括 RESTful,参数校验、使用 Tomcat 作为内嵌容器器等功能。

  • Spring Boot 2.X 常用注解说明
    get: 查询一些信息						post:提交一些需要服务器保存的信息
    put: 更新,更新一些用户信息 delete:删除信息
    @GetMapping = @RequestMapping(method = RequestMethod.GET)
    @PostMapping = @RequestMapping(method = RequestMethod.POST)
    @PutMapping = @RequestMapping(method = RequestMethod.PUT)
    @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE) eg: @RequestMapping(value="/user",method = RequestMethod.GET)等同于 @GetMapping(value = "/user") 如果指定以 Post 的方式去请求,然后使用 Get 的方式(或其他非 post 请求方式)去请求的话,则会报 405 不允许访问的错误。如果不进⾏设置默认两种方式的请求都支持。
  • Spring Boot 对 JSON的支持以及常用 JSON 注解使用

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。现在大部分的数据交互方式都采用 JSON。 而 Spring Boot 对 JSON 支持很完善,在 Web 层仅需要一个注解即可。

    性能:Jackson > FastJson > Gson > Json-lib 同个结构。

    jackson处理相关自动(在实体类字段上使用以下注解)
    指定字段不返回:@JsonIgnore
    指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
    空字段不返回:@JsonInclude(Include.NON_NUll) --->对于字符串类型的不返回,int类型的返回0
    指定别名: @JsonProperty("XXXX")
  • Spring Boot 常见 web 中的传递参数方式

    ① 使用 URL 进行传参:@PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中,如 URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。

    @RequestMapping(value="get/{name}", method=RequestMethod.GET)
    public String get(@PathVariable String name) {
    return name;
    }
    在浏览器中输入网址:http://localhost:8080/get/Rookie,返回:Rookie,说明 name 值已经成功传入。

    ②数据校验:Web开发中最常见的就是输入数据的校验。

    ​ 在 Spring MVC 中有两种方式可以验证输入:Spring 自带的验证框架和利用 JSR 实现。

    ​ JSR 是一个规范文档,指定了一整套 API,通过标注给对象属性添加约束。

    ​ Hibernate Validator 就是 JSR 规范的具体实现。

    ​ Spring Boot 的参数校验依赖于 hibernate-validator 来进行。

    ​ 使用 Hibernate Validator 校验数据,需要定义一个接收的数据模型,使用注解的形式描述字段校验的 规则。

    实体类:
    @Getter
    @Setter
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    public class Person {
    @NotEmpty(message="姓名不能为空")
    private String name; @Max(value = 100, message = "年龄不能大于100岁")
    @Min(value= 18 ,message= "必须年满18岁!" )
    private int age; @NotEmpty(message="密码不能为空")
    @Length(min=6,message="密码长度不能小于6位")
    private String pass;
    } 请求接口:
    @RestController
    public class PersonController { @RequestMapping("/savePerson")
    public void savePerson(@Valid Person person, BindingResult result) { System.out.println("Person:"+person);
    if(result.hasErrors()){
    List<ObjectError> errorList = result.getAllErrors();
    errorList.stream().forEach(error-> System.out.println(error.getCode()+"====="+error.getDefaultMessage()));
    }
    }
    } 编写测试类:
    @Test
    public void savePerson() throws Exception {
    mockMvc.perform(MockMvcRequestBuilders.post("/savePerson")
    .param("name","")
    .param("age","666")
    .param("pass","test")
    );
    } 测试结果显示:
    Person:Person(name=, age=666, pass=test)
    Length=====密码长度不能小于6位
    Max=====年龄不能大于100岁
    NotEmpty=====姓名不能为空 结果显示已触发了校验规则,返回错误信息。在实际开发过程中可对错误信息进行包装,最后返回到前端展示。 @Valid:参数前面添加 @Valid 注解,代表此对象使用了参数校验;
    BindingResult:参数校验的结果会存储在此对象中,可以将错误信息打印出来。
    Hibernate Validator 基本上包含了常用的数据校验,包括校验属性是否为空、长度、大小、特定格式等完整的注解自己查表对比。
  • Spring Boot 常用获取读取配置文件的注解详解
    @PropertySource 指定配置文件位置
    @ConfigurationProperties 标注该注解的类与配置文件进行绑定,通过设置的前缀,来进行属性设置。 代码演示:
    Author 类:
    @Component
    @PropertySource("author.properties")
    @ConfigurationProperties(prefix = "author")
    public class Author { //Value("${author.name}")
    private String name; //Vlue("${author.age}")
    private int age;
    } author.properties 配置文件
    # 配置作者信息
    author.name=rookie
    author.age=18 启动类:
    @RestController
    @SpringBootApplication
    public class ConfigFileDemoApplication { public static void main(String[] args) {
    SpringApplication.run(ConfigFileDemoApplication.class, args);
    } @Autowired
    private Author author; @GetMapping("/author")
    public String getAuthor(){ return "姓名:"+author.getName()+"==========="+"年龄:"+author.getAge();
    } }

    因为我们在 Author 类中已经加了 @Component 注解,因此可以将此注解下的类作为 bean 注入到 Spring 容器中,方便使用。

    使用频次最高的就是 @PropertySource & @ConfigurationProperties 配合使用,需要注意的是当在 resources 下再创建 config 文件夹,再将 author.properties 放进 config 文件夹时,需要修改@PropertySource("classpath:config /author.properties") 才可以正确读取配置路径。

    当没使用 @ConfigurationProperties(prefix = "author") 注解的时,要想得到配置文件中属性,则需要结合以下注解进行数据源的配置(以上面 Author 类注释掉的部分为例):

    @Value("${author.name}")

    @Value("${author.age}")

    也可以直接写在 application.properties 中(不建议这么做,但如果是全局变量提倡这种方法),当写在此文件中时,不需要指明资源文件路劲,只需要指明前缀即可。

    @ImportResource 将外部的配置文件加载到程序中来。
    如果要让编写的 Spring 配置文件生效,如 beans.xml,可以使用 @ImportResource 注解,将配置文件导入进来。 代码演示:
    Student 类
    @Getter
    @Setter
    public class Student { private String name; private String sex; private String phone;
    } rookie.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.rookie.model.Student">
    <property name="name" value="jack"/>
    <property name="sex" value="男人"/>
    <property name="phone" value="132659896254"/>
    </bean>
    </beans> 启动类
    @RestController
    @SpringBootApplication
    @ImportResource(locations = "classpath:rookie.xml")
    public class ConfigFileDemoApplication { public static void main(String[] args) {
    SpringApplication.run(ConfigFileDemoApplication.class, args);
    } @Autowired
    private Student student; @GetMapping("/student")
    public String getStudent(){ return "姓名:"+student.getName()+"==========="+"性别:"+student.getSex()+"==========="+"手机号:"+student.getPhone();
    }
    }

Spring Boot 2.X 对 web 的开发支持(二)的更多相关文章

  1. [原创]Spring boot 框架构建jsp web应用

    说明 Spring boot支持将web项目打包成一个可执行的jar包,内嵌tomcat服务器,独立部署 为支持jsp,则必须将项目打包为war包 pom.xml中设置打包方式 <packagi ...

  2. 使用idea搭建Spring boot+jsp的简单web项目

    大家好: 这是我的第一篇博客文章,简单介绍一下Spring boot + jsp 的搭建流程,希望给跟我一样新接触Spring boot的读者一点儿启发. 开发工具:jdk1.8   idea2017 ...

  3. SpringBoot实战(十)之使用Spring Boot Actuator构建RESTful Web服务

    一.导入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  4. Spring Boot Security And JSON Web Token

    Spring Boot Security And JSON Web Token 说明 流程说明 何时生成和使用jwt,其实我们主要是token更有意义并携带一些信息 https://github.co ...

  5. 使用Spring Boot来加速Java web项目的开发

    我想,现在企业级的Java web项目应该或多或少都会使用到Spring框架的. 回首我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用 ...

  6. Spring Boot入门教程1、使用Spring Boot构建第一个Web应用程序

    一.前言 什么是Spring Boot?Spring Boot就是一个让你使用Spring构建应用时减少配置的一个框架.约定优于配置,一定程度上提高了开发效率.https://zhuanlan.zhi ...

  7. Spring Boot笔记五: Web开发之Webjar和静态资源映射规则

    目录 Webjar /** 访问当前项目的任何资源 欢迎页 标签页图标 Webjar 开始讲到Spring Boot的Web开发了,先介绍Webjar,这个其实就是把一些前端资源以jar包的形式导入到 ...

  8. [译]Spring Boot 构建一个RESTful Web服务

    翻译地址:https://spring.io/guides/gs/rest-service/ 构建一个RESTful Web服务 本指南将指导您完成使用spring创建一个“hello world”R ...

  9. Spring Boot 构建一个 RESTful Web Service

    1  项目目标: 构建一个 web service,接收get 请求 http://localhost:8080/greeting 响应一个json 结果: {"id":1,&qu ...

随机推荐

  1. 2018-2019 ACM-ICPC, Asia Seoul Regional Contest K TV Show Game 2-sat

    题目传送门 题意: 有n个人,k盏灯,灯有红蓝两种颜色,每个人都猜了三种灯的颜色,问如何安排灯的颜色,使得每个人猜的灯至少有两个是对的. 思路: 很容易想到2-sat,但是显然枚举每个人猜对的情况是不 ...

  2. 笔记62 Spring Boot快速入门(二)

    SpringBoot部署 一.jar方式 1.首先安装maven. <1>下载最新的maven版本:https://maven.apache.org/download.cgi <2& ...

  3. mysql 判断指定条件数据存不存在,不存在则插入

    折腾了半天终于把这个给折腾顺了,但是后来发现用不了竟然...悲剧啊,但是还是要记录下加深记忆 insert into table1 (field1, field2,field3) select ?fi ...

  4. JavaSE---枚举

    1.概述 1.1 某些情况下,一个类的对象是  有限且固定的,eg:四季... 1.2 手动实现枚举类: 1.1.1 私有化构造器 1.1.2 将类的属性用private final修饰: 将类的实例 ...

  5. leetcode-12双周赛-1246-删除回文子数组

    题目描述: 方法:区间dp O(N^3) class Solution: def minimumMoves(self, A: List[int]) -> int: N = len(A) dp = ...

  6. I. Five Day Couple--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/H来源:牛客网 题目描述 ...

  7. (4)centos7 基础命令

    1.显示文件列表 ls 显示当前目录下所有非隐藏的文件夹名称 -a #显示路径下所有文件及目录 (包括以.开头的隐藏文件) -l #除文件名称外,亦将文件型态.权限.拥有者.文件大小等资讯详细列出(不 ...

  8. HTTP六大请求

    标准Http协议支持六种请求方法,即: 1.GET 2.POST 3.PUT 4.Delete 5.HEAD 6.Options 但其实我们大部分情况下只用到了GET和POST.如果想设计一个符合RE ...

  9. 函数的四种调用模式.上下文调用.call.apply

    闭包:函数就是一个闭包,一个封闭的作用域;         返回函数,要返回多个函数就用一个对象封装一下,         立即执行函数+return 回调函数   JS动态创建的DOM,不会被搜索引 ...

  10. jquery 弥补ie6不支持input:hover状态

    <!doctype html><html>    <head>    <meta charset="utf-8">    <t ...