详解@RequestMapping注解

1、简介与源码

@RequestMapping注解是Spring Web层面开发常用的注解之一,用于映射请求URL和处理请求方法之间的对应关系,作用的非常强大的。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {

	String name() default "";

	@AliasFor("path")
	String[] value() default {};

	@AliasFor("value")
	String[] path() default {};

	RequestMethod[] method() default {};
	
	String[] params() default {};
	
	String[] headers() default {};

	String[] consumes() default {};
	
	String[] produces() default {};
}

  • Target元注解:用于声明被作用的注解可以使用在那些位置,ElementType.TYPE作用在类上,ElementType.METHOD作用在方法上。也就是说RequestMapping注解可以加在类和方法上。

  • Retention元注解:用于声明被作用的注解的生命周期,RetentionPolicy.RUNTIME表示被RequestMapping注解标记的类可以被反射加载到。

  • Mapping注解:用于处理URL - 控制器的映射,可以通过请求路径找到队友的Controller,Mapping的源码也是保持了与RequestMapping注解一样的Target + Retention。

  • Documented元注解:用于声明被作用的注解可以被导出为JavaDoc文档。

通过以上注解可以了解到RequestMapping注解的使用方式了,接下来就是了解内部各个属性含义、使用!



2、value与path

  • 这两个属性用法差不多,别名都是对方似乎没什么差别;通过源码的英文看了个大概好像是说value表示url的映射、path表示相对路径。其实使用起来没什么区别,都是可以使用的!

  • 细节:RequestMapping作用的位置,作用类、方法表示的路径意义不相同!

  • 作用方法:表示访问该路径会找到这个方法进行处理!

  • 作用类上:表示内部所有的方法都需要带上前缀父路径,在加上方法上的路径。

@Controller
@RequestMapping(value = "/father")
public class RequsetMappingAnnotation {

    @RequestMapping(value = "/t1")
    public String test1(){
        //    http:localhots:8080/项目名/father/t1
        return "index";
    }
    
    @RequestMapping(value = "/t2")
    public String test2(){
        //    http:localhots:8080/项目名/father/t2
        return "index";
    }
}

@Controller
public class RequsetMappingAnnotation {

    @RequestMapping(path = "/t1")
    public String test1(){
        //    http:localhots:8080/项目名/t1
        return "index";
    }
    
	@RequestMapping(path = "/t2")
    public String test1(){
        //    http:localhots:8080/项目名/t2
        return "index";
    }
}


3、methods

  • 该属性用于表示请求的方式,GET、POST、HEAD、PUT… 其他的请求方式提交会返回405!

  • 由于methods属性是一个数组因此可以一次性放很多的请求方式,默认不写表示所有的请求都接收!

  • 当然RequestMapping注解衍生出一些小弟,这些小弟就是专门的一种请求。例如:GetMapping表示get请求的mapping…

  • 不过这些小弟请求只能标记在方法上,内部其他的属性也都和RequestMapping一样。

//这里存在父路径father
//@RequestMapping(value = "/t2",method = {RequestMethod.POST,RequestMethod.GET})
//@GetMapping("/t2")
@PostMapping("/t2")			//只支持post提交
public String test2(){
    //    http:localhots:8080/项目名/father/t2
    return "hello";
}

在这里插入图片描述



4、params

  • params表示该请求路径中必须携带的参数有哪些,如果没有请求参数就会出错400

  • params也是一个数组,可以支持指定多个参数

  • params不仅仅支持固定的参数,还支持指定参数名+参数的值。

//类上注解RequestMapping存在父路径father
@RequestMapping(value = "/t3",params = "method=1")
public String test3(Model model){
    //    http:localhots:8080/项目名/father/t3
    model.addAttribute("message","t3");
    return "hello";
}

在这里插入图片描述



5、 headers

  • headers用于规定接受请求头中必须携带头、不能携带哪些头

  • 同时也可以指定header头参数值可以为哪些值,参数值不能为哪些值…

谷歌浏览器不好测试,使用firefox比较好测试模拟。

@RequestMapping(value = "/t4", headers = {"mytest=123456"})
public String test4(Model model){
    System.out.println("headers!");
    model.addAttribute("message","t4");
    return "hello";
}

在这里插入图片描述
在这里插入图片描述



6、consumes

  • 用于指定请求的内容类型,也就是contentType类型。可以通过HttpServletRequest.getContentType获取到页面的内容格式。

  • 一般常见的类型也就是text/html;charset=utf-8表示内容是一个utf-8编码的html页面

  • application/json表示是一个json数据格式。

  • 注意:consumes可以指定多个类型,请求的类型至少满足所规定的一个!

最常见的就是ajax与后端交互,使用application/json格式。

@RequestMapping(value = "/t5", consumes = {"text/*", "application/json"})
public void test5(HttpServletRequest req){
    System.out.println(req.getContentType());
}


7、produces

  • 用于指定响应的内容的内容的类型。

  • 比较常见的就是json 和 text/html。

使用jackson响应一个json格式的字符串给页面

@RequestMapping(value = "/t6", produces = {"application/json;charset=utf-8"})
@ResponseBody			//表示返回的值为字符串,不经过视图解析器
public String test6(Model model) throws JsonProcessingException {
    User user = new User("薯条", 21, "汉子");
    ObjectMapper objectMapper = new ObjectMapper();
    String str = objectMapper.writeValueAsString(user);
    return str;
}

在这里插入图片描述



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值