spring-mvc-boot-5 custom parameter conversion and output JSON

First, the principle

HTTP Request = Request header (Header) + request body (Body) + URL + parameters, which further comprises a server and client interaction context session (Session) mechanism, and here refers to the conversion request message conversion body .

When a request comes, we will first be obtained from the http request parameters and context.

     Note 1: If it is a simple parameter, will be converted in a simple converter, which is provided SpringMVC itself. Such as int and so on.

     Note 2: If the conversion is http request body (BODY), calls HttpMessageConverter interface.

     Note 3: HttpMessageConverter interface call request body (Body) conversion,

               First of all, it will determine whether the conversion canRead.

               Then, the (user JSON) converts read parameters to the controller (User User).

      Note 4: The parameter conversion is used canRead + read

      Note 5: The output is used as JSON canWrite + write

               Return result types, and media types as parameters, then the method determines if MappingJackson2HttpMessageConverter conversion of canWrite



Two, HttpMessageConverter Interface analysis      

 public interface HttpMessageConverter<T> {

     1.是否可读,其中clazz为Java类型,mediaType为HTTP请求类型
     canRead(clazz,mediaType);

     2.是否可转换为mediaType类型
     canWrite(Class <?> clazz,MediaType mediaType);

     3.可支持的媒体类型列表
     List<MediaType> getSupportedMediaTypes();

     4.当canRead验证通过后,读入HTTP请求信息
      T read(clazz,inputMessage)

     5.当 canWrite 方法验证通过后,写入响应
      void write(T t,contentType,outputMessage)
}

Third, architecture analysis

--------------------------------------------------------
       +----------------------------+
       |  HttpMessageConverter      |
       +--------------+---------+---+
                      |        |||   +----------+
                      |         +----|调用控制器|
                      |              +----------+
                      |                |||
+---------------------|-----------------|------+
|webDataBinder       |||                |      |
|  +------------------------------+     |      |
|  | |---------|     |---------|  |     |      |
|  | |Converter|     |Formatter|  |     |      |
|  | +---------|     |---------+  |     |      |
|  |       |----------------|     |     |      |
|  |       |GenericConverter|     |     |      |
|  |       |----------------|     |     |      |
|  +---------------|--------------+     |      |
|           |------|----|        -+----------+ |
|           |   POJO    |---------| 验证机制 | |
|           |-----------|        -|----------| |
+----------------------------------------------+

Fourth, explain

1. SpringMVC is to obtain the parameters by WebDataBinder mechanism, its main role is to parse the context of the HTTP request, the controller before the call.

  •    WebDataBinder mechanism [authentication parameters acquisition parameters --- --- --- Conversion Parameters> Controller
  •    WebDataBinder mechanism through three interfaces Converter [[General] + Formatter [format conversion] + GenericConverter [Array]] converted to various parameters
  •    SpringMVC these three interfaces are used RI mechanism, and has registered a number of converters in the registered machines.

After 2. @ RequestBody denoted using the annotation content request the processor body (the BODY) performs parameter conversion, the front end thereof as JSON request type, SPRINGMVC calls the custom converter []:
 

前端的JSON类型数据---canRead方法---read方法---转换为控制器的(User)类参数  

3. Use Examples

  • 1-one conversion application: Converter-one custom converters: @ Component + realization Converter. springMVC automatically scans registered to the converter.


 


NOTE: Use url is - spaced string, the controller automatically converted to user


  • Application of Array 2 conversion: GenericConverter transducer array: the string is first split according to the sub-comma. Then follow the original generic type String, the generic objective of the User Converter <String, User> conversion.  


Guess you like

Origin blog.csdn.net/lidongliangzhicai/article/details/91978350