零基礎快速入門SpringBoot2.0 (一)


零基礎快速入門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/#appendix-dependency-versions

 

五、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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM