2024年2月28日 星期三

[Spring Boot] 根據尚硅谷IT培训学校教程學習24-37

 這是Day4,一樣使用此教材: 

 尚硅谷IT培训学校: https://www.youtube.com/watch?v=XcblwCaqZ2I&list=PLmOn9nNkQxJEeIH75s5pdTUnCo9-xOc7c&index=24



自動配置列表
C:\Users\user\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\3.2.2\spring-boot-autoconfigure-3.2.2.jar!\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports


org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration


server.port的來源



自動配置SpringMVC

HiddenHttpMethodFilter

FormContentFilter

WebMvcConfigurer

addInterceptors

addCorsMappings

configureContentNegotiation

WebProperties

ResourceHandlerRegistry

classpath:/META-INF/resources/找資源
classpath:/resources/
classpath:/static/
classpath:/public/


classpath類路徑






setCachePeriod
setCacheControl
setUserLastModified




EnableWebMVCConfiguration


WelcomPageHandlerMapping
















spring.web.resources.add-mappings=true //cache
spring.web.resources.cache.period=3600 //秒 //有下面那行,就不用這行
spring.web.resources.cache.cachecontrol.max-age=7200 //超過的話就會去server抓
spring.web.resources.cache.use-last-modified=true //資源的最後修改時間,用來對比,沒變化就返回304







spring.mvc.webjars-path-pattern=/webjars/**
spring.mvc.static-path-pattern=/**

若改成 spring.mvc.static-path-pattern=/static/**




spring.web.resources-static-location=classpath:/a/,classpath:/b/




spring.web.resources.cache.cachecontrol.cache-public=true //共享緩存


 WebMvcConfigurer / ResourceHandlerRegistry / setCacheControl / maxAge: 


http://localhost:8080/static/test.txt

若加@EnableWebMvc 就會禁用默認值





禁用cache

EnableWebMvcConfiguration的爸爸:
DelegatingWebMvcConfiguration的內容: 
    @Autowired(
        required = false
    )
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
        }

    }



@Autowired 是把容器中所有WebMvcConfigurer組件都拿來(自動注入),所有configure都會被組合到這個類,保存所有WebMvcConfigurer



return new WebMvcConfigurer() {: 


Ant風格路徑






內容協商
一套系統多數據返回

基於請求頭
HTTP標準
Accept: application/json
Accept: application/xml
Accept: application/haha

/person?format=json

路徑匹配

@RestController
都會包含Response Body
默認會用JSON

C:\Users\user\.m2\repository\org\springframework\boot\spring-boot-starter-web\3.2.2\spring-boot-starter-web-3.2.2.pom

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>3.2.2</version>
      <scope>compile</scope>
    </dependency>


C:\Users\user\.m2\repository\org\springframework\boot\spring-boot-starter-json\3.2.2\spring-boot-starter-json-3.2.2.pom

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.15.3</version>
      <scope>compile</scope>
    </dependency>



Add a Maven dependency
Press Alt Insert to open the Generate context menu.


        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>


Jackson Dataformat Xml: 




進行內容協商,所以默認JSON被拿掉









瀏覽器優先text/html

開啟基於請求參數的內容協商功能
spring.mvc.contentnegotiation.favor-parameter=true

參數type
spring.mvc.contentnegotiation.parameter-name=type





try with 寫法: 自動關流



沒有留言:

張貼留言