【springMVC_6_SpringMVC请求_不常用的注解】

本文详细介绍了Spring MVC中两个重要的注解:@RequestParam和@RequestBody。@RequestParam用于从请求参数中获取值,适用于GET和POST请求,而@RequestBody则用于读取请求体内容,通常用于POST请求。示例代码展示了如何在控制器方法中使用这两个注解来接收参数。
摘要由CSDN通过智能技术生成

三、不常用的注解

1.RequestParam注解
1. 作用:把请求中的指定名称的参数传递给控制器中的形参赋值
2. 属性
1. value:请求参数中的名称
2. required:请求参数中是否必须提供此参数,默认值是true,必须提供
3. 代码如下

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @RequestMapping("/save")
    public String save(@RequestParam(value = "username",required = false) String name){
        System.out.println(name);
        return "suc";
    }
}

2. RequestBody注解
1. 作用:用于获取请求体的内容(注意:get方法不可以)
2. 属性
1. required:是否必须有请求体,默认值是true
3. 代码如下

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @RequestMapping("/save")
    public String save(@RequestParam(value = "username",required = false) String name){
        System.out.println(name);
        return "suc";
    }

    @RequestMapping("/save2")
    public String save2(@RequestBody String body){
        System.out.println(body);
        return "suc";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值