SpringBoot之请求处理

SpringBoot之请求处理


请求映射

@RequestMapping

使用@RequestMapping可以将请求映射到对应的方法上,通过method属性可以指定请求方式,默认所有方式都可以,例如

// 将/home路径映射到home方法上,并指定请求方式为POST或GET
@RequestMapping(value = "/home", method = {RequestMethod.POST, RequestMethod.GET})
public String home(){
    return "MyHome";
}

当我们使用Rest风格(即使用HTTP请求方式动词来表示对资源的操作,例如,用Delete方式访问/user时表示删除用户)提交表单时,会发现,虽然SpringBoot可以处理Delete等请求方式,但表单却只能以Get/Post方式提交。

@RequestMapping(value = "/car", method = RequestMethod.DELETE)
public String car(){
    return "MyCar";
}

别慌,SpringBoot已经为我们想好了解决方案,首先,我们需要开启如下配置

# 开启隐藏请求方式过滤器
spring.mvc.hiddenmethod.filter.enabled=true

然后,我们可以通过在表单中添加一个_method字段来指定实际的请求方式,例如

<form action="/car" method="post">
  <input type="hidden" name="_method" value="delete">
  <input type="submit" value="提交">
</form>

这样,我们就可以delete方式将表单提交到/car了(实际上还是Post,只不过在后端SpringBoot改成了delete),注意:此时表单需要用post方式提交


@XXXMapping

如果你懒得为所有@RequestMapping指定为Get请求方式,你可以使用@GetMapping,他就是@RequestMapping(method=RequestMethod.GET)。同理常用的有以下几个:

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping

请求参数

@PathVariable

将路径变量映射到参数上,用法如下

@GetMapping("/user/{id}/{age}")
public Map<String, Object> user(@PathVariable("id") String id,
                                @PathVariable("age") Integer age,
                                @PathVariable Map<String, String> map) {
    Map<String, Object> result = new HashMap<>();

    result.put("id", id);
    result.put("age", age);
    result.put("map", map);
    return result;
}

访问http://127.0.0.1:8080/user/1/18,可以看到如下结果

{
	"id": "1",
	"map": {
		"id": "1",
		"age": "18"
	},
	"age": 18
}

其中,@PathVariablevalue表示该参数对应哪个路径变量,当该注解写在Map上,则会将所有的参数放入该Map里。


@RequestHeader

获取请求头的数据,例如

@GetMapping("/user")
public Map<String, Object> user(@RequestHeader("User-Agent") String userAgent,
                                @RequestHeader Map<String, String> header) {
    Map<String, Object> result = new HashMap<>();

    result.put("userAgent", userAgent);
    result.put("header", header);
    return result;
}

其中header里拿到的是所有的请求头数据。


@RequestParam

获取请求参数,如表单提交的数据,例如

@GetMapping("/user")
public Map<String, Object> user(@RequestParam("age") Integer age,
                                @RequestParam("interest") List<String> interest,
                                @RequestParam Map<String, String> map) {
    Map<String, Object> result = new HashMap<>();

    result.put("age", age);
    result.put("interest", interest);
    result.put("map", map);
    return result;
}

访问http://127.0.0.1:8080/user?age=1&interest=game&interest=football可以看到如下结果,发现map只能对于同名的两个参数,只能获取第一个。

{
	"interest": [
		"game",
		"football"
	],
	"map": {
		"age": "1",
		"interest": "game"
	},
	"age": 1
}

@CookieValue

获取Cookie的值,当Cookie中找不到所需要的值时,将无法访问,例如

//获取cookie
@GetMapping("/user")
public Map<String, Object> user(@CookieValue("BIDUPSID") String BIDUPSID,
                                @CookieValue("BIDUPSID") Cookie cookie) {
    Map<String, Object> result = new HashMap<>();

    result.put("BIDUPSID1", BIDUPSID);
    result.put(cookie.getName()+"2", cookie.getValue());

    return result;
}

@RequestBody

获取响应体,该注解只对Post请求有用,例如

// 获取请求体
@PostMapping("/save")
public String user(@RequestBody String content) {
    return content;
}

@RequestPart

获取上传的文件,可以使用MultipartFile类型参数来接受,若上传多个文件,则可以使用数组来接收,例如

// 首页
@PostMapping("/")
public String index(@RequestPart("icon") MultipartFile img,
                    @RequestPart("icons") MultipartFile[] imgs) {
    return "index";
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值