zoukankan      html  css  js  c++  java
  • spring mvc 获取请求中参数方式

    一、请求为GET

      内容类型为:Content-Type: null (常用)

      接收方式为:键名称 / 有键名属性的类

      例子:

        request:http://localhost:8080/form?name=张三param&age=20

        接收:save(String name, Integer age) / save(User user)


      内容类型为:Content-Type: multipart/form-data; boundary=...6936  (参数存放到body中)(不用)

      接收方式为:键名称 / 有键名属性的类

      例子:

        request:http://localhost:8080/form

        接收:save(String name, Integer age) / save(User user)


      内容类型为:Content-Type: application/x-www-form-urlencoded  (参数存放到body中)(不用)

      接收方式为:不能接收


      内容类型为:Content-Type: application/json  (参数存放到body中)(常用)

      接收方式为: 有键名属性的类+@RequestBody

      例子:

        request:http://localhost:8080/form

        接收:save(@RequestBody User user)

    二、请求为POST

      内容类型为:Content-Type: null

      接收方式为:键名称 / 有键名属性的类

      例子:

        request:http://localhost:8080/form?name=张三param&age=20

        接收:save(String name, Integer age) / save(User user)


      内容类型为:Content-Type:multipart/form-data; boundary=...936  (参数存放到body中)(文件上传)(常用)

      接收方式为: 键名称 / 有键名属性的类

      例子:

        request:http://localhost:8080/form

        接收:save(String name, Integer age) / save(User user)


      内容类型为:Content-Type:application/x-www-form-urlencoded  (参数存放到body中)(表单提交)(常用)

      接收方式为: 键名称 / 有键名属性的类

      例子:

        request:http://localhost:8080/form

        接收:save(String name, Integer age) / save(User user)


      内容类型为:Content-Type: application/json  (参数存放到body中)(常用)

      接收方式为: 有键名属性的类+@RequestBody

      例子:

        request:http://localhost:8080/form

        接收:save(@RequestBody User user)

    三、Spring MVC中获取参数常用注解

    3.1 @RequestAttribute (request.getAttribute(String name)):用于获取自定义在请求中的参数

    3.2 @RequestParam(request.getParameter(String name)):用于获取 拼接在请求中的参数/表单提交中的参数

    3.3 @RequestHeader(request.getHeader(String name)):用于获取请求头中的参数

    3.4 @RequestBody:用于获取请求body中的json数据

    3.5 @RequestPart:用于获取Content-Type:multipart/form-data;中文件类型参数

    3.6 @PathParam:用于获取请请求中拼接的参数

    3.7 @PathVariable:用于获取URI中数据

  • 相关阅读:
    debian配置apache2.4配置虚拟主机遇到的问题
    Javascript关于attachEvent和addEventListener区别与兼容写法
    图解linux下top命令的使用
    idea报错:java 不支持发行版本5
    java-访问权限
    IDEA图标含义
    IDEA生成UML类图
    idea快捷键
    idea同时运行两个main()
    idea关闭vim编辑模式
  • 原文地址:https://www.cnblogs.com/pascall/p/10271690.html
Copyright © 2011-2022 走看看