首页 技术 正文
技术 2022年11月19日
0 收藏 599 点赞 3,486 浏览 4775 个字

零基础快速入门SpringBoot2.0 (一)

一、SpringBoot2.x依赖环境和版本新特性说明

简介:讲解新版本依赖环境和springboot2新特性概述

1、依赖版本jdk8以上, Springboot2.x用JDK8, 因为底层是 Spring framework5,

2、安装maven最新版本,maven3.2以上版本,下载地址 :https://maven.apache.org/download.cgi

3、Eclipse或者IDE

4、新特性

5、翻译工具:https://translate.google.cn/

6、springbootGitHub地址:https://github.com/spring-projects/spring-boot

7、springboot官方文档:https://spring.io/guides/gs/spring-boot/

二、快速创建SpringBoot2.x应用之手工创建web应用

简介:使用Maven手工创建SpringBoot2.x应用

手工创建:https://projects.spring.io/spring-boot/#quick-start

官方推荐包命名接口,不要使用默认 defaultPackage

官方文档: https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-using-the-default-package

例子:

com

+- example

+- myapplication

+- Application.java

|

+- customer

|   +- Customer.java

|   +- CustomerController.java

|   +- CustomerService.java

|   +- CustomerRepository.java

|

+- order

+- Order.java

+- OrderController.java

+- OrderService.java

+- OrderRepository.java

三、快速创建SpringBoot2.x应用之工具类自动创建web应用

简介:使用构建工具自动生成项目基本架构

  1. 工具自动创建:http://start.spring.io/

四、SpringBoot2.x的依赖默认Maven版本

简介:讲解SpringBoot2.x的默认Maven依赖版本

1、官网地址

https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/

五、SpringBoot2.xHTTP请求配置讲解

简介:SpringBoot2.xHTTP请求注解讲解和简化注解配置技巧

1、@RestController and @RequestMapping是springMVC的注解,不是springboot特有的

2、@RestController = @Controller+@ResponseBody

3、@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan

localhost:8080

六、开发必备工具PostMan接口工具介绍和使用

简介:模拟Http接口测试工具PostMan安装和讲解

1、接口调试工具安装和基本使用

2、下载地址:https://www.getpostman.com/

七、SpringBoot基础HTTP接口GET请求实战

简介:讲解springboot接口,http的get请求,各个注解使用

1、GET请求

1、单一参数@RequestMapping(path = “/{id}”, method = RequestMethod.GET)

1) public String getUser(@PathVariable String id ) {}

2)@RequestMapping(path = “/{depid}/{userid}”, method = RequestMethod.GET) 可以同时指定多个提交方法

getUser(@PathVariable(“depid”) String departmentID,@PathVariable(“userid”) String userid)

3)一个顶俩

@GetMapping = @RequestMapping(method = RequestMethod.GET)

@PostMapping = @RequestMapping(method = RequestMethod.POST)

@PutMapping = @RequestMapping(method = RequestMethod.PUT)

@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)

4)@RequestParam(value = “name”, required = true)

可以设置默认值,比如分页

4)@RequestBody 请求体映射实体类

需要指定http头为 content-type为application/json charset=utf-8

5)@RequestHeader 请求头,比如鉴权

@RequestHeader(“access_token”) String accessToken

6)HttpServletRequest request自动注入获取参数

八、常用json框架介绍和Jackson返回结果处理

简介:介绍常用json框架和注解的使用,自定义返回json结构和格式

1、常用框架 阿里 fastjson,谷歌gson等

JavaBean序列化为Json,性能:Jackson > FastJson > Gson > Json-lib 同个结构

Jackson、FastJson、Gson类库各有优点,各有自己的专长

空间换时间,时间换空间

2、jackson处理相关自动

指定字段不返回:@JsonIgnore

指定日期格式:@JsonFormat(pattern=”yyyy-MM-dd hh:mm:ss”,locale=”zh”,timezone=”GMT+8″)

空字段不返回:@JsonInclude(Include.NON_NUll)

指定别名:@JsonProperty

九、SpringBoot2.x目录文件结构讲解

简介:讲解SpringBoot目录文件结构和官方推荐的目录规范

1、目录讲解

src/main/java:存放代码

src/main/resources

static: 存放静态文件,比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)

templates:存放静态页面jsp,html,tpl

config:存放配置文件,application.properties

resources:

2、引入依赖 Thymeleaf

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

注意:如果不引人这个依赖包,html文件应该放在默认加载文件夹里面,

比如resources、static、public这个几个文件夹,才可以访问

3、同个文件的加载顺序,静态资源文件

Spring Boot 默认会挨个从

META/resources > resources > static > public  里面找是否存在相应的资源,如果有则直接返回。

4、默认配置

1)官网地址:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content

2)spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

5、静态资源文件存储在CDN

十、SpringBoot2.x文件上传实战

简介:讲解HTML页面文件上传和后端处理实战

1、讲解springboot文件上传 MultipartFile file,源自SpringMVC

1)静态页面直接访问:localhost:8080/index.html

注意点:

如果想要直接访问html页面,则需要把html放在springboot默认加载的文件夹下面

2)MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutStream方便和高效)

访问路径 http://localhost:8080/images/39020dbb-9253-41b9-8ff9-403309ff3f19.jpeg

十一、jar包方式运行web项目文件上传和访问(核心知识)

简介:讲解SpingBoot2.x使用 java -jar运行方式的图片上传和访问处理

1、文件大小配置,启动类里面配置

@Bean

public MultipartConfigElement multipartConfigElement() {

MultipartConfigFactory factory = new MultipartConfigFactory();

//单个文件最大

factory.setMaxFileSize(“10240KB”); //KB,MB

/// 设置总上传数据总大小

factory.setMaxRequestSize(“1024000KB”);

return factory.createMultipartConfig();

}

2、打包成jar包,需要增加maven依赖

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

如果没加相关依赖,执行maven打包,运行后会报错:no main manifest attribute, in XXX.jar

GUI:反编译工具,作用就是用于把class文件转换成java文件

3、文件上传和访问需要指定磁盘路径

application.properties中增加下面配置

1) web.images-path=/Users/jack/Desktop

2) spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path}

4、文件服务器:fastdfs,阿里云oss,nginx搭建一个简单的文件服务器

更多学习资料可参考:https://xdclass.net/html/course_catalogue.html?video_id=4

https://ke.qq.com/course/299498

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848