restful接口

请求

GET:读取(Read)
POST:新建(Create)
PUT:更新(Update)
PATCH:更新(Update),通常是部分更新
DELETE:删除(Delete)

注解

@RequestParam

RequestParam注解接收的参数是来自于requestHeader中,即请求头。URL是这样的:http://host:port/path?参数名=参数值

例如:xxx?username=123&password=456,

@RequestMapping("/xxx")
public String edit(Model model, @RequestParam Map<String, Object> paramMap ) {
    String username= paramMap.get("username").toString();
    String password= paramMap.get("password").toString;
    return page("edit");
}

@RequestMapping("/xxx")
public String edit(Model model, @RequestParam String username, @RequestParam String password) {
    return page("edit");
}

@RequestParam有三个配置参数:

    required 表示是否必须,默认为 true,必须。
    defaultValue 可设置请求参数的默认值。
    value 为接收url的参数名(相当于key值)。

@RequestParam用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性。

@RequestParam通常用于GET请求,也可用于其它类型的请求,例如:POST、DELETE等请求。

比如向表中插入单条数据,但是这样不支持批量插入数据啊,如果改用 json 字符串来传值的话,类型设置为 application/json,点击发送的话,会报错,后台接收不到值,为 null。

这时候,注解@RequestBody就派上用场了。

@RequestBody

RequestBody注解接收的参数则是来自于requestBody中,即请求体中。

// Map<String, Object>  键值对形式接收
@RequestMapping(value="/xxxx", method=RequestMethod.POST)
public String edit(@RequestBody Map<String, Object>  queryMap) {
    return page("edit");
}

// 定义vo对象接收(建议使用,方便取出对应数据)
@RequestMapping(value="/xxxx", method=RequestMethod.POST)
public String edit(@RequestBody User user) {
    return page("edit");
}

@RequestBody一般用于处理非 Content-Type: application/x-www-form-urlencoded编码格式的数据,比如:application/json、application/xml等类型的数据。

比如application/json类型,使用注解@RequestBody可以将body里面所有的json数据传到后端,后端再进行解析。

@PathVariable

参数值需要在url进行占位,URL是这样的:http://host:port/path/参数值 (用在resultful接口的传参)

例如:/xxx/{username}/{password}

@RequestMapping("/xxx/{username}/{password}")
public String edit(Model model, @PathVariable String username,@PathVariable String password) 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值