【Spring-MVC】SpringMVC中的注解

30 篇文章 1 订阅

【Spring-MVC】SpringMVC中的注解

4.1 路径请求的注解

4.1.1 @RequestMapping

SpringMVC将请求映射在该注解下,该注解可以修饰在类上,也可以修饰在方法上,如果同时修饰在类上和方法上,那么请求的路径为类的请求下的方法的请求。

属性说明
name给映射地址指定一个别名,用的很少
value用于将指定的实际地址映射到方法或者类上,可以处理多个url,使用,隔开
path和value一样
method指定请求的方法类型,包括GETPOSTPUTPATCHDELETE
params指定请求中必须包含哪些参数,才让该方法处理
headers指定请求头必须包含哪些参数,才让该方法处理
consumes指定请求里面的内容的类型,指的就是Content-Type,比如text/plain、application/json
produces指定返回的内容类型,指定的是请求头里面的Accept里面,不是响应的Content-Type
4.1.2 @GetMapping

处理get请求,相当于是@RequestMapping中的method设置为了get,是对其的一个简化注解,只可以处理get请求

Annotation定义:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 这里标注了本质还是RequestMapping,但是设定了get方法
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping {
	//属性和RequestMapping一样,但是没有method属性
}
4.1.3 @PostMapping

处理post请求,和@GetMapping一样,都是对@RequestMapping进行了封装,里面的方法改为了post,只能处理post请求

Annotation定义:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 这里标注了本质还是RequestMapping,但是设定了post方法
@RequestMapping(method = RequestMethod.POST)
public @interface GetMapping {
	//属性和RequestMapping一样,但是没有method属性
}
4.1.4 @PutMapping

处理put请求,和@GetMapping一样,都是对@RequestMapping进行了封装,里面的方法改为了put,只能处理put请求

Annotation定义:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 这里标注了本质还是RequestMapping,但是设定了put方法
@RequestMapping(method = RequestMethod.PUT)
public @interface GetMapping {
	//属性和RequestMapping一样,但是没有method属性
}
4.1.5 @DeleteMapping

处理delete请求,和@GetMapping一样,都是对@RequestMapping进行了封装,里面的方法改为了delete,只能处理delete请求

Annotation定义:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 这里标注了本质还是RequestMapping,但是设定了delete方法
@RequestMapping(method = RequestMethod.DELETE)
public @interface GetMapping {
	//属性和RequestMapping一样,但是没有method属性
}
4.1.6 @PatchMapping

处理patch请求,和@GetMapping一样,都是对@RequestMapping进行了封装,里面的方法改为了patch,只能处理patch请求

Annotation定义:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 这里标注了本质还是RequestMapping,但是设定了patch方法
@RequestMapping(method = RequestMethod.PATCH)
public @interface GetMapping {
	//属性和RequestMapping一样,但是没有method属性
}

4.2 参数绑定的注解

4.2.1 @RequestParam

一般来处理Content-Type为:application/x-www-form-urlencoded(键值对,默认的表单提交就是该请求头)编码数据内容,在servlet阶段可以通过传入一个key来获取值。

属性说明
name和value绑定请求参数的名字
required被该注解表示的是否必须存在,默认为true,代表该参数必传,可以使用defaultValue为其设置默认值,防止报错
defaultValue如果请求没有该值,默认值为defaultValue的值,不设置的情况下为null

作用:

  • 来限定某一个参数必须存在
  • 为一个参数赋初始默认值
  • 绑定请求中的参数名
@GetMapping("/getData")
@ResponseBody
											       			  // 绑定请求参数名字。   必须传参数。     默认值为moon
public String getData(@RequestParam(name = "user_name",required = true,defaultValue = "moon") String username,String password){
  User user = new User();
  user.setUsername(username);
  user.setPassword(password);
  return user.toString();
}
4.2.2 @PathVariable

主要用在Restful开发风格中,可以接受到请求中的数据

属性说明
name和value绑定url的参数的名字
required被该注解表示的是否必须存在,默认为true,代表该参数必传

请求地址:http://localhost:8080/bind/getData3/moonLong/122333

// 				  请求地址  使用{}来获取参数
@GetMapping("/getData/{name}/{password}")
@ResponseBody
//										// 将name绑定到username上								默认都是password,不需要绑定
public String getData2(@PathVariable("name") String username,@PathVariable String password){
  User user = new User();
  user.setUsername(username);
  user.setPassword(password);
  return user.toString();
}
4.2.3 @RequestBody

用来接受请求体中的数据,不能放在get请求上,因为get请求没有请求体,而且并不是所有的post请求的body都支持

注意:

  • 不是全部的post都支持,比如默认的Contend-Type:application/x-www-form-urlencoded是不支持的。因为tomcat已经对请求进行了处理.

常见的Content-Type:

  • application/x-www-form-urlencoded:是form表单默认的enctype,如果form的method为get,name数据会变编码成key/value的格式(username=…&password=…)的字符串,然后这个字符串拼接url后面,通过?分隔;如果form的method为post,浏览器会将form中的数据封装到http body中,然后发送给服务器
  • text/plain:纯文本传输
  • text/html:html格式
  • image/jpg:jpg的图片
  • text/xml:xml格式–代表每一个标签
  • application/xml:xml数据格式–代表标签内的数据
  • application/json:json数据格式
  • application/msword:word数据格式
@PostMapping("/getData5")
@ResponseBody
/**
* 不加@RequestBody能读到数据,加了反而读取不到数据
* 原因是因为`application/x-www-form-urlencoded`格式的数据在Tomcat中
* 通过parseParameters()去解析body中的数据,填充到Parameters里面, 
* 所以后续在方法中想通过流的方式去读取body中的数据是读不到的
*/
public String getData5( User user){
  public String getData5(@RequestBody User user){
    return "getData5....";
  }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值