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中数据

猜你喜欢

转载自www.cnblogs.com/pascall/p/10271690.html