SpringMVC_常用注解

4. 常用注解

  • RequestParam:把请求中指定名称的参数给controller方法中的形参赋值。
  • RequestBody:用于获取请求体内容。用于接收前端传递过来的JSON数据
  • PathVariable:用于绑定 url中的占位符,主要用于REST风格中
  • RequestHerder:用于获取请求消息头的值
  • CookieValue:用于把指定 cookie名称的值传入控制器方法参数

4.1. @RequestParam

4.1.1. 作用
  • 把请求中指定名称的参数赋值给方法的形参。
?username=tom  -->  public String hello(@requestParam("username") String name)
4.1.2. 属性
  • value:指定请求参数中的名称。
  • required:请求参数中是否必须提供此参数。默认值:true。表示必须提供,如果不提供将报错。
4.1.3. 使用场景
  • 当参数名称和方法形参名称不一致的时候可以使用该注解进行属性映射
  • 当某个参数必须传递,使用该注解
4.1.1. 示例1:

参数名称和方法形参名称不一致

controller:

@RequestMapping("/test/param")
public String testRequestParam(String name){
    System.out.println("name : "+name);
    return "success";
}

请求路径:

http://localhost:8080/test/param.do?name=tom  #controller可以正常接收参数值
http://localhost:8080/test/param.do?username=tom  #controller不能接收参数值

使用@RquestParam注解

    @RequestMapping("/test/param")
    public String testRequestParam(@RequestParam(value = "username") String name){
        System.out.println("name : "+name);
        return "success";
    }
4.1.2. 示例2:

参数token是一个非常重要的验证参数,访问方法时必须传递

    @RequestMapping("/test/param")
    public String testRequestParam(@RequestParam("username") String name,
                                   @RequestParam(value = "userToken",required = true) String token){
        System.out.println("name : "+name);
        System.out.println("token : "+token);
        return "success";
    }

访问路径:传递userToken,正常访问

http://localhost:8080/test/param.do?username=tom&userToken=werwer12312313

访问路径:没有传递userToken,400异常

http://localhost:8080/test/param.do?username=tom

4.2. @RequestBody

4.2.1. 作用:

用于获取请求体内容。直接使用得到是 key=value&key=value…结构的数据。

get请求方式不适用。

4.2.2. 属性:
  • required:是否必须有请求体。默认值是:true。当取值为 true 时,get 请求方式会报错。如果取值为 false,get请求得到是 null。
  • 使用场景
    • 将前端传递的JSON格式参数封装到pojo对象中
4.2.3. 示例1: 简单使用,介绍JSON字符串
  1. 添加支持JSON解析的依赖

  2. 编写controller方法,使用@RequestBody注解接收不解析json字符串

  3. 使用postman发送请求

  4. pom.xml添加支持json解析的依赖,SpringMVC默认使用jackson对json串进行解析

<!-- 版本管理 -->
<jackson-version>2.11.0</jackson-version>


<!-- 依赖jar -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson-version}</version>
</dependency>
  1. 编写controller方法测试
//普通字符串接收json串
@RequestMapping("/test/body")
public String testBody(@RequestBody String body){
    System.out.println(body); //{"name":"张三","age":20,"money":200.2}
    return "success";
}
//使用pojo对象接收json串
@RequestMapping("/test/body/user")
public String testBodyPojo(@RequestBody(required = false) User user){
    System.out.println(user);
    return "success";
}
  1. postman测试
    在这里插入图片描述

4.3. @PathVeriable

4.3.1. 作用:

用于绑定 url中的占位符。例如:请求 url中 /delete/{id},这个**{id}**就是 url占位符。

不支持get请求

url支持占位符是 spring3.0之后加入的。是 springmvc支持 rest风格 URL的一个重要标志。

4.3.2. 属性:
  • value:用于指定 url中占位符名称。
  • required:是否必须提供占位符。
4.3.3. 示例

controller方法

@RequestMapping(value = "/user/{id}/{name}",method = RequestMethod.POST)
public String testPathVariable(@PathVariable(value = "id",required = true)Integer id,@PathVariable("name")String name){
    System.out.println("id = "+id+" name = "+name);
    return "success";
}
4.3.4. restful

REST全称是Representational State Transfer,中文意思是表述(表征)性状态转移。他是一种设计风格。通过URL+HTTP 确定要执行的操作,restful很大一个特点是URL中没有动词,和版本号,通过HTTP请求方式区分不同的操作,使用请求头区分不同版本。

  • HTTP

    • 请求方式:
      • GET:查询
      • POST:新增-更新-删除
      • PUT:更新-新增
      • DELETE:删除
  • restful风格请求示例:

    • 根据ID查询订单: http://localhost:8080/orders/1001?pageNum=1&pageSize=5 GET

    • 新增订单: http://localhost:8080/orders POST

    • 更新订单: http://localhost:8080/orders/ordertime/2020-07003 PUT

      vaersion=v1.0

    • 删除订单: http://localhost:8080/orders/1002 DELETE

4.4. @RequestHeader

4.4.1. 作用

用于获取请求消息头中的值

4.4.2. 属性
  • value:指定要获取的消息头名称
  • required:是否必须有此消息头
4.4.3. 示例
  1. 获取已有消息头信息
  2. 获取自定义消息头信息
  • controller:
@RequestMapping("/header")
public String getHeader(@RequestHeader(value = "Host")String host,
                        @RequestHeader(value = "token",required = true)String token){
    System.out.println("Host: "+host);
    System.out.println("token: "+token);
    return "success";
}
  • postman测试
    在这里插入图片描述

4.5. @CookieValue

4.5.1. 作用:

用于把指定 cookie 名称的值传入控制器方法的参数中。

4.5.2. 属性:
  • value:指定 cookie 的名称。
  • required:是否必须有此 cookie。
4.5.3. 示例
  • controller方法
@RequestMapping(value = "/cookie/value",method = RequestMethod.GET)
public String getCookieVal(@CookieValue("JSESSIONID")String jsessionID){
    System.out.println("JSESSIONID: "+jsessionID);
    return "success";
}
  • postman测试
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44509920/article/details/107700259
今日推荐