SpringBoot(二):WEB篇


  上篇文章介紹了Spring Boot的部分特性:SpringBoot(二):入門篇,講解了SpringBoot的啟動類、配置文件、日志、多環境等

  本章及后續幾章將基於此 搭建一個完整可供日常使用的WEB開發框架(有些未必是SpringBoot體系棧的功能,但是在日常開發中都會普遍使用到)

 

WEB層

JSON接口

  在SpringBoot中,開發web程序非常簡單,在第一章《SpringBoot(一):初章》中我們已經搭建了一個JSON接口的開發

  即只需要類上添加@RestController注解,默認類中的所有方法都會以JSON的格式返回(大家可以參考第一章,@RestController其實就是@Controller和@ResponseBody注解的組合)(@RestController其實並不是SpringBoot的專屬注解,在傳統Spring項目中我們依舊可以使用)

三種方式效果是等同的

@RestController
public class DemoController {

@RequestMapping("/login")
public User login(User user) {
return user;
}
}

@Controller
public class DemoController {

@RequestMapping("/login")
@ResponseBody
public User login(User user) {
return user;
}
}

@Controller
public class DemoController {

@RequestMapping("/login")
public void login(User user, HttpServletResponse response) {
response.getWriter.write(JSONObject.fromObject(user).toString());
}
}

 

JSON接口(FastJSON)

  @ResponseBody注解默認只支持Jackson或者GSON的,有意的同學可以查看RequestMappingHandlerAdapter類下的構造方法

  那么我們如果想讓FastJson處理我們的轉換,應該如何配置呢?下面貼上代碼

  首先,導入FastJson的依賴

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.46</version>
</dependency>

  配置方式1,繼承WebMvcConfigurerAdapter並且重寫 configureMessageConverters(List<HttpMessageConvert<?>> converts)方法

@SpringBootApplication
public class WebApplication extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);


        // 1.定義一個convert轉換消息的對象
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        // 2.添加FastJson的配置信息
        FastJsonConfig config = new FastJsonConfig();

        // 3.在convert中添加配置信息
        config.setSerializerFeatures(
                SerializerFeature.PrettyFormat
        );
        fastJsonHttpMessageConverter.setFastJsonConfig(config);

        // 4.將convert添加到converts中
        converters.add(fastJsonHttpMessageConverter);

    }

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

  配置方式2,使用@Bean注解返回一個HttpMessageConverts對象

@SpringBootApplication
public class WebApplication {
    @Bean
    public HttpMessageConverters converter() {
        // 1.定義一個convert轉換消息的對象
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        // 2.添加FastJson的配置信息
        FastJsonConfig config = new FastJsonConfig();

        // 3.在convert中添加配置信息
        config.setSerializerFeatures(
                SerializerFeature.PrettyFormat
        );
        fastJsonHttpMessageConverter.setFastJsonConfig(config);

        // 4.將convert轉換為HttpMessageConverters並且添加注冊到Spring容器中
        return new HttpMessageConverters(fastJsonHttpMessageConverter);

    }

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

 

Spring Boot 2.0的WebFlux(目前簡單了解即可)

  什么是Spring WebFlux?它是一種異步的、非阻塞的、支持背壓(Back pressure)機制的Web開發框架。

  PS:Spring Web MVC 是基於Servlet API和Servlet容器設計的,Spring WebFlux是基於Reactive Streams API和Servlet3.1+容器設計的

  

  要深入了解Spring的WebFlux,首先要知道Reactive Stream(響應式編程),具體可以參加知乎專欄的《響應式編程(Reactive Programming)介紹》

  相比函數式編程而言,它只是另外一種編程姿勢而已,就好像不論是從前面還是從后面,目的地都是一樣的,姿勢不同而已

  

  函數式編程:函數式編程是種編程方式,它將電腦運算視為函數的計算。函數編程語言最重要的基礎是λ演算(lambda calculus),而且λ演算的函數可以接受函數當作輸入(參數)和輸出(返回值)

  響應式編程:響應式編程是一種面向數據流和變化傳播的編程范式。這意味着可以在編程語言中很方便地表達靜態或動態的數據流,而相關的計算模型會自動將變化的值通過數據流進行傳播。

     (舉例:在非響應式編程中,a = b +  c; 標識將b+c的結果賦給a,之后改變b或c的值不會影響到a,但在響應式編程中,a的值是會隨着b和c的更新而更新的)

  WebFlux支持兩種編程風格

    1. 使用@Controller注解,與傳統Spring項目或者SpringBoot2.0版本之前的項目相同

    2. 使用Java 8 lambda的函數式編程風格

   

  TIP:目前,響應式編程不支持MySQL並且不支持MySQL的事務,所以對於WebFlux目前暫時不多闡述

  

 

Thymeleaf的使用

  在Java世界的MVC框架中,可供使用的視圖技術有很多:JSP(它並不是模板引擎)、Thymeleaf、Freemark、Velocity。其中Thymeleaf是SpringBoot官網推薦的一款優秀的模板引擎,它在HTML5/XHTML的視圖層表現的很好,在一定程度上,它是可以完全替代JSP+JSTL的。Thymeleaf強調的是自然模板,也就是允許模板作為產品原型使用(即可在瀏覽器直接打開,便於前端書寫調試,在作為視圖來使用時,動態的替換其中的部分數據內容)。

 


免責聲明!

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



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