zoukankan      html  css  js  c++  java
  • 【随手记录】关于@RequestPart 与 @RequestParam

     
     
     

    @RequestPart :看源码备注:

    Annotation that can be used to associate the part of a "multipart/form-data" request
     * with a method argument.
     *
     * <p>Supported method argument types include {@link MultipartFile} in conjunction with
     * Spring's {@link MultipartResolver} abstraction, {@code javax.servlet.http.Part} in
     * conjunction with Servlet 3.0 multipart requests, or otherwise for any other method
     * argument, the content of the part is passed through an {@link HttpMessageConverter}
     * taking into consideration the 'Content-Type' header of the request part. This is
     * analogous to what @{@link RequestBody} does to resolve an argument based on the
     * content of a non-multipart regular request.
     *
     * <p>Note that @{@link RequestParam} annotation can also be used to associate the part
     * of a "multipart/form-data" request with a method argument supporting the same method
     * argument types. The main difference is that when the method argument is not a String
     * or raw {@code MultipartFile} / {@code Part}, {@code @RequestParam} relies on type
     * conversion via a registered {@link Converter} or {@link PropertyEditor} while
     * {@link RequestPart} relies on {@link HttpMessageConverter HttpMessageConverters}
     * taking into consideration the 'Content-Type' header of the request part.
     * {@link RequestParam} is likely to be used with name-value form fields while
     * {@link RequestPart} is likely to be used with parts containing more complex content
     * e.g. JSON, XML).

    @RequestPart主要用来处理content-type为 multipart/form-data 或 multipart/mixed stream 发起的请求,可以获取请求中的参数,包括普通文本、文件或复杂对象比如json、xml等,针对json等复杂对象,需要明确对应的content-type,例如:

     发出来的请求头:

    Content-Type: multipart/form-data; boundary=xxxxxxxxxx

    ----xxxxxxxxxx
    Content-Disposition: form-data; name="jsonData"
    Content-Type: applicatoin/json
    {"name":"jack""age":25}

    ----xxxxxxxxxxxx

    @RequestParam:看源码备注:

    Annotation which indicates that a method parameter should be bound to a web
     * request parameter.
     *
     * <p>Supported for annotated handler methods in Spring MVC and Spring WebFlux
     * as follows:
     * <ul>
     * <li>In Spring MVC, "request parameters" map to query parameters, form data,
     * and parts in multipart requests. This is because the Servlet API combines
     * query parameters and form data into a single map called "parameters", and
     * that includes automatic parsing of the request body.
     * <li>In Spring WebFlux, "request parameters" map to query parameters only.
     * To work with all 3, query, form data, and multipart data, you can use data
     * binding to a command object annotated with {@link ModelAttribute}.
     * </ul>
     *
     * <p>If the method parameter type is {@link Map} and a request parameter name
     * is specified, then the request parameter value is converted to a {@link Map}
     * assuming an appropriate conversion strategy is available.
     *
     * <p>If the method parameter is {@link java.util.Map Map&lt;String, String&gt;} or
     * {@link org.springframework.util.MultiValueMap MultiValueMap&lt;String, String&gt;}
     * and a parameter name is not specified, then the map parameter is populated
     * with all request parameter names and values.

    @RequestParam默认主要来处理query parameters, form data,and parts in multipart requests, 且是 key-value键值对这种文本,要区分 

      Spring MVC:

        会把query parameters, form data,and parts in multipart requests 里面的参数组合到一起存放在一个参数Map里(key相同的参数,值会追加)

      Spring WebFlux:

        只会处理query parameters

    另外 SpringMVC处理multipart请求 可以看这里

  • 相关阅读:
    Hanoi塔
    采药
    进制转换(大数)
    Load Balancing with NGINX 负载均衡算法
    upstream模块实现反向代理的功能
    epoll
    在nginx启动后,如果我们要操作nginx,要怎么做呢 别增加无谓的上下文切换 异步非阻塞的方式来处理请求 worker的个数为cpu的核数 红黑树
    粘性会话 session affinity sticky session requests from the same client to be passed to the same server in a group of servers
    负载均衡 4层协议 7层协议
    A Secure Cookie Protocol 安全cookie协议 配置服务器Cookie
  • 原文地址:https://www.cnblogs.com/whaleX/p/14108650.html
Copyright © 2011-2022 走看看