• 【随手记录】关于@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请求 可以看这里

  • 相关阅读:
    类和接口对比
    concurrenthasmap
    java中的三大注解
    基本数据类型,注意首字母
    常见ascii码记忆
    Java软件工程师面试题:Java运行时异常与一般异常有什么不一样?
    &和&&的共同点和区别、Java字符含义和Java创建对象的几种方式
    Java面试题中常考的容易混淆的知识点区别
    Java面试题整理:这些Java程序员面试中经常遇见的题目,必须掌握才能有好结果
    Spring Cloud 微服务架构的五脏六腑,统统晒一晒!
  • 原文地址:https://www.cnblogs.com/whaleX/p/14108650.html
Copyright © 2020-2023  润新知