发布日期:2019-05-06 16:35:58

本文是学习springmvc官方文档的学习摘要。 https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet

 

知识点

1) DispatcherServlet

  • DispatcherServlet
    • WebApplicationInitializer
    • AbstractAnnotationConfigDispatcherServletInitializer
    • AnnotationConfigWebApplicationContext
    • ServletRegistration
    • initialization parameters
      • contextClass
        • ConfigurableWebApplicationContext (By default XmlWebApplicationContext)
      • contextConfigLocation
      • namespace
        • By Default: [servlet-name]-servlet
      • throwExceptionIfNohandlerFound
        • By default it's false. so 404.  
        • default servlet handling is configured or not?
  • Context Hierarchy
    • RequestContextUtils
    • contextConfigLocation
  • Special Bean Types
    • HandlerMapping
    • HandlerAdapter
    • HandlerExceptionResolver
    • ViewResolver
    • LocaleResolver
    • LocaleContextResolver
    • ThemeResolver
    • MultipartResolver
    • FlashMapManager
  • Web MVC Config
    • DispatherServlet.properties
    • Spring Boot relies on MVC java configuration to configure Spring MVC and provides many extra convenient options
  • Servlet Config
    • WebApplicationInitializer
    • AbstractDispatcherServletInitializer
      • isAsyncSupported
    • AbstractAnnotationConfigDispatcherServletInitializer
  • Processing
    • DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE->LocaleResolver->ThemeResolver->MultipartResolver->Handler->ModelAndView->view render.
    • MultipartHttpServletRequest
    • last-modification-date
    • LastModified
  • Interception
    • HandlerInterceptor
      • preHandler
        • return true or false to determine if the execution chain should be continued or not.
      • postHandler
        • Relationship with @ResponseBody and ResponseEntity is too late to make any changes to the response.
        • ResponseBodyAdvice. RequestMappingHandlerAdapter
      • afterCompletion
  • Exceptions
    • HandlerExceptionResolver
      • SimpleMappingExceptionResolver
      • DefaultHandlerExceptionResolver
        • ResponseEntityExceptionHandler
      • ResponseStatusExceptionResolver
        • @ResponseStatus
      • ExceptionHandlerExceptionResolver
    • Exception resolver chain
      • by declaring multiple HandlerExceptionResolver
    • Container Error Page
      • <error-page><location>/error</location><error-page>
  • View Resolution
    • ViewResolver Implementations
      • AbstractCachingViewResolver
      • XmlViewResolver (WEB_INF/views.xml)
      • ResourceBundleViewResolver (WEB-INF/classes/view.properties)
      • UrlBasedViewResolver
      • InternalResourceViewResolver
      • FreeMarkerViewResolver
      • ContentNegotiatingViewResolver
    • Handling
      • View Resolver Chain (InternalResourceViewResolver should be configured as the last one.
      • Redirecting  (redirect: prefix in the view name)
      • Forwarding (forward: prefix in the view name)
      • Content Negotiation
  • Locale (RequestContext.getLocale())  org.springframework.web.servlet.i18n
    • Time Zone (RequestContext.getTimeZone()) : LocaleContextResolver
    • Header Resolver (accept-language)
    • Cookie Resolver: CookieLocaleResolver
    • Session Resolver: SessionLocaleResolver
    • Locale Inteceptor: LocaleChangeInterceptor
  • Themes  (org.springframework.ui.context.ThemeSource)
    • Define a theme
      • org.springframework.ui.context.support.ResourceBundleThemeSource loads properties files from the root of the classpath.
      • 保留字:themeSource  for a bean in the application context.
        • spring:theme custom tag
    • ThemeResolver
      • FixedThemeResolver : Selects a fixed theme, set by using the defaultThemeName property
      • SessionThemeResolver: The theme is maintained in the user's HTTP session. It needs to be set only once for each session but is not persisted between sessions.
      • CookieThemeResolver: The selected theme is stored in a cookie on the client.
      • ThemChangeInterceptor
  • Multipart Resolver (org.springframework.web.multipart) : multipartResolver
    • Apache Commons FileUpload: a bean of type CommonsMultipartResolver with a name of multipartResolver.
    • Servlet 3.0 
      • MultipartConfigElement
      • <multipart-config>
      • A bean of type StandardServletMultipartResolver with a name of multipartResolver
  • Logging (DEBUG-level and TRACE-level)
    • Sensitive Data
      • enabled explicitly through the enableLoggingReqeustDetails property on DispatcherServlet.

 

2)Filters

  • Form Data. (FormContentFilter)  application/x-www-form-urlencoded   ServletRequest.getParameter*()
  • Forwarded Headers. (ForwardedHeaderFilter) (load blancers) Forwarded Http headers  . removeOnly=true
  • Shallow ETag (ShallowEtagHeaderFilterIf-None-Match 304(NOT_MODIFIED).  
    • Refer to HTTP Caching
  • CORS (CorsFilter): Must be ordered adead of Spring Security's Chain of filters.

 

3)Annotated Controllers

  • Declaration
    • @Controller
    • @RestController
  • Request Mapping
    • Annoations
      • @RequestMapping
      • @GetMapping
      • @PostMapping
      • @PutMapping
      • @DeleteMapping
      • @PatchMapping
    • URI Patterns
      • ?
      • *
      • **
      • @PathVairable
    • Pattern Comparison
      • AntPathMatcher.getPatternComparator(String path)
      • AntPatternComparator
      • PathMatcher
    • Suffix Match
      • useSuffixPatternMatching(false)
      • favorPathExtension(false)
    • Suffix Match and RFD(Reflected File Download)
      • Context-Desposition:inline;filename=f.txt
      • HttpMessageConverter
    • Consumable Media Types
    • Producible Media Types
    • Parameters, Headers
    • HTTP HEAD, OPTIONS
    • Custom Annotations
      • RequestMappingHandlerMapping
      • RequestCondition
    • Explicit Registrations
      • RequestMappingHandlerMapping
      • RequestMappingInfo
  • Handler Methods
    • Method Arguments
    • Return Values
    • Type Conversion
      • WebDataBinder
      • FormattingConversionService
    • Matrix Variables
      • @MatrixVariable
      • /cars;color=red,green;year=2012
      • /cars;color=red;color=green;year=2012
      • MultiValueMap
      • <mvc:annotation-driven enable-matix-variables="true"/>
    • @RequestParam
    • @RequestHeader
    • @CookieValue
    • @ModelAttribute
      • BindingResult
    • @SessionAttributes
      • SessionStatus
    • @SessionAttribute
    • @RequestAttribute
    • Redirect Attributes
    • Flash Attributes
    • Multipart
    • @RequestBody
    • HttpEntity
    • @ResponseBody
      • Reactive types
    • ResponseEntity
    • Jackson JSON
      • @JsonView
  • Model
  • DataBinder
    • @InitBinder
    • WebDataBinder
  • Exceptions
    • @ExceptionHandler
    • ExceptionDepthComparator
  • Controller Advice
    • @ControllerAdvice
    • @RestControllerAdvice

 

4) URI Links

  • UriComponents
    • UriComponentsBuilder
    • UriComponents
    • URI
  • UriBuilder
    • UriBuilder
    • UriBuilderFactory
    • DefaultUriBuilderFactory
    • RestTemplate
    • WebClient
  • URI Encoding
  • Relative Servlet Requests
    • ServletUriComponentsBuilder
  • Links to Controllers
    • MvcUriComponentsBuilder
  • Links in Views
    • mvcUrl() from spring.tld Tag Libary
    • HandlerMethodMappingNamingStrategy

 

5) Asynchronous Requests

  • DeferredResult
  • Callable
  • Processing
    • ServletRequest.startAsync
    • AsyncContext
    • DispatcherType
    • Exception Handling
    • Interception
      • HandlerInterceptor
      • AsyncHandlerInterceptor
      • CallableProcessingInterceptor
      • DeferredResultProcessingInterceptor
    • Compared to WebFlux
  • HTTP Streaming
    • Objects
      • ResponseBodyEmitter
      • HttpMessageConverter
    • SSE (Server Sent Events): Refer to https://www.w3.org/TR/eventsource/
      • SseEmitter (a subclass of ResponseBodyEmitter)
    • Raw Data
      • StreamingResponseBody
  • Reactive Types
    • WebClient
    • ReactiveAdapterRegistry
  • Disconnects
    • WebSocket
    • heartbeat mechanism
  • Configuration
    • Must be enabled at the Servlet container level.
    • Servlet Container
      • Filter and Servlet declarations have an "asyncSupported" flag that is set to true.
      • ASYNC javax.servlet.DispatchType  for Filter mappings.
      • AbstractAnnotationConfigDispatcherInitializer
      • <async-supported>true</async-supported>
      • <dispatcher>ASYNC</dispatcher>
    • Spring MVC
      • configureAsyncSupport on WebMvcConfigurer
      • <async-support> under <mvc:annotation-driver>
      • AsyncTaskExecutor
      • SimpleAsynceTaskExecutor
      • DefferredResultProcessingInteceptor
      • CallableProcessingInterceptor
      • DefferedResult
      • Callable
      • ResponseBodyEmitter
      • SseEmitter
      • WebAsyncTask

 

6)CORS (Cross Origin Resource Sharing)

  • Introduction
  • Processing
    • Origin
    • CorsConfiguration
    • CorsProcessor
    • DefaultCorsProcessor
    • AbstractHandlerMapping
    • CorsConfigurationSource
    • allowCredentials and maxAges
  • @CrossOrigin
  • Global Configuration
    • Java Configuration
      • @EnableWebMvc
      • CorsRegistry
    • XML Configuration
      • <mvc:cors>
  • CORS Filter
    • CorsFilter
    • CorsConfigurationSource
    • UrlBasedCorsConfigurationSource

 

7)Web Security

8)HTTP Caching

  • CacheControl
  • Controllers
  • Static Resources
  • ETag Filter
    • ShallowEtagHeaderFilter

9) View Technologies

10)MVC Config

  • Enable MVC Configuration
    • @EnableWebMvc or <mvc:annotation-driven>
  • MVC Config API
    • Implement WebMvcConfigurer
  • Type Conversion 
  • Validation
  • Interceptors
  • Content Types
  • Message Converters
  • View Controllers
    • <mvc:view-controller path="/" view-name="home"/>
    • ViewControllerRegistry
  • View Resources
    • ViewResolverRegistry
    • <mvc:view-resolvers>
  • Static Resources
    • VersionResourceResolver
    • ContentVersionStrategy
  • Default Servlet
    • <mvc:default-servlet-handler/>
  • Path Matching
    • PathMatchConfigurer
    • <mvc:path-matching>
  • Advanced Java Config
    • DelegatingWebMvcConfiguration
    • WebMvcCOnfigurer
  • Advanced XML Config
    • BeanPostProcessor

11) HTTP/2

  • Serlvet 4.0 container is required to support HTTP/2

 

12 REST Clients: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client

  • RestTemplate (Will be deprecated and replaced by WebClient)
  • WebClient (Introduced since 5.0): Support both synchronous and asynchronous as well as streaming scenarios. Supports:
    • Non-blocking I/O
    • Reactive Streams back pressure
    • high concurrency with fewer hardware resources
    • Functional-style, fluent API that taks advantage of java 8 lambdas
    • Synchronous and asynchronous interactions
    • Streaming up to or streaming down from a server

 

13 Testing

spring-test

 

14 WebStockets

RFC 6455:https://tools.ietf.org/html/rfc6455

  • HTTP Versus WebSocket
  • When to Use WebSockets
  • WebSocket API
    • WebSocketHandler
    • WebSocket Handshake
    • Deployment
    • Server Configuration
    • Allowed Origins
    • SockJS Fallback
    • STOMP

15 Other Web Frameworks

Spring integration with third-party web frameworks

参考文档

发表评论