Spring MVC 接收HTTP请求参数的注解

1.Spring MVC中,处理的比较多的几种 HTTP请求信息格式(Content-Type) 

  • application/x-www-form-urlencoded(默认)   
  • multipart/form-data  (form表单里面有文件上传时,必须要指定enctype属性值为multipart/form-data,意思是以二进制流的形式传输文件)
  • application/json、application/xml等格式的数据 

HTTP请求中,request的body部分的数据编码格式由header部分的Content-Type指定

 

2.Spring MVC 用来处理请求参数的注解

Spring MVC 提供了多个注解来获取HTTP请求中的提交的数据内容,具体用哪个注解是根据请求的编码方式(request header content-type 值)来决定的。

@PathVariable  

@PathVariable 用来获取请求url中的参数

@RequestParam 

@RequestParam接收的数据是来自HTTP请求体 或 即请求头requestHeader中(也就是在url中,格式为xxx?)的QueryString中

@RequestParam 用来处理 请求体(RequestBody)中 以application/x-www-form-urlencoded 或者 multipart/form-data编码方式提交的 数据

RequestParam可以接受简单类型的属性,也可以接受对象类型

RequestParam实质是将Request.getParameter() 中的Key-Value参数Map 利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段。get方式中query String的值,和post方式中body data的值都会被Servlet接受到并转化到Request.getParameter()参数集中,所以@RequestParam可以获取的到 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/person")
public class PersonController {

    Logger logger = LoggerFactory.getLogger(PersonController.class);

    @PostMapping("/list")
    public String findList(@RequestParam String firstName, @RequestParam String lastName) {

        logger.info("wow, firstName is : " + firstName);
        logger.info("wow, lastName is : " + lastName);
        return "ok";

    }

}

请求参数放在请求体中,并且请求体的编码方式是application/x-www-form-urlencoded

 

 请求参数放在请求头中

 

  请求参数放在请求头中,如果是打开F12看到的是如下信息

@RequestBody

@RequestBody接收的参数是来自RequestBody中,即HTTP请求体。所以Get请求不能用@RequestBody注解。

@RequestBody 用来处理 请求体(RequestBody)中  application/json、application/xml等格式提交的数据

import lombok.Data;

@Data
public class PersonRequest {

    private Long id;
    private String firstName;
    private String lastName;
    private Integer age;
    private String phone;
    private String address;
}

 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

@RestController
@RequestMapping("/api/person")
public class PersonController {

    Logger logger = LoggerFactory.getLogger(PersonController.class);

    @PostMapping("/list")
    public void findList(@RequestBody PersonRequest request) {

        logger.info("request is : " + JSON.toJSONString(request));

    }
}

 POST测试如下

 

 打开F12测试,会看到如下

@ModelAttribute 

@ModelAttribute 注解类型将参数绑定到Model对象


3.Spring MVC  控制器方法的两种返回值

Spring MVC 在使用 @RequestMapping 后,返回值通常解析为跳转路径

但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,会直接返回 json 数据,写入 HTTP response body 中。 比如异步获取 json 数据。
 

     

@RequestBody @RequestParam 用混的话会报错:MissingServletRequestParameterException: Required String parameter 'xxx' is not present

 

posted on 2021-01-18 10:37  dreamstar  阅读(1355)  评论(0编辑  收藏  举报