1. HttpMessageConverter message converter
(1) HttpMessageConverter Interface Source:
public interface HttpMessageConverter<T> { boolean canRead(Class<?> clazz, MediaType mediaType); boolean canWrite(Class<?> clazz, MediaType mediaType); List<MediaType> getSupportedMediaTypes(); T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException; void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException; }
The interface defines four methods are canRead (), canWrite (), write () method when a read () and write data to the read data.
(2) if the label configuration, the default configuration of RequestMappingHandlerAdapter (note the RequestMappingHandlerAdapter not AnnotationMethodHandlerAdapter), and configure the default HttpMessageConverter a bit for him:
- ByteArrayHttpMessageConverter converts byte arrays.
- StringHttpMessageConverter converts strings.
- ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.
- SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.
- FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.
- Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.
- MappingJacksonHttpMessageConverter converts to/from JSON — added if Jackson is present on the classpath.
- AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.
- RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.
- ByteArrayHttpMessageConverter: responsible for reading the data in binary format and write data in binary format;
- StringHttpMessageConverter: responsible for reading and writing data string format data in a binary format;
- ResourceHttpMessageConverter: responsible for reading the resource file and write the resource file data;
- FormHttpMessageConverter: submitted form is responsible for reading data (data format readable by application / x-www-form-urlencoded, can not be read multipart / form-data format of the data); responsible for writing application / x-www-from- urlencoded and multipart / form-data format of the data;
- MappingJacksonHttpMessageConverter: responsible for reading and writing data json format;
- SouceHttpMessageConverter: xml is responsible for reading and writing data in javax.xml.transform.Source defined;
- Jaxb2RootElementHttpMessageConverter: responsible for reading and writing data xml label format;
- AtomFeedHttpMessageConverter: responsible for reading and writing data in Atom format;
- RssChannelHttpMessageConverter: responsible for reading and writing data RSS format;
When using @RequestBody @ResponseBody and annotations, RequestMappingHandlerAdapter using them to read or write data to the respective formats.
When @RequestBody Note: The Content-Type header portion Request object type, matching one by one to read the data HttpMessageConverter suitable.
When @ResponseBody Note: The Accept Request object attribute header portion (comma delimited), accept one by one according to the type, to traverse to find HttpMessageConverter can handle.
(3) HttpMessageConverter class diagram
- HttpMessageConverter
- AbstractHttpMessageConverter
- AbstractJackson2HttpMessageConverter
- AbstractWireFeedHttpMessageConverter
- AbstractXmlHttpMessageConverter
- ByteArrayHttpMessageConverter
- GsonHttpMessageConverter
- ObjectToStringHttpMessageConverter
- ProtobufHttpMessageConverter
- ResourceHttpMessageConverter
- StringHttpMessageConverter
- SourceHttpMessageConverter
- AbstractHttpMessageConverter
-
- BufferedImageHttpMessageConverter
- FormHttpMessageConverter
- AllEncompassingFormHttpMessageConverter
- GenericHttpMessageConverter
- AbstractJackson2HttpMessageConverter
- GsonHttpMessageConverter
- Jaxb2CollectionHttpMessageConverter
2. HandlerMethodArgumentResolver parameter parser
In the beginner springmvc framework, I always had a question, why the controller method was able to put so many parameters, but also can get the desired object, such as HttpServletRequest or HttpServletResponse, various notes @ RequestParam, @ RequestHeader, @ RequestBody , @ PathVariable, @ ModelAttribute and so on.
Of course, the main character is: org.springframework.web.method.support.HandlerMethodArgumentResolver interface.
// interface defines the public interface HandlerMethodArgumentResolver { // for determining whether the process parameters decomposition, returns true need, and to call the following method will resolveArgument. Boolean supportsParameter (MethodParameter Parameter); // Object real processing parameters for decomposition method, the object parameter is returned in the controller method. ResolveArgument Object (MethodParameter Parameter, ModelAndViewContainer mavContainer, NativeWebRequest WebRequest, WebDataBinderFactory binderFactory) throws Exception; }
springmvc comes with some implementations:
- ServletRequestMethodArgumentResolver and ServletResponseMethodArgumentResolver handled automatically bound HttpServletRequest and HttpServletResponse
- RequestParamMapMethodArgumentResolver handled @RequestParam
- RequestHeaderMapMethodArgumentResolver handled @RequestHeader
- PathVariableMapMethodArgumentResolver handled @PathVariable
- ModelAttributeMethodProcessor handled @ModelAttribute
- RequestResponseBodyMethodProcessor handled @RequestBody
3. HandlerMethodReturnValueHandler return value resolver
HandlerMethodReturnValueHandler for the return value of the function execution Controller performs processing operations, springMVC provided a plurality HandlerMethodReturnValueHandler implementation class.
(1) HandlerMethodReturnValueHandler interface definition:
- supportsReturnType support the processing for determining whether the returned value.
- handleReturnValue realize the processing operation of the return value.
springMVC operation flow of the return value: the process is performed in the invokeAndHandle in ServletInvocableHandlerMethod, embodied in HandlerMethodReturnValueHandlerComposite in HandlerMethodReturnValueHandlerComposite return value processor contains all springMVC provided.
(2) HandlerMethodReturnValueHandler class diagram
- AbstractMessageConverterMethodProcessor
- AsyncTaskMethodReturnValueHandler
- CallableMethodReturnValueHandler
- DeferredResultMethodReturnValueHandler
- HandlerMethodReturnValueHandlerComposite
- HttpHeadersReturnValueHandler
- ListenableFutureReturnValueHandler
- MapMethodProcessor
- ModelAndViewMethodReturnValueHandler
- ModelAndViewResolverMethodReturnValueHandler